Source

rsync_backup / rsync_backup.py

Diff from to

File rsync_backup.py

 from os.path import exists, abspath
 import sys
 import errno
-from subprocess import call as cmd
+import subprocess
 import shutil
 
 RSYNC = "rsync -avH --numeric-ids --link-dest=%(link_dst)s %(src)s/ %(dst)s/"
 
 
+cmd = subprocess.check_call
+
+
 def join(path, n, token):
     if n == 0:
         # there is only ONE "0" directory, no token appended
     return os.path.join(path, "%d%s" % (n, token))
 
 
-def rsync(src, dst, link_dst):
+def rsync(src, dst, link_dst, **kw):
     """backup contents of src directory into dst directory"""
-    return cmd(RSYNC % dict(src=abspath(src),
-                            dst=abspath(dst),
-                            link_dst=abspath(link_dst)),
-               shell=True)
+    cmd(RSYNC % dict(src=abspath(src),
+                     dst=abspath(dst),
+                     link_dst=abspath(link_dst)),
+        shell=True, **kw)
 
 
 def dirshift(path, n, token):
         shutil.rmtree(del_dir)
 
 
-def backup(src, dst, n, token=""):
+def backup(src, dst, n, token="", **kw):
     """do a backup from src to dst, shifting dirs first"""
     dirshift(dst, n, token)
-    return rsync(src, join(dst, 0, token), join(dst, 1, token))
+    rsync(src, join(dst, 0, token), join(dst, 1, token), **kw)
 
 
 if __name__ == "__main__":
         print "rsb N token src dst"
         sys.exit(1)
     n, token, src, dst = sys.argv[1:]
-    rc = backup(src, dst, int(n), token)
+    try:
+        backup(src, dst, int(n), token)
+    except subprocess.CalledProcessError as err:
+        rc = err.returncode
+    else:
+        rc = 0
     sys.exit(rc)