bzr backup with rsnapshot
rss

Banged out a quick script for backing up a directory containing a bunch of bzr depots, for use with rsnapshot:

To use:

  1. Save to /usr/local/bin/backup_bzr.py
  2. chmod u+x /usr/local/bin/backup_bzr.py
  3. Edit /etc/rsnapshot.conf and add:
    1. backup_script   /usr/local/bin/backup_bzr.py . [root dir of your bzr dirs]  localhost/bzr

Update: Just realized that maybe this isn’t as useful as I thought. ;)   Seeing as how bzr is self-contained, you could probably just rsync the entire directory and get away with it that way.  D’oh.  So used to old-style VCS that need a separate backup process than ‘cp’. ;) I’ll leave this code here in case anyone else finds it useful.

Download: backup_bzr.py
  1. #!/usr/bin/python
  2. import os
  3. import subprocess
  4. import sys
  5.  
  6. class App(object):
  7. def Setup(self):
  8.  
  9. # TODO: Proper command line parsing
  10.  
  11. if len(sys.argv) < 2:
  12. print 'Invalid arguments.  Specify a destination directory, as well as root directory of bzr depots.'
  13. return -1
  14.  
  15. self.BackupDirectory    = sys.argv[1]
  16. self.RootDirectory        = sys.argv[2]
  17.  
  18. return 0;
  19.  
  20. def IsBzrDirectory(self, p):
  21. return os.path.exists(os.path.join(p, '.bzr'))
  22.  
  23. def RunCommand(self, c):
  24. print c
  25. p = subprocess.Popen(c, shell=True)
  26.  
  27. if p.wait() != 0:
  28. raise Exception('Failure running command.')
  29.  
  30. def BackupBzrDirectory(self, p):
  31. backupDirectory = os.path.join(self.BackupDirectory, os.path.split(p)[-1])
  32.  
  33. if not os.path.exists(backupDirectory):
  34. cmd = 'bzr init %s'%(backupDirectory)
  35.  
  36. self.RunCommand(cmd)
  37.  
  38. pwd = os.getcwd()
  39. os.chdir(backupDirectory)
  40. cmd = 'bzr pull %s'%(p)
  41. self.RunCommand(cmd)
  42. os.chdir(pwd)
  43. return 0
  44.  
  45. def Run(self):
  46. e = self.Setup()
  47.  
  48. if e != 0:
  49. return e
  50.  
  51. if self.RootDirectory != None:
  52. for dirName in os.listdir(self.RootDirectory):
  53. fqp = os.path.join(self.RootDirectory, dirName)
  54.  
  55. if self.IsBzrDirectory(fqp):
  56. e = self.BackupBzrDirectory(fqp)
  57.  
  58. if e != 0:
  59. return e
  60.  
  61. return 0
  62.  
  63. if '__main__' == __name__:
  64.  
  65. sys.exit(App().Run())
Share or Store:
  • Digg
  • del.icio.us

No Comments yet »

RSS feed for comments on this post. TrackBack URI

Leave a comment