-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpybackup.py
executable file
·80 lines (61 loc) · 2.7 KB
/
pybackup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3
#
# Recursively removing old backup files in backup directories
#
# 2018-2021 Denis Pavlov
#
import os
import time
from configread import configread
from json import load
# set project name as current directory name
project = os.path.abspath(__file__).split('/')[-2]
# set config file name
conf_file = '/usr/local/pybackup/conf.d/pybackup.conf'
# read configuration parameters and save it to dictionary
backup_parameters = configread(conf_file, 'Backup', 'backup_custom_dirs_file',
'backup_main_dir', 'default_storage_period',
'debug')
backup_main_dir = backup_parameters['backup_main_dir']
default_storage_period = int(backup_parameters['default_storage_period'])
# get debug boolean variable from config for debugging enable/disable
debug = eval(backup_parameters['debug'])
# form dictionary of backup directories parameters
with open(backup_parameters['backup_custom_dirs_file'], "r") as backup_custom_dirs_file:
backup_custom_dirs = load(backup_custom_dirs_file)
print('Directories with custom config:')
for directory, parameters in backup_custom_dirs.items():
print(f'directory: {directory}, parameters: {parameters}')
def remove_old_files(entry, parent_storage_period):
if entry.is_dir():
if entry.path not in backup_custom_dirs:
if debug:
print(
f'Directory {entry.path} will be scanned for old backup files')
for entry in os.scandir(entry.path):
remove_old_files(entry, parent_storage_period)
else:
is_delete = backup_custom_dirs[entry.path].get('delete', 'no')
if is_delete == 'yes':
storage_period = backup_custom_dirs[entry.path].get(
'storage_period', parent_storage_period)
print(
f'Directory {entry.path} have custom config and '
f'will be scanned for backup files older than {storage_period} days')
for entry in os.scandir(entry.path):
remove_old_files(entry, storage_period)
else:
print(f'Directory {entry.path} is protected from deleting')
elif entry.is_file():
creation_days_ago = int(
(time.time() - os.stat(entry.path).st_mtime)/86400)
if creation_days_ago > parent_storage_period:
os.remove(entry.path)
print(
f'File {entry.path} was modified more than {parent_storage_period} days ago and deleted')
def main():
# scan backup root directory
for entry in os.scandir(backup_main_dir):
remove_old_files(entry, default_storage_period)
if __name__ == "__main__":
main()