Created
December 7, 2014 10:56
-
-
Save amiraliakbari/0bd36f974cd5a05bd2ed to your computer and use it in GitHub Desktop.
Auto Backup
This file contains hidden or 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
#!/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