def mirror_remote(src_dir): # # This isn't invoked as a top-level operation so don't create a full # logfile; just use standard debugging-level support. # logger = enbackup.log.Logger('mirror') try: config = enbackup.utils.DefaultConfigParser( { "Paths" : { "src_dir_restrict": src_dir_restrict_default } }) config.read(cfg_file_path) src_dir_restrict = config.get("Paths", "src_dir_restrict") (path_is_valid, msg) = validate_local_dir(src_dir, src_dir_restrict) if not path_is_valid: logger.log(msg) raise MirrorCmdError(src_dir, msg) # # If all is well, start the rsync server to mirror the requested # directory. If currently running as root, make sure we drop down to # the enbackup user account for actually running the rsync. We don't # really expect this to happen on the remote end, but best to be # sure! # rsync_cmd_str = mirror_cmd_remote + "{0}".format(src_dir) if os.geteuid() == 0: rsync_cmd = ["su", get_username(), "--command", rsync_cmd_str] else: rsync_cmd = rsync_cmd_str.split() logger.debug("Remote mirror command: {0}".format(rsync_cmd)) logger.debug("About to lock '{0}' for remote mirror". format(src_dir)) with DirLock("mirror-remote (PID {0})".format(os.getpid()), src_dir): run_cmd(rsync_cmd, logger) logger.debug("Remote mirror done") except Exception as e: logger.error("Failed remote mirror: {}".format(e)) raise
def notify_remote(server, remote_subcommand): ssh_user = get_username() # # Verify that the 'server' argument is a simple name -- no spaces, # escaping, quotes, etc. permitted -- before running the command. # If running as root, switch to the enbackup user before running # the SSH so that the right key gets picked up. In future we # could make this configurable... # if re.match("([A-Za-z0-9/\-_:]+)", server): ssh_cmd_str = "ssh {0}@{1} enbackup notify {2}".format( ssh_user, server, " ".join(remote_subcommand)) if os.geteuid() == 0: ssh_cmd = ["su", ssh_user, "--command", ssh_cmd_str] else: ssh_cmd = ssh_cmd_str.split() run_cmd(ssh_cmd, logger) else: logger.error("Server '{0}' name is not in the correct format".format( server))