-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdobackup2.py
79 lines (63 loc) · 2.16 KB
/
dobackup2.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
import subprocess
import datetime
import os
import sys
DEBUG=False
BACKUPDEVICE="/dev/sdc1"
BACKUPMAPPER="encBackup"
BACKUPMOUNT="/mnt/backup"
#BACKUPMOUNT="/home/alex/backupdummy"
def getBackupName():
now = datetime.datetime.today()
return "%d%02d%02d_%02d%02d" % (now.year, now.month, now.day,
now.hour, now.minute)
def exec_cmd(cmd, DEBUGOVERRIDE=None):
if DEBUGOVERRIDE is None and DEBUG or DEBUGOVERRIDE == True:
print "DEBUG: ", cmd
else:
subprocess.check_call(cmd)
def doOpenDevice():
#print "Opening encrypted device..."
#exec_cmd(["cryptsetup", "luksOpen", BACKUPDEVICE, BACKUPMAPPER])
print "Mounting device"
exec_cmd(["mount", BACKUPMOUNT])
def doCloseDevice():
print "Unmounting device"
exec_cmd(["umount", BACKUPMOUNT])
#print "Closing encrypted device"
#exec_cmd(["cryptsetup", "luksClose", BACKUPMAPPER])
def doBackup(source, jobname): #, backupname):
print "Doing backup of %s" % source
backupstore = os.path.join(BACKUPMOUNT, jobname)
print "Examining existing backups in %s" % backupstore
#backups = getBackups(backupstore)
#backups.sort()
#if len(backups) > 0:
#print "Found: "
#for i in backups:
#print " %s" % i
#linkdest = os.path.join(backupstore, backups[-1])
#print "Using %s as basis for new backup" % linkdest
#else:
#print "No previous backup found. Will copy all files"
#linkdest = None
cmd = ["rsync", "-a", "-t", "-x", "--delete", "--stats"]
#if linkdest is not None:
#cmd.append("--link-dest=%s" % linkdest)
if not source.endswith(os.path.sep):
source += os.path.sep
cmd.append(source)
cmd.append(backupstore)
exec_cmd(cmd)
def getBackups(path):
return os.listdir(path)
if __name__=='__main__':
if not os.geteuid()==0:
sys.exit("\nOnly root can run this script\n")
#backupname = getBackupName()
#print "Determined backup name to: %s" % backupname
doOpenDevice()
doBackup("/home/alex/.xbmc", "xbmc") #, backupname)
#doBackup("/home/alex/.lircrc", "lircrc") #, backupname)
#doBackup("/", "root", backupname)
doCloseDevice()