diff --git a/backup.py b/backup.py index 5f5b063..da0b3d9 100644 --- a/backup.py +++ b/backup.py @@ -6,6 +6,8 @@ def main(): config = json.load(config_file) backup_path = config['local']['path'] + backup_server_age = config['local']['backup_server_age'] + backup_local_age = config['local']['backup_age'] hosts = config['remote'] @@ -15,13 +17,21 @@ def main(): backup_dir = '{}/{}'.format(backup_path, service).lower() if not os.path.exists(backup_dir): os.mkdir(backup_dir) + print('deleting local files older than {}days in {}'.format( + backup_local_age, backup_dir + )) + cmd = 'find {} -mindepth 1 -mtime +{} -delete'.format( + backup_dir, backup_local_age + ) + print('({})'.format(cmd)) + os.system(cmd) print('from ' + hosts[host][service] + ' to ' + backup_dir) cmd = 'scp {}:{}/* {}'.format(host, hosts[host][service], backup_dir) print('({})'.format(cmd)) os.system(cmd) - cmd = 'ssh {} rm -f {}/* '.format(host, hosts[host][service], - backup_dir) + cmd = 'ssh {} "find {} -mindepth 1 -mtime +{} -delete"'.format( + host, hosts[host][service],backup_dir, backup_server_age) print('({})'.format(cmd)) os.system(cmd) diff --git a/config.json.example b/config.json.example index 915ff3c..ff25f00 100644 --- a/config.json.example +++ b/config.json.example @@ -9,6 +9,8 @@ } }, "local" : { - "path" : "/backup/storage/folder/" + "path" : "/backup/storage/folder/", + "backup_server_age" : 7, + "backup_age" : 30 } } \ No newline at end of file