From 0376026de09733315a9ffddc98b2f6d97919f52b Mon Sep 17 00:00:00 2001 From: "Adrian C. (anrxc)" Date: Sun, 30 May 2010 22:48:40 +0200 Subject: ryrestore: imported first version Backup restore script that can be used with rybackup for quick and easy restoration of specific files or directories. --- ryrestore.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100755 ryrestore.py 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. + +# 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() -- cgit v1.2.3