~ellisonbg/ipython/bugfixes0411409

« back to all changes in this revision

Viewing changes to tools/bkp.py

  • Committer: ville
  • Date: 2008-02-16 09:50:47 UTC
  • mto: (0.12.1 ipython_main)
  • mto: This revision was merged to the branch mainline in revision 990.
  • Revision ID: ville@ville-pc-20080216095047-500x6dluki1iz40o
initialization (no svn history)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
"""Backup directories using rsync. Quick and dirty.
 
3
 
 
4
backup.py config_file final_actions
 
5
"""
 
6
 
 
7
#----------------------------------------------------------------------------
 
8
# configure in this section
 
9
 
 
10
# all dirs relative to current dir.
 
11
 
 
12
# output directory for backups
 
13
outdir = ''
 
14
 
 
15
# list directories to backup as a dict with 1 or 0 values for
 
16
# recursive (or not) descent:
 
17
to_backup = {}
 
18
 
 
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 '
 
24
 
 
25
# global options for rsync
 
26
rsync_opts='-v -t -a --delete --delete-excluded'
 
27
 
 
28
#----------------------------------------------------------------------------
 
29
# real code begins
 
30
import os,string,re,sys
 
31
from IPython.genutils import *
 
32
from IPython.Itpl import itpl
 
33
 
 
34
# config file can redefine final actions
 
35
def final():
 
36
    pass
 
37
 
 
38
# load config from cmd line config file or default bkprc.py
 
39
try:
 
40
    execfile(sys.argv[1])
 
41
except:
 
42
    try:
 
43
        execfile('bkprc.py')
 
44
    except IOError:
 
45
        print 'You need to provide a config file: bkp.py rcfile'
 
46
        sys.exit()
 
47
 
 
48
# make output dir if needed
 
49
outdir = os.path.expanduser(outdir)
 
50
try:
 
51
    os.makedirs(outdir)
 
52
except OSError:  # raised if dir already exists -> no need to make it
 
53
    pass
 
54
 
 
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')
 
58
 
 
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)])
 
61
 
 
62
# rsync takes -r as a flag, not 0/1 so translate:
 
63
rec_flag = {0:'',1:'-r'}
 
64
 
 
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### ')
 
70
 
 
71
# final actions?
 
72
final()