Last active
November 14, 2018 14:10
-
-
Save TheYkk/aa819f09ce533627ea0506ab3267d2c2 to your computer and use it in GitHub Desktop.
mysql backup
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
[mysqldump] | |
host=localhost | |
user=user | |
password=password |
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
import datetime, os | |
today = str(datetime.datetime.today()).split(" ")[0] | |
backups = os.listdir("files") | |
def days_between(d1, d2): | |
d1 = datetime.datetime.strptime(d1, "%Y-%m-%d") | |
d2 = datetime.datetime.strptime(d2, "%Y-%m-%d") | |
return abs((d2 - d1).days) | |
cmd = 'mysqldump --defaults-extra-file=".database.cnf" --all-databases --events > files/'+today+'.sql' | |
os.system(cmd) | |
for backup in backups: | |
day = backup.replace(".sql", "") | |
diff = days_between(day, today) | |
if diff > 7: | |
os.remove('files/'+backup) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment