-
Notifications
You must be signed in to change notification settings - Fork 0
/
duplicity_restorer.py
74 lines (61 loc) · 2.72 KB
/
duplicity_restorer.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
from subprocess import call
import subprocess
import os
from multiprocessing import Pool
import duplicity_conf as conf
import datetime
def getDateObject(hostname, remote_path, options):
cmd = 'duplicity collection-status ' + options +' ' + hostname + '/' + remote_path
p = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
# print out.split('\n')
if err:
return datetime.datetime(1900,1,1)
if 'Chain end time: ' not in out:
return datetime.datetime(1900,1,1)
date_object = datetime.datetime.strptime(out.split('\n')[12].replace('Chain end time: ',''),'%a %b %d %H:%M:%S %Y')
return date_object
def execute_duplicity_command(host):
print 'Running: '
print host
p = subprocess.Popen(host.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if err:
print 'Error while running Duplicity command: '
print host
print err
return
print '---------------------------------------'
print 'Output from running Duplicity command: '
print host
print out
print '---------------------------------------'
class Duplicity_restorer:
def __init__(self, path):
self.path = path
def set_up_hosts(self):
self.hosts = []
#Set up any environment variables set in conf
for env_var in conf.env_vars:
os.environ[env_var[0]] = env_var[1]
#Format every duplicity command based on configuration variables
dateObjects = []
for i in xrange(0,len(conf.remote_hosts)):
dateObjects.append(getDateObject(conf.remote_hosts[i], conf.remote_paths[i]+'/distdup'+str(i), conf.remote_host_options[i]))
sorted_dateObjects = sorted(dateObjects, reverse=True)
newest_dateObject = sorted_dateObjects[0]
max_delta = datetime.timedelta(minutes=15)
for i in xrange(0,len(dateObjects)):
delta = newest_dateObject-dateObjects[i]
if max_delta > delta:
self.hosts.append('duplicity {options} {host}/{remote_path}/distdup{i} {restore_path}/distdup{i}'.format(restore_path=self.path, host=conf.remote_hosts[i], remote_path=conf.remote_paths[i], options=conf.remote_host_options[i], i = i))
else:
print 'Backup of host number ' + str(i) +' is not updated or unavailable. Not downloaded.'
print 'Corresponding command is: '
print 'duplicity {options} {host}/{remote_path}/distdup{i} {restore_path}/distdup{i}'.format(restore_path=self.path, host=conf.remote_hosts[i], remote_path=conf.remote_paths[i], options=conf.remote_host_options[i], i = i)
def run_restoration(self):
Pool().map(execute_duplicity_command, self.hosts)
def remove_env_var(self):
#Delete any environment variables set in conf
for env_var in conf.env_vars:
del os.environ[env_var[0]]