-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackup.py
More file actions
executable file
·177 lines (129 loc) · 4.93 KB
/
backup.py
File metadata and controls
executable file
·177 lines (129 loc) · 4.93 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python3
import os
import sys
import datetime
# =============================================================================
# Create archive
# =============================================================================
def makeExcludeDir(to_exclude):
exclude_str = ""
for i in to_exclude:
exclude_str += "--exclude='{}' ".format(i)
return exclude_str.strip()
def addFile(directory, target_file_name, file_to_add, to_exclude):
cmd = "tar -r"
if len(to_exclude) > 0:
cmd += " {}".format(to_exclude)
cmd += " --file={} {}".format(target_file_name, file_to_add)
os.chdir(directory)
os.system(cmd)
# Compress to the backup file
def Compress(bu_files, target_file_name):
# Get list of files and directories to compress. List will have tuples
# like:
# (directory, filename)
to_compress, to_exclude = getFilesToCompress(bu_files)
to_exclude_str = makeExcludeDir(to_exclude)
# Add files in to_compress
for i in to_compress:
file_path = os.path.join(i[0], i[1])
if os.path.exists(file_path):
print('Adding file "{}"'.format(file_path))
addFile(i[0], target_file_name, i[1], to_exclude_str)
else:
print('File "{}" does not exist. Skipping.'.format(file_path))
# Get files and directories from the file bu_files
def getFilesToCompress(bu_files):
bu_files_fd = open(bu_files)
list_of_files_to_add = []
list_of_files_to_exclude = []
for i in bu_files_fd:
line = i.strip()
if line == "":
continue
if line[-1] == "/":
line = line[:-1]
if line[0] != "/":
line = os.path.expanduser(line)
line = os.path.join(os.getcwd(), line)
dirname, basename = (os.path.dirname(line), os.path.basename(line))
# Check name collision
for j in list_of_files_to_add:
if j[1] == basename:
print('Multiple files with name "{}"'.format(j[1]))
exit(1)
# Append
if basename[0] != "-":
list_of_files_to_add.append((dirname, basename))
else:
basename_to_exclude = basename[1:]
print("Will exclude: {}".format(os.path.join(dirname, basename_to_exclude)))
list_of_files_to_exclude.append(basename_to_exclude)
return list_of_files_to_add, list_of_files_to_exclude
# =============================================================================
# Script Arguments
# =============================================================================
def printUsage():
print("Usage:")
print("\t-h - Display help")
print("\t-b bu_file - bu_file location. Defaults to the script's directory")
print(
"\t-o target_file - The resulting tar file. Default to the script's directory"
)
def getDefaultFileName():
today = datetime.date.today()
date_str = str(today.year) + "_" + str(today.month) + "_" + str(today.day)
return os.path.join(os.getcwd(), "backup_" + date_str + ".tar")
def parseArgs(argv):
bu_files = os.path.join(os.getcwd(), "bu_files")
target_file_name = getDefaultFileName()
# Get file names from arguments
i = 0
while i < len(argv):
if argv[i] == "-b":
try:
bu_files = argv[i + 1]
except IndexError:
print("Flag -b needs argument")
print("")
printUsage()
exit(1)
i += 2
elif argv[i] == "-o":
try:
target_file_name = argv[i + 1]
# Make path absolute, if it is not
if not os.path.isabs(target_file_name):
target_file_name = os.path.join(os.getcwd(), target_file_name)
# Add tar extension, if it's not there
if os.path.splitext(target_file_name)[1] != ".tar":
target_file_name += ".tar"
except IndexError:
print("Flag -o needs argument")
printUsage()
print("")
exit(1)
i += 2
else:
print("Argument {} not recognized".format(argv[i]))
print("")
printUsage()
exit(1)
return bu_files, target_file_name
# =============================================================================
# Main
# =============================================================================
if __name__ == "__main__":
# Take arguments
# If there is a help flag, just display the help and quit.
if "-h" in sys.argv:
printUsage()
exit(1)
bu_files, target_file_name = parseArgs(sys.argv[1:])
if not os.path.isfile(bu_files):
print("File {} doesn't exist".format(bu_files))
exit(1)
if os.path.isfile(target_file_name):
print("File {} already exists".format(target_file_name))
exit(1)
Compress(bu_files, target_file_name)