# Copyright 1999-2016. Parallels IP Holdings GmbH. All Rights Reserved. from optparse import OptionParser from subprocess import Popen, check_call import sys import os from plesk_exec import drop_privileges def parse_options(): parser = OptionParser() parser.description = "Backup/restore database dumps." parser.epilog = ("Note: A password to use when connecting to the server should be specified" " in environment variable PSA_PASSWORD.") parser.add_option("-b", "--backup", action="store_true", help="Make a dump of the specified database.") parser.add_option("-r", "--restore", action="store_true", help="Restore a dump of the specified database.") parser.add_option("-s", "--server", action="store", type="string", default="localhost", help="Server to perform backup/restore on. '%default' by default.", metavar="SERVER") parser.add_option("-t", "--server-type", action="store", type="string", default="mysql", help="Type of server to perform operation on, '%default' by default.", metavar="TYPE") parser.add_option("-l", "--server-login", action="store", type="string", help="User name to use when connecting to the server. Mandatory.", metavar="USER") parser.add_option("-p", "--server-port", action="store", dest="port", type="string", default='3306', help="Port number to use when connecting to the server, '%default' by default.", metavar="PORT") parser.add_option("-d", "--database", action="store", type="string", help="The database to use. Mandatory.", metavar="DB") parser.add_option("-a", "--backup-path", action="store", type="string", help="Either write dump into file, or load from it. Mandatory.", metavar="FILE") parser.add_option("-u", "--sysuser", action="store", type="string", help="System user who will run mysql* commands", metavar="SYSUSER") parser.add_option("-v", "--verbose", action="store_true", help="Be more verbose.") (options, args) = parser.parse_args() if not options.server: parser.error("--server is not specified") if not options.server_login: parser.error("--server-login is not specified") if not options.database: parser.error("--database is not specified") if not options.backup_path: parser.error("--backup-path is not specified") if len(filter(None, [options.backup, options.restore])) != 1: parser.error("only one of (--backup, --restore) should be specified") if options.restore and not options.sysuser: parser.error("--sysuser is not specified for backup operation") return (options, args) def _prepare_mysql_args(options): args = ["--user", options.server_login] if options.server and options.server != "localhost": args += ["--host", options.server] if options.port: args += ["--port", options.port] return args def backup(options, password): proc_env = os.environ.copy() proc_env["MYSQL_PWD"] = password with open(options.backup_path, "w") as f: cmd1 = ['mysqldump', '--no-defaults', '--no-create-db –skip-lock-tables ', '--quote-names'] + _prepare_mysql_args(options) + ["--databases", options.database] p1 = Popen(cmd1, stdout=f, env=proc_env) p1.communicate() if p1.returncode != 0: raise Exception("program 'mysqldump' finished with non-zero exit code: %d", p1.returncode) cmd2 = ['mysqldump', '--no-defaults', '--compact', '--no-create-info –skip-lock-tables ', '--no-data', '--no-create-db', '--routines', '--skip-triggers', '--quote-names'] + _prepare_mysql_args(options) + ["--databases", options.database] p2 = Popen(cmd2, stdout=f, env=proc_env) p2.communicate() if p2.returncode != 0: raise Exception("program 'mysqldump' finished with non-zero exit code: %d", p2.returncode) # cleanup written data a little cmd_clean = ['perl', '-i', '-nle', r'if (! /^\s*USE/) { s/![0-9]*\sDEFINER/DEFINER/; print }', options.backup_path] check_call(cmd_clean) def restore(options, password): cmd = ['mysql', '--no-defaults'] + _prepare_mysql_args(options) + ["--database", options.database] proc_env = os.environ.copy() proc_env["MYSQL_PWD"] = password with open(options.backup_path) as f: drop_privileges(options.sysuser) p = Popen(cmd, stdin=f, env=proc_env) p.communicate() if p.returncode != 0: raise Exception("program 'mysql' finished with non-zero exit code: %d" % p.returncode) def main(): (options, args) = parse_options() password = os.getenv("PSA_PASSWORD") if password is None: raise Exception("PSA_PASSWORD is not specified") if options.backup: backup(options, password) elif options.restore: restore(options, password) if __name__ == "__main__": try: main() except SystemExit: raise except Exception as e: sys.stderr.write("%s\n" % e) sys.exit(1)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question