Skip to content

Instantly share code, notes, and snippets.

@amiraliakbari
Created December 7, 2014 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amiraliakbari/0bd36f974cd5a05bd2ed to your computer and use it in GitHub Desktop.
Save amiraliakbari/0bd36f974cd5a05bd2ed to your computer and use it in GitHub Desktop.
Auto Backup
#!/usr/bin/env python
import ConfigParser
import os
import time,sys
DB_NAME = 'PNAME'
PATH = '/www/PNAME/src/backup/'
DAY_LIMIT_FOR_KEEPING_BACKUPS = 60
# On Debian, /etc/mysql/debian.cnf contains 'root' a like login and password.
config = ConfigParser.ConfigParser()
config.read("/etc/mysql/debian.cnf")
username = config.get('client', 'user')
password = config.get('client', 'password')
hostname = config.get('client', 'host')
filestamp = time.strftime('%Y-%m-%d')
database_list_command="mysql -u %s -p%s -h %s --silent -N -e 'show databases'" % (username, password, hostname)
filename = "%s%s-%s.sql" % (PATH,DB_NAME, filestamp)
os.popen("mysqldump -u %s -p%s -h %s -e --opt -c %s | gzip -c > %s.gz" % (username, password, hostname, DB_NAME, filename))
now = time.time()
for f in os.listdir(PATH):
f = os.path.join(PATH, f)
if os.stat(f).st_mtime < now - DAY_LIMIT_FOR_KEEPING_BACKUPS * 86400:
if os.path.isfile(f):
os.remove(os.path.join(PATH, f))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment