MySQL备份脚本V2(添加日志功能及备份后检查)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL备份脚本V2(添加日志功能及备份后检查)相关的知识,希望对你有一定的参考价值。
备份脚本
#!/usr/bin/env python #_*_coding:utf-8_*_ """ @File: backup_db.py @Author: OldTan @Email: [email protected] @Last Modified: 20180408 """ import os import datetime from threading import Thread from logs import log 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' ] after_backup = [] 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} expiredDbFile = ('%(db)s.%(date)s.sql' % kv) if os.path.exists(expiredDbFile): msg = 'Start Delete expired database file: %s' % (expiredDbFile) log(msg) 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} dbname = ('%(db)s.%(date)s.sql' % kv) after_backup.append(dbname) excute(format % kv) def excute(cmd): os.system(cmd) def check(): for i in after_backup: if os.path.exists(i): m = 'Backup success DB: %s' % i log(m) else: m = 'Backup failed DB: %s' % i log(m) def main(): dbs = range(len(DBS)) def exec_task(th): for i in dbs: th[i].start() for i in dbs: th[i].join() backup_t = [] clean_t = [] for i in dbs: t1 = Thread(target=backup, args=(DBS[i],)) t2 = Thread(target=clean, args=(DBS[i],)) backup_t.append(t1) clean_t.append(t2) exec_task(backup_t) exec_task(clean_t) check() if __name__ == "__main__": main()
scp sql文件脚本
#!/usr/bin/env python #_*_coding:utf-8_*_ """ @File: backup_db.py @Author: OldTan @Email: [email protected] @Last Modified: 20180408 """ import os import datetime from threading import Thread from logs import log 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)[email protected]%(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} d = ('%(db)s.%(date)s.sql' % kv ) if os.path.exists(d): m = "Start SCP {} database sql file...".format(db) log(m) excute( format % kv) def excute(cmd): os.system(cmd) def main(): print(''' -------------------------------------------- Transfer sql backup files -------------------------------------------- ''') thread_list = [] db_number = range(len(DBS)) for i in db_number: scp_t = Thread(target=scp_db_file, args=(DBS[i],)) thread_list.append(scp_t) for i in db_number: thread_list[i].start() for i in db_number: thread_list[i].join() if __name__ == "__main__": main()
清除到期sql文件脚本
#!/usr/bin/env python import os import datetime from threading import Thread from logs import log 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} expiredDbFile = ('%(db)s.%(date)s.sql' % kv) if os.path.exists(expiredDbFile): msg = 'Start Delete expired database file: %s' % (expiredDbFile) log(msg) excute( format % kv) def excute(cmd): os.system(cmd) def main(): thread_list = [] dbs = range(len(DBS)) for i in dbs: clean_t = Thread(target=clean, args=(DBS[i],)) thread_list.append(clean_t) for i in dbs: thread_list[i].start() for i in dbs: thread_list[i].join() if __name__ == "__main__": main()
以上是关于MySQL备份脚本V2(添加日志功能及备份后检查)的主要内容,如果未能解决你的问题,请参考以下文章