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()