2
"""Backup directories using rsync. Quick and dirty.
4
backup.py config_file final_actions
7
#----------------------------------------------------------------------------
8
# configure in this section
10
# all dirs relative to current dir.
12
# output directory for backups
15
# list directories to backup as a dict with 1 or 0 values for
16
# recursive (or not) descent:
19
# list exclude patterns here (space-separated):
20
# if the pattern ends with a / then it will only match a directory, not a
21
# file, link or device.
22
# see man rsync for more details on the exclude patterns
23
exc_pats = '#*# *~ *.pyc *.pyo *.o '
25
# global options for rsync
26
rsync_opts='-v -t -a --delete --delete-excluded'
28
#----------------------------------------------------------------------------
30
import os,string,re,sys
31
from IPython.genutils import *
32
from IPython.Itpl import itpl
34
# config file can redefine final actions
38
# load config from cmd line config file or default bkprc.py
45
print 'You need to provide a config file: bkp.py rcfile'
48
# make output dir if needed
49
outdir = os.path.expanduser(outdir)
52
except OSError: # raised if dir already exists -> no need to make it
55
# build rsync command and call:
56
exclude = re.sub(r'([^\s].*?)(\s|$)',r'--exclude "\1"\2',exc_pats)
57
rsync = itpl('rsync $rsync_opts $exclude')
59
# the same can also be done with lists (keep it for reference):
60
#exclude = string.join(['--exclude "'+p+'"' for p in qw(exc_pats)])
62
# rsync takes -r as a flag, not 0/1 so translate:
63
rec_flag = {0:'',1:'-r'}
65
# loop over user specified directories calling rsync
66
for bakdir,rec in to_backup.items():
67
bakdir = os.path.expanduser(bakdir)
68
xsys(itpl('$rsync $rec_flag[rec] $bakdir $outdir'),
69
debug=0,verbose=1,header='\n### ')