-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbu
More file actions
executable file
·71 lines (55 loc) · 2.02 KB
/
bu
File metadata and controls
executable file
·71 lines (55 loc) · 2.02 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
#!/usr/bin/env python3
# quick and dirty backup versions of the given file to a subdirectory named .bu
# revisions are copied to <filename>;<version_number> (retro throwback to vms)
import sys
import fnmatch
import os
import re
import shlex
DEBUG=False
def getHighestVersion(files):
pattern = re.compile(r"^.+;(\d+)$")
highestVersion = -1
for filename in files:
version = int(pattern.match(filename).group(1))
if DEBUG:
print("%s %d" % (filename, version))
if (version > highestVersion):
highestVersion = version
return highestVersion
def cmd(s):
if DEBUG:
print("cmd: %s" % (s))
os.system(s)
if __name__=='__main__':
filepath = sys.argv[1]
dirname = os.path.dirname(filepath)
if (dirname==""):
dirname="."
backup_dirname = os.path.join(dirname,".bu")
filename = os.path.basename(filepath)
if (len(filename)==0):
print("Cannot backup directories.... yet?")
sys.exit(1)
if (not os.path.exists(backup_dirname)):
os.mkdir(backup_dirname)
directoryListing = os.listdir(backup_dirname)
if DEBUG:
print("os.listdir(%s)=%s" % (backup_dirname, directoryListing))
if (len(directoryListing)==0):
version=0
if DEBUG:
print("no prior backups")
else:
versionFilenames = fnmatch.filter(directoryListing, "%s;*" % filename)
if DEBUG:
print("prior backups:%s" % versionFilenames)
version = getHighestVersion(versionFilenames)+1
backup_filepath = os.path.join(backup_dirname,filename)
cmd("cp %s %s\;%d" % (shlex.quote(filepath), shlex.quote(backup_filepath), version))
cmd("chmod a-w %s\;%d" % (shlex.quote(backup_filepath), version))
backupFile = "%s;%d" % (backup_filepath, version)
if (os.path.exists(backupFile)):
print("%s backed up to %s" % (filepath, backupFile))
else:
print("*** Error copying file. check existance of backup, change script variable Debug to True and try again")