#!/usr/bin/python
#! _*_ coding:utf-8 _*_
from datetime import date, datetime, timedelta
import os,time
user = 'exsd_db'
passw = '--login-path'
ip = 'rm-wz9yz653npnqgzap9.mysql.rds.aliyuncs.com'
dir = '/data/backup/db/tmp'
mysqlbase = '/usr/local/mysql/bin/'
if os.path.isdir(dir):
print("dir ok")
else:
os.makedirs(dir)
DBNAME=['exsd_db']
os.chdir(dir)
for dbname in DBNAME:
print(passw)
ad = os.popen(mysqlbase+'mysql '+passw+'=test -h'+ip+' -u'+user+' -e "use ' + dbname + ';show tables "|grep -v Tables_in').read()
ad1=ad.replace(' ',',').rstrip(',').split(',')
for tabname in ad1:
os.system(mysqlbase+'mysqldump '+passw+'=test -h'+ip+' -u'+user+' --set-gtid-purged=off --single-transaction -R '+dbname+' ' + tabname+' > '+dir+ '/'+dbname + '.'+ tabname +'.sql')
dat = time.strftime('%Y%m%d_%H%M',time.localtime(time.time()))
os.system('cd '+dir+';tar zcvf '+dat+dbname+'.tar.gz *sql; rm -f *.sql')
os.system('find /data/backup/db/tmp/* -mtime +6 -name "2017*.tar.gz" -exec rm -fr {} ;')
设置计划任务每小时备份一次方便恢复单表:
0 */1 * * * /root/python/mysqldump_table.py >/dev/null 2>&1
#@_@ 欢迎交流!联系人 广西宾阳 阿桂 qq149951292
#! _*_ coding:utf-8 _*_
from datetime import date, datetime, timedelta
import os,time
user = 'exsd_db'
passw = '--login-path'
ip = 'rm-wz9yz653npnqgzap9.mysql.rds.aliyuncs.com'
dir = '/data/backup/db/tmp'
mysqlbase = '/usr/local/mysql/bin/'
if os.path.isdir(dir):
print("dir ok")
else:
os.makedirs(dir)
DBNAME=['exsd_db']
os.chdir(dir)
for dbname in DBNAME:
print(passw)
ad = os.popen(mysqlbase+'mysql '+passw+'=test -h'+ip+' -u'+user+' -e "use ' + dbname + ';show tables "|grep -v Tables_in').read()
ad1=ad.replace(' ',',').rstrip(',').split(',')
for tabname in ad1:
os.system(mysqlbase+'mysqldump '+passw+'=test -h'+ip+' -u'+user+' --set-gtid-purged=off --single-transaction -R '+dbname+' ' + tabname+' > '+dir+ '/'+dbname + '.'+ tabname +'.sql')
dat = time.strftime('%Y%m%d_%H%M',time.localtime(time.time()))
os.system('cd '+dir+';tar zcvf '+dat+dbname+'.tar.gz *sql; rm -f *.sql')
os.system('find /data/backup/db/tmp/* -mtime +6 -name "2017*.tar.gz" -exec rm -fr {} ;')
设置计划任务每小时备份一次方便恢复单表:
0 */1 * * * /root/python/mysqldump_table.py >/dev/null 2>&1
#@_@ 欢迎交流!联系人 广西宾阳 阿桂 qq149951292