[NCLUG] Server-to-Server backups
dann frazier
dannf at dannf.org
Wed Jun 28 09:38:23 MDT 2006
I've been tweaking this script to do my rsync backups for a while.
It uses the --linkdest function of rsync to automatically hardlink
files back to the last backup if they aren't any different.
I haven't looked at any of the existing rsync backup scripts, I've
heard that rsync-diff style methods are better.
--
dann frazier
-------------- next part --------------
#!/usr/bin/python
import os, sys
import optparse, datetime
import os.path, re
Version = "1.0.1"
if __name__ == '__main__':
parser = optparse.OptionParser()
parser.add_option("-d", "--directory", dest="dir",
help="Local directory for storing backups",
metavar="DIRECTORY")
parser.add_option("--host", dest="host",
help="hostname of machine to backup",
metavar="HOST")
parser.add_option("-n", "--name", dest="name",
help="name to use for this backup, defaults to HOST",
metavar="NAME")
parser.add_option("-e", "--exclude-file", dest="excludefile",
help="rsync exclude file",
metavar="EXCLUDEFILE")
parser.add_option("--version", action="store_true", dest="version",
default=False, help="print the version and exit")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
default=False, help="Pass the verbose option to rsync")
(option, args) = parser.parse_args()
if option.version:
sys.stdout.write("rsync-backup version "+Version+".\n")
sys.exit(0)
if not option.dir or not option.host or not option.excludefile:
sys.stderr.write("Error: --directory, --host, and --exclude-file are required.\n")
parser.print_help()
sys.exit(1)
if not os.path.isdir(option.dir):
sys.stderr.write("Error: " + option.dir + " does not exist or is not a directory.\n")
sys.exit(1)
if not os.path.isfile(option.excludefile):
sys.stderr.write("Error: " + option.excludefile + " does not exist or is not a regular file.\n")
sys.exit(1)
## look for a directory to hard link identical files to
seedRegEx = re.compile("^" + option.name + "-\d\d\d\d-\d\d-\d\d$")
possibleSeeds = []
for file in os.listdir(option.dir):
m = seedRegEx.match(file)
if m and os.path.isdir(os.path.join(option.dir, file)):
possibleSeeds.append(file)
if possibleSeeds:
possibleSeeds.sort()
newestSeed = possibleSeeds[-1]
linkdest = "--link-dest " + os.path.join(option.dir, newestSeed) + " "
else:
linkdest = ""
date = datetime.date.today().isoformat()
if option.verbose:
verbose = "v"
else:
verbose = ""
rsyncCmd = "rsync -a" + verbose + "zSH --numeric-ids --partial -e ssh --delete --delete-excluded --exclude-from " + option.excludefile + " root@" + option.host + ":/ " + linkdest + os.path.join(option.dir, option.name + "-" + date)
sys.stdout.write("Running " + rsyncCmd + "...\n")
os.system(rsyncCmd)
More information about the NCLUG
mailing list