Last active
December 15, 2020 22:01
-
-
Save bigntallmike/bb8c951b4240ebf2144d987ffbe6fa06 to your computer and use it in GitHub Desktop.
Simple routine to call mysqldump to backup sql data in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from configparser import ConfigParser | |
def backup_mysql(): | |
config = ConfigParser() | |
config.read("/etc/sysconfig/mysqldump") | |
config = config['mysqldump'] | |
mysql_dump = ['mysqldump', | |
'--user=%(username)s' % config, | |
'--password=%(password)s' % config, | |
'--all-databases', | |
'--log-error=%(errorfile)s' % config ] | |
with open(config['backupfile'], "wb") as sqldata: | |
proc = subprocess.Popen(mysql_dump, stdout=sqldata, stderr=sys.stderr) | |
ret = proc.wait() | |
if ret is not None: | |
print("mysqldump exited with %d" % ret) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that
/etc/sysconfig/mysqldump
needs to contain a series of variables (no quote marks) for username, password, errorfile and backupfile.