Last active
August 29, 2015 13:57
-
-
Save igordelorenzi/9623807 to your computer and use it in GitHub Desktop.
Backup Script w/ Rotation Based on GFS
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
# -*- encoding: utf-8 -*- | |
import logging | |
import sh | |
import random | |
from bakthat.helper import BakHelper | |
# from datetime import datetime | |
logging.basicConfig(level=logging.INFO) | |
# CHECK WHAT WILL BE KEPT (BACKUP ROTATION) | |
# import datetime | |
# start_date = datetime.date(1999, 1, 1) | |
# end_date = datetime.date(1999, 12, 31) | |
# backups = [start_date + datetime.timedelta(days=i) | |
# for i in xrange((end_date - start_date).days + 1)] | |
# from grandfatherson import dates_to_keep, SATURDAY | |
# sorted(dates_to_keep(backups, days=14, weeks=4, months=6, | |
# firstweekday=SATURDAY, now=now)) | |
""" | |
Before run this script you should install and configure bakthat. | |
$ sudo pip install bakthat sh | |
$ bakthat configure | |
Run one of the following: | |
$ mysql_config_editor set --login-path=local | |
--host=localhost --user=localuser --password | |
OR (create a configuration file): | |
$ vim /path/to/.database.cnf | |
[client] | |
host = localhost | |
user = databaseuser | |
password = pass | |
and add (mysqldump parameter) | |
--defaults-extra-file=/path/to/.database.cnf | |
in place of | |
--login-path=local | |
""" | |
DESTINATION = "glacier" | |
BACKUP_NAME = "full_backup" | |
BACKUP_PASS = "" | |
MYSQL_DB = "superadb" | |
TMP = "/tmp/{0}/{1}".format(random.getrandbits(32), BACKUP_NAME) | |
sh.mkdir("-p", TMP) | |
sh.cp("-R", "/var/www/website/dir1", TMP) | |
sh.cp("-R", "/var/www/website/dir2", TMP) | |
sh.mysqldump("--defaults-extra-file=/root/.database.cnf", MYSQL_DB, _out=TMP+"/dump.sql") | |
with BakHelper(BACKUP_NAME, | |
destination=DESTINATION, | |
password=BACKUP_PASS, | |
tags=["dir1", "dir2", "mysql"]) as bh: | |
bh.backup(TMP) | |
bh.rotate() | |
sh.rm("-r", TMP) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment