PageRenderTime 92ms CodeModel.GetById 51ms app.highlight 6ms RepoModel.GetById 34ms app.codeStats 0ms

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

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