-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup
More file actions
executable file
·404 lines (332 loc) · 12.8 KB
/
backup
File metadata and controls
executable file
·404 lines (332 loc) · 12.8 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/python3
"""
Copyright 2016 Nick Apperley
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import subprocess
import sys
import os
import datetime
import shutil
import json
import logging
import tempfile
import setproctitle as proc_title
from signal import signal, SIGTERM
"""
Does backups for a Linux computer. Script requires Python 3, and needs to run
as root. The following Python packages need to be installed:
* setproctitle
"""
conf_file = '/etc/linux_backup_conf.json'
conf = None
rsync_process = None
test_mode = False
def _rotate_log():
"""
Rotates the log file when it reaches a certain size by copying its
contents to a new file.
"""
# Maximum size of the log file in KB.
max_size = 50000
filename = os.path.basename(conf['log-path'])
old_log_path = '{}/{}.old'.format(os.path.dirname(conf['log-path']),
filename[:filename.rindex('.')])
if os.path.exists(conf['log-path']) and os.path.isfile(conf['log-path']):
# Size of the log file in KB.
log_size = os.path.getsize(conf['log-path']) / 1000
if log_size >= max_size:
# Move log file.
shutil.move(conf['log-path'], old_log_path)
def _setup_logging():
# Use the following date format: year-month-day 24hour:minute.
# Example date: 2004-02-28 14:45:10.
date_format = '%Y-%m-%d %H:%M:%S'
# Use the following text format: [date] (log level) - log message.
# Example text: [2004-02-28 14:45:10] (INFO) - Backup finished.
txt_format = '[%(asctime)s] (%(levelname)s) - %(message)s'
# Use the main logger for the module.
logger = logging.getLogger(__name__)
handler = None
formatter = logging.Formatter(txt_format, date_format)
try:
handler = logging.FileHandler(conf['log-path'])
except IOError:
print(conf['messages']['error']['log-setup'].format(conf['log-path']))
exit(-1)
_rotate_log()
handler.setFormatter(formatter)
# Ensure all info level messages are logged.
handler.setLevel(logging.INFO)
# Have all log messages saved to the log file.
logger.addHandler(handler)
def _program_arg(arg_flag):
"""
Retrieves the program argument.
:param arg_flag: Argument flag representing the argument to retrieve.
:return: The argument if found, otherwise an empty string is returned.
"""
result = ''
pos = -1
# Go through the program arguments.
for arg in sys.argv:
pos += 1
if arg == arg_flag:
if (pos + 1) < len(sys.argv):
result = sys.argv[pos + 1]
break
return result
def _rsync_args(src, dest, exclude_file_path=''):
# The following rsync args are used:
# -a Backup sym links and other file types.
# -p Preserve permissions on files and directories.
# -A Preserve ACL's (Access Control Lists).
# -O Ignore setting the modification time on directories
# and files to avoid errors occuring (with Ext4).
# -r Recursively backup directories.
# -v Output any info/errors that occur.
# -b Do a backup
# --backup-dir Specify the name of the backup directory
# --delete Deletes files and directories at the destination, which
# don't exist on the source side.
# --exclude-from Path to text file containing a list of files and
# directories to exclude for backup.
args = ['rsync', '-apAOrvb', '--backup-dir={}'.format(
datetime.date.today()), '--delete', src, dest]
if exclude_file_path is not '':
args.append('--exclude-from')
args.append(exclude_file_path)
return args
def do_backup(src, dest, exclude_file_path=''):
"""
Uses rsync to backup files/directories from the source path to the
destination path.
:param src: Path to an existing source directory.
:param dest: Path to an existing destination directory.
:param exclude_file_path: Path to exclude file containing a list of
directories to exclude from the backup.
"""
global rsync_process
out_type = subprocess.PIPE
start_msg = conf['messages']['info']['backup-started'].format(src, dest)
# Open a process to be run later. Set output to the console.
rsync_process = subprocess.Popen(_rsync_args(
src, dest, exclude_file_path), stdout=out_type)
print(start_msg)
logging.info(start_msg)
# Run the process (rsync) and capture all terminal output.
output = rsync_process.communicate()[0]
logging.info(output)
print(conf['messages']['info']['backup-finished'])
logging.info(conf['messages']['info']['backup-finished'])
def _check_backups_conf_arg(backups_conf_arg):
# Use the main logger for the module.
logger = logging.getLogger(__name__)
valid = True
if not os.path.exists(backups_conf_arg):
print(conf['messages']['error']['backups-conf-not-found'])
logger.error(conf['messages']['error']['backups-conf-not-found'])
valid = False
elif not os.path.isfile(backups_conf_arg):
print(conf['messages']['error']['backups-conf-not-file'])
logger.error(conf['messages']['error']['backups-conf-not-file'])
valid = False
return valid
def _check_src_arg(src_arg):
# Use the main logger for the module.
logger = logging.getLogger(__name__)
valid = True
if not os.path.exists(src_arg):
print(conf['messages']['error']['src-dir-not-found'])
logger.error(conf['messages']['error']['src-dir-not-found'])
valid = False
elif not os.path.isdir(src_arg):
print(conf['messages']['error']['src-dir-not-dir'])
logger.error(conf['messages']['error']['src-dir-not-dir'])
valid = False
return valid
def _check_dest_arg(dest_arg):
# Use the main logger for the module.
logger = logging.getLogger(__name__)
valid = True
if not os.path.exists(dest_arg):
print(conf['messages']['error']['dest-dir-not-found'])
logger.error(conf['messages']['error']['dest-dir-not-found'])
valid = False
elif not os.path.isdir(dest_arg):
print(conf['messages']['error']['dest-dir-not-dir'])
logger.error(conf['messages']['error']['dest-dir-not-dir'])
valid = False
return valid
def _validate_args():
valid = True
src_arg = _program_arg(conf['prog-flags']['src'])
dest_arg = _program_arg(conf['prog-flags']['dest'])
backups_conf_arg = _program_arg(conf['prog-flags']['backups-conf'])
usage_msg = """
Usage:
(1) backup -s src_dir_path -d dest_dir_path
(2) backup -c backups_conf_file_path
"""
if len(sys.argv) == 1:
print(usage_msg)
valid = False
elif src_arg is not '' and dest_arg is not '':
if _check_src_arg(src_arg) is False or \
_check_dest_arg(dest_arg) is False:
valid = False
elif backups_conf_arg is not '':
if _check_backups_conf_arg(backups_conf_arg) is False:
valid = False
else:
print(usage_msg)
valid = False
if not valid:
exit(-1)
def _parse_date(date_val):
"""
Parses a string and returns a Date object.
:param date_val: The string representing the date.
:return: A Date object if date_val is valid, otherwise None is returned.
"""
result = None
# Use the following date format: year-month-day.
# Example date: 2004-02-28.
date_format = '%Y-%m-%d'
try:
result = datetime.datetime.strptime(date_val,
date_format).date()
except ValueError:
pass
return result
def backup_dates(backup_dir):
"""
Retrieves a list containing dates of all backups that exist in the
specified backup directory.
:param backup_dir: Path to the backup directory.
:return: A list containing Date objects (backup dates) sorted from oldest
to newest, otherwise an empty list is returned.
"""
dates = []
if os.path.exists(backup_dir) and os.path.isdir(backup_dir):
for file_item in os.listdir(backup_dir):
if os.path.isdir('{}/{}'.format(backup_dir, file_item)):
tmp_date = _parse_date(file_item)
if tmp_date is not None:
dates.append(tmp_date)
return sorted(dates)
def remove_stale_backups(backup_dir):
"""
Removes unneeded backups.
:param backup_dir: Path to the backup directory.
"""
# Use the main logger for the module.
logger = logging.getLogger(__name__)
pos = 0
start_msg = conf['messages']['info']['del-backups-started'].format(
backup_dir)
finish_msg = conf['messages']['info']['del-backups-finished'].format(
backup_dir)
print(start_msg)
logger.info(start_msg)
for item in sorted(backup_dates(backup_dir), reverse=True):
pos += 1
if pos > conf['backups']['max']:
# Recursively remove the backup (directory).
shutil.rmtree('{}/{}'.format(backup_dir, item), ignore_errors=True)
print(finish_msg)
logger.info(finish_msg)
def _read_backups_conf(backups_conf_path):
"""
Reads values from a backups configuration file and returns the results.
:param backups_conf_path: Path to the backups configuration file (a
JSON file).
:return: A list representing a configuration file (refer to Python's
json module documentation), otherwise an empty list is returned if the
configuration file cannot be read.
"""
# Use the main logger for the module.
logger = logging.getLogger(__name__)
result = []
error_msg = conf['messages']['error']['invalid-backups-conf'].format(
backups_conf_path)
with open(backups_conf_path, 'r') as bcf:
try:
result = json.load(bcf)['backups']
except ValueError:
print(error_msg)
logger.error(error_msg)
return result
def _create_exclude_file(dirs_to_exclude):
"""
Creates a temporary file containing a list of directories to
exclude from a backup when running rsync.
:param dirs_to_exclude: A list of directories to exclude.
:return: A temporary File object pointing to the temporary exclude file.
"""
exclude_file = tempfile.NamedTemporaryFile(mode='w', prefix='backup_',
dir='/tmp', delete=False)
with exclude_file:
for a_dir in dirs_to_exclude:
exclude_file.write('{}\n'.format(a_dir))
return exclude_file
def create_multiple_backups(backups_conf):
"""
Creates multiple backups as defined in the backups configuration.
:param backups_conf: A list containing the configuration.
"""
for conf_item in backups_conf:
if 'exclude-dirs' in conf_item:
exclude_file = _create_exclude_file(conf_item['exclude-dirs'])
do_backup(conf_item['src-dir'], conf_item['dest-dir'],
exclude_file.name)
# Delete the exclude file.
os.remove(exclude_file.name)
else:
do_backup(conf_item['src-dir'], conf_item['dest-dir'])
def _main():
proc_title.setproctitle(conf['proc-title'])
# Setup a handler for the kill signal.
signal(SIGTERM, lambda signum, frame: _cleanup())
try:
_setup_logging()
_validate_args()
backups_conf_file_path = _program_arg(
conf['prog-flags']['backups-conf'])
if backups_conf_file_path is not '':
backups_conf = _read_backups_conf(backups_conf_file_path)
if len(conf) == 0:
exit(-1)
for item in backups_conf:
remove_stale_backups(item['dest-dir'])
create_multiple_backups(backups_conf)
else:
remove_stale_backups(_program_arg(conf['prog-flags']['dest']))
do_backup(_program_arg(conf['prog-flags']['src']),
_program_arg(conf['prog-flags']['dest']))
except KeyboardInterrupt:
_cleanup()
def _cleanup():
print(conf['messages']['info']['exit'])
if rsync_process is not None:
rsync_process.kill()
exit()
if __name__ == '__main__':
with open(conf_file, 'r') as pcf:
try:
conf = json.load(pcf)
except ValueError:
print("Cannot load the program's configuration file."
'Please fix the file and run backup again.')
exit(-1)
if test_mode:
conf['log-path'] = '/tmp/backup.log'
_main()