Instead of trying to do all this implicitly with magic code and magic flag files, add four very simple single use functions to read and write either .env or config.json. Use those to set up config.json from a .env file if necessary.
With this in place, simplify and fix the backup and restore code.
Creating new backups, we no longer store the .env file since it is derived from the config.json.
Restoring an old backup, we load the .env file and create a config.json file from it, the same way we would do it at app start.
Instead of trying to do all this implicitly with magic code and magic flag files, add four very simple single use functions to read and write either .env or config.json. Use those to set up config.json from a .env file if necessary.
With this in place, simplify and fix the backup and restore code.
Creating new backups, we no longer store the .env file since it is derived from the config.json. Restoring an old backup, we load the .env file and create a config.json file from it, the same way we would do it at app start.