PageRenderTime 39ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/silverlining/mgr-scripts/transfer-backup.py

https://bitbucket.org/ianb/silverlining/
Python | 60 lines | 56 code | 3 blank | 1 comment | 8 complexity | d43fd9cc664dce532ee25a8f5c4507f4 MD5 | raw file
Possible License(s): GPL-2.0
  1. #!/usr/bin/env python
  2. import sys
  3. sys.path.insert(0, '/usr/local/share/silverlining/lib')
  4. import os
  5. import optparse
  6. import shutil
  7. from silversupport import transfermethods
  8. parser = optparse.OptionParser(
  9. usage='%prog DIR DEST')
  10. parser.add_option(
  11. '--remove', action='store_true',
  12. help="Remove the DIR after successful transfer")
  13. parser.add_option(
  14. '--archive', action='store_true',
  15. help="Create an archive and print the result, instead of transfering")
  16. def main():
  17. options, args = parser.parse_args()
  18. dir = args[0]
  19. dest = args[1]
  20. if options.archive:
  21. run_archive(dir, dest)
  22. return
  23. if dest.startswith('remote:'):
  24. dest = dest[len('remote:'):]
  25. dest = os.path.join('/var/lib/silverlining/backups', dest)
  26. transfermethods.local(dir, dest)
  27. elif dest.startswith('site:'):
  28. raise NotImplementedError('site: has not yet been implemented')
  29. elif dest.startswith('ssh:'):
  30. dest = dest[len('ssh:'):].lstrip('/')
  31. if '/' in dest:
  32. hostname, path = dest.split('/', 1)
  33. if path.lower().startswith('cwd/'):
  34. path = path[4:]
  35. else:
  36. path = '/' + path
  37. else:
  38. hostname = dest
  39. path = ''
  40. transfermethods.ssh(dir, '%s:%s' % (hostname, path))
  41. elif dest.startswith('rsync:'):
  42. dest = dest[len('rsync:'):]
  43. transfermethods.rsync(dir, dest)
  44. else:
  45. print 'Unknown destination type: %s' % dest
  46. sys.exit(1)
  47. if options.remove:
  48. shutil.rmtree(dir)
  49. def run_archive(dir, ext):
  50. tmp = transfermethods.make_temp_name('file'+ext)
  51. transfermethods.archive(dir, tmp)
  52. print 'archive="%s"' % tmp
  53. if __name__ == '__main__':
  54. main()