diff --git a/file_mover/file_mover.py b/file_mover/file_mover.py index d0fc7d51..816bea74 100755 --- a/file_mover/file_mover.py +++ b/file_mover/file_mover.py @@ -50,9 +50,12 @@ def main(): if sys.argv[1:]: config_file = sys.argv[1] remotes, jobs, settings = load_config(config_file) - for job in jobs: + for index, job in enumerate(jobs): src = create_remote(remotes[job["src"]], settings) dest = create_remote(remotes[job["dest"]], settings) + if index == 0: + #delete old files (not very adaptable, cant handle dirs in dirs) + dest.delete_remote_zips("/home/nonroot/backups") if 'demo_' in job["job_name"]: source_files = src.list_dir() distribute_files(source_files, dest) @@ -66,7 +69,6 @@ def main(): logging.warning(log_msg) sys.exit(1) src.delete_local_zip(zip_file_path) - dest.delete_remote_zips(zip_file_path) elif "mysqldump" in job["job_name"]: src.remote_channel_command(job, "mysqldump") zipname = src.remote_channel_command(job, "zip_mysqldump") diff --git a/file_mover/remote_sftp.py b/file_mover/remote_sftp.py index b35e1b7d..08c71af2 100755 --- a/file_mover/remote_sftp.py +++ b/file_mover/remote_sftp.py @@ -180,11 +180,16 @@ class sftp_remote: self.delete_file(last_modified, pathfile, 45) self.disconnect() - def delete_remote_zips(self, zip_file_path): + def delete_remote_zips(self, backups_dir): self.connect() - if zip_file_path.endswith(".zip"): - pathfile, last_modified = self.change_path(zip_file_path) - self.delete_file(last_modified, pathfile, 10) + for directories in self.sftp.listdir(backups_dir): + for files in self.sftp.listdir(f'{backups_dir}/{directories}'): + pathfile = f'{backups_dir}/{directories}/{files}' + if not pathfile.endswith(".zip"): + continue + utime = self.sftp.stat(pathfile).st_mtime + last_modified = datetime.fromtimestamp(utime) + self.delete_file(last_modified, pathfile, 10) self.disconnect() def delete_remote_zip_temp(self, zipname): @@ -195,4 +200,3 @@ class sftp_remote: def delete_local_zip(self, local_zip_path_name): os.remove(local_zip_path_name) -