This database backup script is actually a small modification of another backup script available in Google. I am not able to get that URL to mark/link reference. The only modification I added is a four liner to remove a week old backup automatically. You may also see https://www.supportsages.com/2011/05/applying-wildcards-in-grant-option-of-mysql/ as well. Indenting in python is lost, will fix tomorrow ð
#!/usr/bin/env python
import ConfigParser
import os
import time
# Variable Definition
username = 'cpdbbackups'
password = 'p@ssw0Rd'
hostname = 're.mo.te.ip
backupfolder = '/home/dbbackups/servername'
filestamp = time.strftime('%Y-%m-%d-%H')
deletetime = time.time() - 7 * 86400
# Delete old files
for backup_file in os.listdir (backupfolder):
full_file_path = os.path.join(backupfolder, backup_file)
if os.path.getmtime(full_file_path) < deletetime:
os.unlink(full_file_path)
# Get a list of databases with :
database_list_command="mysql -u%s -p%s -h %s --silent -N -e 'show databases'" % (username, password, hostname)
for database in os.popen(database_list_command).readlines():
database = database.strip()
if database == 'information_schema':
continue
filename = "%s/%s-%s.sql" % (backupfolder, database, filestamp)
os.popen("mysqldump -u%s -p%s -h %s -e --opt -c %s | gzip -c > %s.gz" % (username, password, hostname, database, filename))




