added checking for backup if it still needed if codes are recent, refactor script by making the logging and backup a function