PageRenderTime 26ms CodeModel.GetById 16ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/silverlining/commands/backup.py

https://bitbucket.org/ianb/silverlining/
Python | 50 lines | 44 code | 5 blank | 1 comment | 8 complexity | 3a2f26c5c3166a32f9e7db8f9da214f1 MD5 | raw file
 1"""Cleans out unused instances from a server"""
 2import re
 3from cmdutils import CommandError
 4from silversupport.shell import ssh, run
 5from silversupport.appdata import normalize_location
 6from silversupport import transfermethods
 7
 8_backup_path_re = re.compile(r'backup="(.*?)"')
 9_scheme_re = re.compile(r'^[a-z]+:')
10_archive_path_re = re.compile(r'archive="(.*?)"')
11
12
13def command_backup(config):
14    hostname, path = normalize_location(config.args.location)
15    stdout, stderr, returncode = ssh(
16        'www-mgr', hostname,
17        '/usr/local/share/silverlining/mgr-scripts/backup-services.py %s' % config.args.location,
18        capture_stdout=True)
19    match = _backup_path_re.search(stdout)
20    if not match:
21        config.logger.fatal("Unexpected output from backup-services.py: %r" % stdout)
22        raise CommandError("Bad output")
23    backup_path = match.group(1)
24    config.logger.notify("Backed up to %s:%s" % (hostname, backup_path))
25    if not _scheme_re.search(config.args.destination):
26        copy_local(hostname, backup_path, config.args.destination)
27    else:
28        ssh('www-mgr', hostname,
29            '/usr/local/share/silverlining/mgr-scripts/transfer-backup.py %s %s'
30            % (backup_path, config.args.destination))
31
32
33def copy_local(hostname, backup_path, dest):
34    clean_paths = [backup_path]
35    if transfermethods.is_archive(dest):
36        stdout, stderr, returncode = ssh(
37            'www-mgr', hostname,
38            '/usr/local/share/silverlining/mgr-scripts/transfer-backup.py --archive %s %s'
39            % (backup_path, transfermethods.extension(dest)),
40            capture_stdout=True)
41        match = _archive_path_re.search(stdout)
42        if not match:
43            raise CommandError("Bad output from transfer-backup.py: %r" % stdout)
44        backup_path = match.group(1)
45        clean_paths.append(backup_path)
46    stdout, stderr, returncode = run(['scp', '-r', 'www-mgr@%s:%s' % (hostname, backup_path), dest])
47    if returncode:
48        raise CommandError("Error copying locally (scp exited with code %s); %s remains on server"
49                           % (returncode, ' '.join(clean_paths)))
50    ssh('www-mgr', hostname, 'rm -r %s' % ' '.join(clean_paths))