You have to start with the smallest and most conservative to the most "refactory" ones:
- figure out what enters and what exits from the program, and create a test suite that reflects every known combination
- move parts of main() to funcions
- figure out what variables can be converted from mutations to returned values
- move thr ifs(){} to maps, early returns and such
- figure out the database schemas from newly written data
- infer model relationships fro further writes
- attempt to implement some pattern design, even if its just dummy variables
Sometimes you even have to do it at the last stretch of time of your workday, and slowly the mess will become clearer, your test suit more exhaustive and comprehensive.
And at some point it will achieve feature parity with the legacy service