1 Star 0 Fork 3

wugeyu/python_auto_backup_mysql

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
autoBackup.py 1.72 KB
一键复制 编辑 原始数据 按行查看 历史
oO小飞Oo 提交于 2017-09-11 17:41 . inited
import os,time,datetime,threading
global timer
def auto_time():
auto_backup()
timer=threading.Timer(3600,auto_time)
timer.start()
def auto_backup():
DB_HOST = '127.0.0.1'
DB_USER = 'root'
DB_USER_PASSWORD = 'abc123'
DB_NAME = 'ldsg_s25'
BACKUP_PATH = '/bk/'
FILE_NAME = time.strftime('%m%d%Y-%H%M%S')
TODAYBACKUPPATH = BACKUP_PATH + time.strftime('%m%d%Y')
print "creating backup folder"
if not os.path.exists(TODAYBACKUPPATH):
os.makedirs(TODAYBACKUPPATH)
print "checking for databases names file."
if os.path.exists(FILE_NAME):
file1 = open(FILE_NAME)
multi = 1
print "Databases file found..."
print "Starting backup of all dbs listed in file " + DB_NAME
else:
print "Databases file not found..."
print "Starting backup of database " + DB_NAME
multi = 0
if multi:
in_file = open(FILE_NAME,"r")
flength = len(in_file.readlines())
in_file.close()
p = 1
dbfile = open(FILE_NAME,"r")
while p <= flength:
db = dbfile.readline() # reading database name from file
db = db[:-1] # deletes extra line
dumpcmd = "mysqldump -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + TODAYBACKUPPATH+"/"+FILE_NAME +"_"+ db + ".sql"
os.system(dumpcmd)
p = p + 1
dbfile.close()
else:
db = DB_NAME
dumpcmd = "mysqldump -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + TODAYBACKUPPATH+"/"+ FILE_NAME +"_"+ db + ".sql"
os.system(dumpcmd)
print "Backup script completed"
print "Your backups has been created in '" + TODAYBACKUPPATH + "' directory"
auto_time()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/wugeyu/python_auto_backup_mysql.git
[email protected]:wugeyu/python_auto_backup_mysql.git
wugeyu
python_auto_backup_mysql
python_auto_backup_mysql
master

搜索帮助