|
| 1 | +import os |
| 2 | +import logging |
| 3 | +import re |
| 4 | + |
| 5 | +from copy_reg import pickle |
| 6 | +from multiprocessing import Pool |
| 7 | +from subprocess import check_output |
| 8 | +from types import MethodType |
| 9 | + |
| 10 | +from RsyncUploadThread import RsyncUploadThread |
| 11 | + |
| 12 | +from mongodb_consistent_backup.Common import config_to_string |
| 13 | +from mongodb_consistent_backup.Errors import OperationError |
| 14 | +from mongodb_consistent_backup.Pipeline import Task |
| 15 | + |
| 16 | + |
| 17 | +# Allows pooled .apply_async()s to work on Class-methods: |
| 18 | +def _reduce_method(m): |
| 19 | + if m.im_self is None: |
| 20 | + return getattr, (m.im_class, m.im_func.func_name) |
| 21 | + else: |
| 22 | + return getattr, (m.im_self, m.im_func.func_name) |
| 23 | + |
| 24 | + |
| 25 | +pickle(MethodType, _reduce_method) |
| 26 | + |
| 27 | + |
| 28 | +class Rsync(Task): |
| 29 | + def __init__(self, manager, config, timer, base_dir, backup_dir, **kwargs): |
| 30 | + super(Rsync, self).__init__(self.__class__.__name__, manager, config, timer, base_dir, backup_dir, **kwargs) |
| 31 | + self.backup_location = self.config.backup.location |
| 32 | + self.backup_name = self.config.backup.name |
| 33 | + self.remove_uploaded = self.config.upload.remove_uploaded |
| 34 | + self.rsync_path = self.config.upload.rsync.path |
| 35 | + self.rsync_user = self.config.upload.rsync.user |
| 36 | + self.rsync_host = self.config.upload.rsync.host |
| 37 | + self.rsync_port = self.config.upload.rsync.port |
| 38 | + self.rsync_ssh_key = self.config.upload.rsync.ssh_key |
| 39 | + self.retries = self.config.upload.rsync.retries |
| 40 | + self.thread_count = self.config.upload.rsync.threads |
| 41 | + self.rsync_binary = "rsync" |
| 42 | + |
| 43 | + self.rsync_flags = ["--archive", "--compress"] |
| 44 | + self.rsync_version = None |
| 45 | + self._rsync_info = None |
| 46 | + |
| 47 | + self._pool = Pool(processes=self.threads()) |
| 48 | + |
| 49 | + def init(self): |
| 50 | + if not self.host_has_rsync(): |
| 51 | + raise OperationError("Cannot find rsync binary on this host!") |
| 52 | + if not os.path.isdir(self.backup_dir): |
| 53 | + logging.error("The source directory: %s does not exist or is not a directory! Skipping Rsync upload!" % self.backup_dir) |
| 54 | + raise OperationError("The source directory: %s does not exist or is not a directory! Skipping Rsync upload!" % self.backup_dir) |
| 55 | + |
| 56 | + def rsync_info(self): |
| 57 | + if not self._rsync_info: |
| 58 | + output = check_output([self.rsync_binary, "--version"]) |
| 59 | + search = re.search("^rsync\s+version\s([0-9.-]+)\s+protocol\sversion\s(\d+)", output) |
| 60 | + self.rsync_version = search.group(1) |
| 61 | + self._rsync_info = {"version": self.rsync_version, "protocol_version": int(search.group(2))} |
| 62 | + return self._rsync_info |
| 63 | + |
| 64 | + def host_has_rsync(self): |
| 65 | + if self.rsync_info(): |
| 66 | + return True |
| 67 | + return False |
| 68 | + |
| 69 | + def prepare_dest_dir(self): |
| 70 | + # mkdir -p the rsync dest path via ssh |
| 71 | + ssh_mkdir_cmd = ["ssh"] |
| 72 | + if self.rsync_ssh_key: |
| 73 | + ssh_mkdir_cmd.extend(["-i", self.rsync_ssh_key]) |
| 74 | + ssh_mkdir_cmd.extend([ |
| 75 | + "%s@%s" % (self.rsync_user, self.rsync_host), |
| 76 | + "mkdir", "-p", self.base_dir |
| 77 | + ]) |
| 78 | + |
| 79 | + # run the mkdir via ssh |
| 80 | + try: |
| 81 | + check_output(ssh_mkdir_cmd) |
| 82 | + except Exception, e: |
| 83 | + logging.error("Creating rsync dest path with ssh failed for %s: %s" % ( |
| 84 | + self.rsync_host, |
| 85 | + e |
| 86 | + )) |
| 87 | + raise e |
| 88 | + |
| 89 | + return True |
| 90 | + |
| 91 | + def done(self, data): |
| 92 | + logging.info(data) |
| 93 | + |
| 94 | + def run(self): |
| 95 | + try: |
| 96 | + self.init() |
| 97 | + self.timer.start(self.timer_name) |
| 98 | + |
| 99 | + logging.info("Preparing destination path on %s" % self.rsync_host) |
| 100 | + self.prepare_dest_dir() |
| 101 | + |
| 102 | + rsync_config = { |
| 103 | + "dest": "%s@%s:%s" % (self.rsync_user, self.rsync_host, self.rsync_path), |
| 104 | + "threads": self.threads(), |
| 105 | + "retries": self.retries |
| 106 | + } |
| 107 | + rsync_config.update(self.rsync_info()) |
| 108 | + logging.info("Starting upload using rsync version %s (%s)" % ( |
| 109 | + self.rsync_info()['version'], |
| 110 | + config_to_string(rsync_config) |
| 111 | + )) |
| 112 | + for child in os.listdir(self.backup_dir): |
| 113 | + self._pool.apply_async(RsyncUploadThread( |
| 114 | + os.path.join(self.backup_dir, child), |
| 115 | + self.base_dir, |
| 116 | + self.rsync_flags, |
| 117 | + self.rsync_path, |
| 118 | + self.rsync_user, |
| 119 | + self.rsync_host, |
| 120 | + self.rsync_port, |
| 121 | + self.rsync_ssh_key, |
| 122 | + self.remove_uploaded, |
| 123 | + self.retries |
| 124 | + ).run, callback=self.done) |
| 125 | + self.wait() |
| 126 | + except Exception, e: |
| 127 | + logging.error("Rsync upload failed! Error: %s" % e) |
| 128 | + raise OperationError(e) |
| 129 | + finally: |
| 130 | + self.timer.stop(self.timer_name) |
| 131 | + self.completed = True |
| 132 | + |
| 133 | + def wait(self): |
| 134 | + if self._pool: |
| 135 | + logging.info("Waiting for Rsync upload threads to stop") |
| 136 | + self._pool.close() |
| 137 | + self._pool.join() |
| 138 | + |
| 139 | + def close(self): |
| 140 | + if self._pool: |
| 141 | + logging.error("Stopping Rsync upload threads") |
| 142 | + self._pool.terminate() |
| 143 | + self._pool.join() |
0 commit comments