-
Notifications
You must be signed in to change notification settings - Fork 1
/
make_backup.py
126 lines (101 loc) · 4.58 KB
/
make_backup.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python3
import configparser
import arrow
import os.path
import os
import sys
from termcolor import cprint
from ipdb import set_trace
from settings import *
from util import *
list_cloud_provider = []
from commands import *
def init():
global list_cloud_provider
check_dir(location_files_to_backup)
check_dir(location_borg_files)
check_dir(location_tmp)
os.environ['BORG_RELOCATED_REPO_ACCESS_IS_OK'] = 'YES'
os.environ['RCLONE_CONFIG'] = location_rclone_config
config = configparser.ConfigParser()
config.read(location_rclone_config)
list_cloud_provider = config.sections()
def create_backup_test_file():
cprint("Let's create the file to test the backup", 'green')
file_abs = os.path.join(location_files_to_backup, test_backup_filename)
with open(file_abs, "w") as f:
f.write(str(arrow.now()))
def check_backup_test_file(mountpoint):
for dir_name, __, files in os.walk(mountpoint):
for file in files:
if file == test_backup_filename:
file_abs = os.path.join(dir_name, file)
break
with open(file_abs, "r") as f:
backup_creation_time = arrow.get(f.read())
now = arrow.now()
diff = now - backup_creation_time
cprint(f"Let's check if the backup work", "green")
print(f"Time backup created: {backup_creation_time}")
print(f"Time now: {now}")
print(f"Time diff: {diff}")
if diff.days == 0:
cprint(f"Checked the backup. Looks good \o/", "green")
else:
cprint(f"Error: Seems like the backup does not work. diff.days={diff.days}", "red")
sys.exit(1)
def do_backup():
cprint("Let's make a borg backup", 'green')
create_backup_test_file()
execute(cmd_borg_create_backup)
execute(cmd_borg_check_backup.format(location_borg_files=location_borg_files))
cprint("Done making a brog backup", 'green')
def put_backup_in_da_cloud(cloud_provider):
cprint(f"Let's put the borg files in da {cloud_provider} cloud", 'green')
execute(cmd_rclone_sync_to_cloud.format(location_borg_files=location_borg_files,
cloud_provider=cloud_provider))
execute(cmd_rclone_check.format(dir_to_check=location_borg_files,
cloud_provider=cloud_provider))
cprint("Done putting the borg files in da cloud", 'green')
def download_backup(cloud_provider, rclone_dir):
cprint(f"Let's download the cloud backup of {cloud_provider} to {rclone_dir}", 'green')
execute(cmd_rclone_syn_to_disk.format(cloud_provider=cloud_provider,
rclone_dir=rclone_dir))
execute(cmd_rclone_check.format(dir_to_check=rclone_dir,
cloud_provider=cloud_provider))
def test_backup(cloud_provider):
cprint(f"Let's test the cloud backup of {cloud_provider}", 'green')
rclone_dir = os.path.join(location_tmp, cloud_provider + "-files")
print(f"Clearing {rclone_dir}")
execute(f"rm -rf {rclone_dir}; mkdir {rclone_dir}")
download_backup(cloud_provider, rclone_dir)
mount_test_umount_backup(cloud_provider, rclone_dir)
print(f"Clearing {rclone_dir}")
execute(f"rm -rf {rclone_dir}")
def mount_test_umount_backup(cloud_provider, rclone_dir):
cprint(f"Let's mount the last the cloud backup of {cloud_provider}", 'green')
mount_dir = os.path.join(location_tmp, cloud_provider + "-mnt")
print(f"Clearing {mount_dir}")
execute(f"rm -rf {mount_dir}; mkdir {mount_dir}")
execute(cmd_borg_check_backup.format(location_borg_files=rclone_dir))
last_backup_name = execute(cmd_borg_list_last_backup_name.format(rclone_dir=rclone_dir))
print(f"Last backup name: {last_backup_name}")
execute(cmd_borg_mount_backup.format(rclone_dir=rclone_dir,
backup_name=last_backup_name,
mount_dir=mount_dir))
check_backup_test_file(mount_dir)
execute(cmd_borg_umount.format(mount_dir=mount_dir))
execute(f"rm -rf {mount_dir}")
def print_used_storage_for_provider(cloud_provider):
cprint(f"Used storage for {cloud_provider}", 'green')
execute(cmd_rclone_size.format(cloud_provider=cloud_provider))
if __name__ == '__main__':
init()
do_backup()
for cloud_provider in list_cloud_provider:
cprint(f"Let's go with {cloud_provider}", 'cyan')
put_backup_in_da_cloud(cloud_provider)
test_backup(cloud_provider)
cprint(f"Done with {cloud_provider}", 'cyan')
#for cloud_provider in list_cloud_provider:
#print_used_storage_for_provider(cloud_provider)