-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathrsync_backup.py
executable file
·99 lines (82 loc) · 3.23 KB
/
rsync_backup.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python2
# -*- coding: utf8 -*-
"""
rsync_backup.py: Make a backup via Rsync. Intended to be used periodically
in a cron job to maintain a local backup of a path in an another
localization (e.g. another drive) and to take advantage of the rsync's
speed and minimal footprint. It's ideal for those cases in which a RAID 1,
a regular backup app or a DCVS may not be the best option to maintain a
mirrored copy.
"""
__author__ = "joe di castro <joe@joedicastro.com>"
__license__ = "GNU General Public License version 3"
__date__ = "2014/06/01"
__version__ = "0.2"
try:
import sys
import os
from argparse import ArgumentParser
from subprocess import Popen, PIPE
from logger import Logger
from notify import notify
except ImportError:
# Checks the installation of the necessary python modules
print((os.linesep * 2).join(["An error found importing one module:",
str(sys.exc_info()[1]), "You need to install it", "Stopping..."]))
sys.exit(-2)
def arguments():
"""Defines the command line arguments for the script."""
main_desc = """Make a backup via rsync"""
parser = ArgumentParser(description=main_desc)
parser.add_argument("source", help="the source path")
parser.add_argument("backup", help="the backup path")
parser.add_argument("-n", dest="notify", action="store_true",
help="show popup notifications")
parser.add_argument("-s", dest="send", action="store_true",
help="send a log via mail to the current local user")
parser.add_argument("-e", dest="exclude",
help="a comma separated list of directories to exclude")
parser.add_argument("-v", "--version", action="version",
version="%(prog)s {0}".format(__version__),
help="show program's version number and exit")
return parser
def main():
"""Main section"""
args = arguments().parse_args()
options = "-azxhvP --delete --force --ignore-errors --stats"
command = "rsync {2} {3} {0} {1}/".format(
args.source,
args.backup,
options,
" ".join("--exclude={0}".format(i) for i in args.exclude.split(","))
)
print command
log = Logger()
url = "http://joedicastro.com"
head = "Backup of {0} into {1}".format(args.source, args.backup)
log.header(url, head)
log.time('Start Backup')
if args.notify:
notify('Rsync Backup', 'Starting backup of {0} into {1}'.
format(args.source, args.backup), 'info')
rsync = Popen(command.split(), stdout=PIPE, stderr=PIPE)
out, errors = rsync.stdout.read(), rsync.stderr.read()
log.list('Rsync output', out)
if errors:
log.list('Errors', errors)
log.time('End Backup')
if args.notify:
notify('Rsync Backup', 'Backup ended', 'ok')
if args.send:
log.send('Rsync backup')
log.write(True)
if __name__ == "__main__":
main()
###############################################################################
# Changelog #
###############################################################################
#
# 0.1:
#
# * First attempt
#