Skip to content

Instantly share code, notes, and snippets.

@bigntallmike
Last active December 15, 2020 22:01
Show Gist options
  • Save bigntallmike/bb8c951b4240ebf2144d987ffbe6fa06 to your computer and use it in GitHub Desktop.
Save bigntallmike/bb8c951b4240ebf2144d987ffbe6fa06 to your computer and use it in GitHub Desktop.
Simple routine to call mysqldump to backup sql data in Python
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)
@bigntallmike
Copy link
Author

Note that /etc/sysconfig/mysqldump needs to contain a series of variables (no quote marks) for username, password, errorfile and backupfile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment