summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian C. (anrxc) <anrxc@sysphere.org>2010-05-30 20:48:40 (GMT)
committerAdrian C. (anrxc) <anrxc@sysphere.org>2010-05-30 20:49:42 (GMT)
commit0376026de09733315a9ffddc98b2f6d97919f52b (patch)
tree1d567e55fa0ed8e023b594707cdc2682b06220f2
parent7da0e86dd003e6d01771e0422a39d3bb0490437f (diff)
downloadrybackup-0376026de09733315a9ffddc98b2f6d97919f52b.tar.xz
ryrestore: imported first version
Backup restore script that can be used with rybackup for quick and easy restoration of specific files or directories.
-rwxr-xr-xryrestore.py75
1 files changed, 75 insertions, 0 deletions
diff --git a/ryrestore.py b/ryrestore.py
new file mode 100755
index 0000000..1404a38
--- /dev/null
+++ b/ryrestore.py
@@ -0,0 +1,75 @@
+#!/usr/bin/env python
+
+# ryrestore -- restore script for rybackup, rsync based backup script
+# with rotating backup-snapshots
+# Copyright (C) 2010 Adrian C. <anrxc sysphere.org>
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+
+# restore
+# % ~/.backup/ryrestore.py /home/user/.zshrc # restore a file
+# % ~/.backup/ryrestore.py /home/user/mail/ # restore a directory
+
+
+from sys import argv
+from subprocess import call
+from datetime import datetime
+from os import getuid, path, listdir
+from rybackup import bak, bin, mount
+
+
+# Functions
+#
+# Managing backup snapshots
+def select(source):
+ try:
+ snapshots = listdir(source)
+ except OSError:
+ raise SystemExit("Error: unable to read backup source")
+
+ # Present the selection to the user, with numbered IDs
+ for index, directory in enumerate(snapshots):
+ mtime = datetime.fromtimestamp(path.getmtime(path.join(source, snapshots[index])))
+ print "%s: %s [%s]" % (index, directory, mtime)
+
+ # Get ID of the backup snapshot from which to restore
+ return snapshots[int(raw_input("\nSnapshot: "))]
+
+
+# Rsync backup restoration
+def restore(source, destination):
+ try:
+ bsnap = select(source)
+ except (ValueError, IndexError):
+ raise SystemExit("Error: incorrect snapshot ID")
+
+ # Mirroring options
+ rsyncopt = (
+ "--archive", # use the archive mode,
+ "--verbose", # increase verbosity,
+ )
+
+ # Restore from backup with rsync
+ call("%s %s %s %s" % (bin['rsync'], " ".join(rsyncopt),
+ path.join(source, bsnap, destination[1:]),
+ destination), shell=True)
+
+
+# Backup restore procedure
+def main():
+ usage = "Usage: %s {file|directory}" % path.split(argv[0])[1]
+
+ if getuid() != 0:
+ raise SystemExit("Error: super user privileges required")
+ try:
+ mount("mount")
+ restore(bak['dst'], argv[1])
+ mount("umount")
+ except IndexError:
+ raise SystemExit(usage)
+
+if __name__ == "__main__":
+ main()