A simple backup script
#backup.py
#
#backupdirs - a list of complete paths of directories to backup
#excludes - a list of subdirectories to be excluded
#backupdir - the path to the directory where the backup should be written
#machinename - the name of the machine
backupdirs = []
excludes = []
backupdir = "."
machinename = "the_box"
from zipfile import ZipFile
import time
try:
from itertools import chain
except:
def chain(*iterators):
for i in iterators:
for j in i:
yield j
def getAllFilesInPath(path):
from os import listdir
from os.path import isdir, join
dirs = chain(*[getAllFilesInPath(join(path, filename))
for filename in listdir(path)
if isdir(join(path, filename)) and join(path, filename) not in excludes])
files = [join(path,filename)
for filename in listdir(path)
if not isdir(join(path, filename))]
return chain(files, dirs)
def backup():
from os.path import join
zf = ZipFile(time.strftime(join(backupdir,machinename+"%-d-%m-%Y.zip")),"w")
for backupdir in backupdirs:
for filename in getAllFilesInPath(backupdir):
zf.write(filename)
zf.close()
if __name__ == "__main__":
backup()
2 Comments:
Maybe you would like to take a look at the ASPN recipe http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/191017
which provides a simple recipe for backing up files in a directory with versioning.
Sadly this script never got used we ended up using something in perl instead.
One interesting thing my script does is that it lets you can set multiple backup directories and even exclude some subdirectories.
There is a later version of my script that would ftp the zipped file to a common repository. There was even one that did an incremental backup. No idea where that one is anymore.
Post a Comment
<< Home