您的位置:首页 > 编程语言 > Python开发

使用python自动备份数据库并上传到七牛服务器(定期清除)

2016-12-03 14:22 645 查看
lnmp 的网站服务器上需要对数据库的数据进行备份,很早之前貌似可以备份到百度云,不过后来百度云把端口关了,好在还可以放在七牛云服务器上,查了一些资料,然后用python写了一个七牛的数据备份的简单脚本,分享如下:。
1、首先肯定要有七牛的账号,然后获取acdess-key和seret_key(上七牛的官网,官方的文档还是简单易懂。
2、有一个邮箱,需要开启第三方登陆,我用的是网易163的邮箱


下面直接贴代码:

#!/root/anaconda/bin/python
#encoding:utf8
#Created by Taro
#2016.10.13
'''
Just as the name suggested,qiniu_backup is mainly used to backup the MySQL databases' data and upload the backup file to the qiniu server,finally it will automatedly delete the file which is out of the date

In addition,you can use the crontab command to backup the data at the time you want
'''
import zipfile
import os
import qiniu    #qiniu package
import smtplib
import time
from email.mime.text import  MIMEText
from email.header import Header
from qiniu import Auth,BucketManager

def rm_old_backupfile(del_time,dir_name,q,bucket_name):
bucket=BucketManager(q)
filelist=os.listdir(dir_name)
for f in filelist:
f_info=os.stat(dir_name+'/'+f)
#print f_info;
if((time.time()-f_info.st_mtime)>del_time):
os.remove(dir_name+'/'+f)
ret,info=bucket.delete(bucket_name,f)
# delete the file on qiniu

def zip_dir(dirname,zipfilename):
#print dirname+'****'+zipfilename
zf=zipfile.ZipFile(zipfilename,"w",zipfile.zlib.DEFLATED)
zf.write(dirname)
zf.close()
def sendmail(re_mail,msg):
mail_host="smtp.163.com"
mail_user=""
mail_pass=""
sender=""
receives=[re_mail]
msg=MIMEText(time.strftime('%Y-%m-%d')+'数据库备份'+str(msg),'plain','utf-8')
msg['From']='Taro'
msg['To']=re_mail
subject='Backup Database successfully'
msg['Subject']=Header(subject,'utf-8')
try:
smtpObj=smtplib.SMTP()
# smtpObj.set_debuglevel(1)
smtpObj.connect(mail_host,25)
smtpObj.login(mail_user,mail_pass)
smtpObj.sendmail(sender,receives,msg.as_string())
smtpObj.quit()
print "Send mail successfully!"
except Exception as err:
print err
if __name__=='__main__':
access_key=''
secret_key=''
bucket = ''
bucket_domain=''
dir_name='/backup/mysql_backup_data' #replace with yours
file_zip=dir_name+'/Myslq_backup'+time.strftime('%Y-%m-%d')+'.zip'
file_sql=dir_name+'/all.'+time.strftime('%Y-%m-%d')+'.sql'
re_mail='mail@qq.com'   #mail address
save_data_time=15*3600; # 15days
try:
#back up mysql database
os.system("mysqldump -u*root* -p*Password* --all-databases>/backup/mysql_backup_data/all.$(date +%Y-%m-%d).sql")
#time.sleep(10)
zip_dir(file_sql,file_zip)
q=Auth(access_key,secret_key)
token=q.upload_token(bucket)
ret,info=qiniu.put_file(token,'Myslq_backup'+time.strftime('%Y-%m-%d')+'.zip',file_zip)
rm_old_backupfile(save_data_time,dir_name,q,bucket)
sendmail(re_mail,'成功!')
sendmail('','成功!')
except Exception as err:
sendmail(re_mail, '失败!')
sendmail('','失败!'+str(err))
print  err;


上面的代码可以实现自动备份和过期清除,接下来只要让程序定时运行就可以了,使用linux自带的crontab 来执行这个python程序就可以。我为了指定运行时的环境变量(有的时候环境变量不对会导致程序出错),使用sh脚本来启动python程序,然后crontab则定时执行这个sh脚本就可以

pymail.sh:

#!/bin/bash
source /etc/profile
source /etc/bashrc
/root/anaconda/bin/python  /backup/qiniu_backup.py


使用 vim /etc/crontab 可以添加定时任务,例如我的:

30 4 * * * root /backup/qiniu_backup.sh >>/backup/qiniu_backup.log 2>&1


意思是每天凌晨四点半进行备份,并将程序的输出结果保存在log文件中。

至此,一个数据库定时自动备份和清除的程序就完成了,总的来说python脚本在这样小功能的实现里面还是很方便的!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息