summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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()