标签:mysql备份脚本
1、备份脚本#!/usr/bin/env python #_*_coding:utf-8_*_ """ @File: backup_db.py @Author: OldTan @Email: 996298929@qq.com @Last Modified: 20180408 """ import os import datetime from threading import Thread HOST = '10.36.1.101' USER = 'root' PW = '1qaz#EDC' PATH = '/mnt/tantianran_mysql_backup/' NOW = datetime.datetime.now() NOWSTR = NOW.strftime('%Y%m%d') CLEANSTR = (NOW - datetime.timedelta(days=5)).strftime('%Y%m%d') DBS = [ 'urun_private_cloud', 'urun-asr', 'ambari', 'hive', 'oozie', 'storm-etl', 'urun-sms', 'analyzer-etl', 'zabbix', 'zabbix-key' ] def clean(db): database = '{dbs}'.format(dbs=db) format = 'rm -rf %(path)s%(db)s.%(date)s.sql' kv = {'path': PATH, 'date': CLEANSTR, 'db' : database} excute( format % kv) def backup(db): database = '{dbs}'.format(dbs=db) format = 'mysqldump -u %(user)s -h %(host)s -p%(pw)s %(db)s > %(path)s%(db)s.%(date)s.sql' kv = {'user': USER, 'host': HOST, 'pw': PW, 'path': PATH, 'date': NOWSTR, 'db' : database} excute(format % kv) def excute(cmd): os.system(cmd) def main(): thread_list = [] for i in range(len(DBS)): backup_t = Thread(target=backup, args=(DBS[i],)) clean_t = Thread(target=clean, args=(DBS[i],)) thread_list.append(backup_t) thread_list.append(clean_t) for t in thread_list: t.start() t.join() if __name__ == "__main__": main()
3、删除5天前的那一个备份
#!/usr/bin/env python import os import datetime from threading import Thread PATH = '/mnt/tantianran_mysql_backup/' NOW = datetime.datetime.now() NOWSTR = NOW.strftime('%Y%m%d') CLEANSTR = (NOW - datetime.timedelta(days=5)).strftime('%Y%m%d') DBS = [ 'urun_private_cloud', 'urun-asr', 'ambari', 'hive', 'oozie', 'storm-etl', 'urun-sms', 'analyzer-etl', 'zabbix', 'zabbix-key' ] def clean(db): database = '{dbs}'.format(dbs=db) format = 'rm -rf %(path)s%(db)s.%(date)s.sql' kv = {'path': PATH, 'date': CLEANSTR, 'db' : database} excute( format % kv) def excute(cmd): os.system(cmd) def main(): thread_list = [] for i in range(len(DBS)): clean_t = Thread(target=clean, args=(DBS[i],)) thread_list.append(clean_t) for t in thread_list: t.start() t.join() if __name__ == "__main__": main()
3、将当天生成的备份scp到别的地方
#!/usr/bin/env python #_*_coding:utf-8_*_ """ @File: backup_db.py @Author: OldTan @Email: 996298929@qq.com @Last Modified: 20180408 """ import os import datetime from threading import Thread REMOTE_PATH = '/mnt/tantianran_mysql_backup/' REMOTE_HOST = '10.36.1.55' REMOTE_USER = 'root' PATH = '/mnt/tantianran_mysql_backup/' NOW = datetime.datetime.now() NOWSTR = NOW.strftime('%Y%m%d') CLEANSTR = NOW.strftime('%Y%m%d') DBS = [ 'urun_private_cloud', 'urun-asr', 'ambari', 'hive', 'oozie', 'storm-etl', 'urun-sms', 'analyzer-etl', 'zabbix', 'zabbix-key' ] def scp_db_file(db): database = '{dbs}'.format(dbs=db) format = 'scp %(path)s%(db)s.%(date)s.sql %(remote_user)s@%(remote_host)s:%(remote_path)s' kv = {'path': PATH, 'date': CLEANSTR, 'db' : database, 'remote_user': REMOTE_USER, 'remote_host': REMOTE_HOST, 'remote_path': REMOTE_PATH} excute( format % kv) def excute(cmd): os.system(cmd) def main(): thread_list = [] for i in range(len(DBS)): scp_t = Thread(target=scp_db_file, args=(DBS[i],)) thread_list.append(scp_t) for t in thread_list: t.start() t.join() if __name__ == "__main__": main()
标签:mysql备份脚本
原文地址:http://blog.51cto.com/freshair/2096870