-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparse-backup.py
More file actions
executable file
·136 lines (91 loc) · 3.56 KB
/
sparse-backup.py
File metadata and controls
executable file
·136 lines (91 loc) · 3.56 KB
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
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/python
import sys
import os
import time
import subprocess
import ConfigParser
VERBOSE=True
Config = ConfigParser.SafeConfigParser()
Config.read('run-backup.conf')
try:
backup_sources = dict(Config.items('sources'))
backup_targets = dict(Config.items('targets'))
backup_actions = dict(Config.items('actions'))
except ConfigParser.Error:
print "Config file is incomplete!"
# Get a list of all mounted images
def get_mounted_images():
# Use hdiutil to find out mounted images
output = subprocess.check_output("hdiutil info", shell=True)
# Check each one against our own image filenames
# TODO: Probably don't need to check here, just list..
mounted_images = []
for line in output.splitlines():
if line.startswith("image-path"):
for name in backup_sources:
if line.endswith(backup_sources[name]):
mounted_images.append(name)
return mounted_images
# Check if image is mounted
def image_mounted(name):
# Check if this image name appears in Mounted Images
if name in get_mounted_images():
return True
else:
return False
def backup_running(_remove=False):
if _remove is True:
# Remove the file
os.remove('/tmp/sparse-backup.lock')
return False
# Check that the lockfile does not exist
if os.path.isfile('/tmp/sparse-backup.lock'):
return True
else:
open('/tmp/sparse-backup.lock', 'w').close()
return False
# Main routine, to perform the backups
def main():
for source in backup_actions:
destinations = backup_actions[source].split(',')
for dest in destinations:
print "\033[1;37m" + source + " -> " + dest + "\033[0m"
if image_mounted(source):
print "\033[91m [FAIL]\n\n Image is currently mounted\033[0m\n"
continue
# Check the source is not mounted
try:
cmd = backup_sources[source] + ' ' + backup_targets[dest]
except KeyError:
print "Backup source or destination does not exist"
continue;
_start_time = time.time()
_p = subprocess.Popen(('rsync', '-avz', '--delete', backup_sources[source], backup_targets[dest]), stdout=subprocess.PIPE,stderr=subprocess.PIPE)
(stdout, stderr) = _p.communicate()
_end_time = time.time()
_runtime = _end_time-_start_time
if _p.returncode is not 0:
# An error occurred
print "\033[1;31m [FAIL]\n"
#lines = stderr.splitlines()
for line in stderr.splitlines():
print " " + line
else:
if _p.returncode is 0 and len(stderr) > 0:
# A warning occurred
#print " \033[93m[Warning]\n"
lines = stderr.splitlines()
for line in lines:
print "\033[1;33m Warning: " + line
print " \033[1;32m[Done]\033[0;32m in " + str(round(_runtime, 2)) + " seconds"
if VERBOSE is True:
print "\033[0;33m"
for line in stdout.splitlines():
print " Output: " + line
print "\033[0m"
if __name__ == "__main__":
if backup_running():
print "Backup already running (lockfile exists), exiting.."
sys.exit(10)
main()
backup_running(_remove=True)