-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpull_backup.py
executable file
·87 lines (65 loc) · 3.25 KB
/
pull_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
#!/usr/bin/python
import os
import shutil
import argparse
VERBOSE = 0
DEBUG = 0
ITERATIONS = 7
BACKUP_FOLDER = "/backup/"
CONFIG = "/etc/backup.conf"
SCP_USER = "root@"
###########################################
# Extra stuff
parser = argparse.ArgumentParser(prog='pull_backup.py')
parser.add_argument('-v','--verbose',dest="verbose",help='Turn verbosity on',default=False,action="store_true")
parser.add_argument('-d','--debug',dest="debug",help='Turn debug_messages on',default=False,action="store_true")
parser.add_argument('-c','--config',dest="config", help='What config file to use', metavar="FILE",default=CONFIG)
parser.add_argument('-i','--iterations',dest="iterations",type=int, help='How many backup iterations to do', metavar="N",default=7)
parser.add_argument('-b','--backup-directory',dest="backup_folder", help="Where to keep the backup files", metavar="FOLDER",default=BACKUP_FOLDER)
arguments = parser.parse_args()
VERBOSE = arguments.verbose
DEBUG = arguments.debug
ITERATIONS = arguments.iterations
BACKUP_FOLDER = arguments.backup_folder
CONFIG = str(arguments.config)
##########################################
def verbose(text):
print text if VERBOSE else ""
def debug(text):
print text if DEBUG else ""
verbose("Opening config file: " + CONFIG);
with open(CONFIG) as config:
for line in config:
verbose("Read line: " + line)
configlist = line.split(":")
pathlist = configlist[1].split(",")
verbose("host: " + configlist[0] )
host = configlist[0]
# step 0: Check there is a backup folder
host_backup_path = BACKUP_FOLDER + host
if not os.path.isdir(host_backup_path):
verbose("Creating backup folder " + host_backup_path)
os.makedirs(host_backup_path)
# Step 1: remove the oldest folder
if os.path.isdir(host_backup_path + "." + str(ITERATIONS)):
verbose("Deleting oldest version of backup directories")
shutil.rmtree(host_backup_path + "." + str(ITERATIONS))
# step 2: move the other folders up
for i in range((ITERATIONS - 1),0,-1):
debug("Checking if " + str(i) + "th folder exists")
if os.path.isdir(host_backup_path + "." + str(i)):
verbose("Moving " + host_backup_path + " from " + str(i) + " to " + str(i + 1))
shutil.move(host_backup_path + "." + str(i),host_backup_path + "." + str(i + 1))
# step 3: cp -al the current folder
# shutil.copytree(host_backup_path, host_backup_path + ".1", copy_function=os.link)
verbose("Copying main folder with hard links")
os.system("cp -al " + host_backup_path + " " + host_backup_path + ".1")
# step 4: sync the current folder
verbose("Synchronizing folders")
for folder in pathlist:
folder = folder.rstrip()
verbose("-> " + folder)
if not os.path.isdir(host_backup_path + folder):
os.makedirs(host_backup_path + folder)
verbose_rsync = "v" if VERBOSE else ""
os.system("rsync -a" + verbose_rsync + " --delete " + SCP_USER + host + ":" + folder + " " + host_backup_path + folder)