由于担心服务器数据安全,所以写了这个脚本,结合前边分享的Server/Client 文件互传,可以备份网站数据到本地,安全又可靠

#!/usr/bin/python
# Filename: webbak.py
import os
import time
import tarfile

os.chdir('/home/web/') #切换目录
source = 'leonis'
bakdir = '/home/web/leonis/'
# mysql dump 
dump = 'mysqldump'
dbuser = 'XXXXXXX'
dbpwd = 'XXXXXXXXXXX'
dbname = 'XXXXXXXX'
sqlfile = '/home/web/leonis/leonis.sql'
sql = "%s -u%s -p%s %s >  %s" % (dump,dbuser,dbpwd,dbname,sqlfile) 
if os.path.exists(sqlfile):  
    os.remove(sqlfile)
else:
    print 'then will dump sql file'
result = os.popen(sql)
if result:              #
	print ("SQL backup completed!")
else:
	print ("SQL backup failed!")
# gzip 压缩 以当日日期命名
filename = bakdir + time.strftime('%Y%m%d')+'.tar.gz'
tar = tarfile.open(filename,"w:gz")
tar.add(source)
tar.close()