python 脚本备份 mysql 数据库到 OSS
2017-10-10 16:58
661 查看
脚本如下:
再加上crontab 就搞定了
crontab -e
#!/usr/bin/python ########################################################### ########################################################## # Import required python libraries import os import time import datetime # MySQL database details to which backup to be done. Make sure below user having enough privileges to take databases backup. # To take multiple databases backup, create any file like /backup/dbnames.txt and put databses names one on each line and assignd to DB_NAME variable. DB_HOST = 'localhost' DB_USER = 'root' DB_USER_PASSWORD = '' DB_NAME = '/home/dbbackup/dbnames.txt' #DB_NAME = 'db_name' BACKUP_PATH = '/mnt/backup/mysql/' # Getting current datetime to create seprate backup folder like "12012013-071334". DATETIME = time.strftime('%Y%m%d-%H%M%S') TODAYBACKUPPATH = BACKUP_PATH + DATETIME # Checking if backup folder already exists or not. If not exists will create it. print "creating backup folder" if not os.path.exists(TODAYBACKUPPATH): os.makedirs(TODAYBACKUPPATH) # Code for checking if you want to take single database backup or assinged multiple backups in DB_NAME. print "checking for databases names file." if os.path.exists(DB_NAME): file1 = open(DB_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 # Starting actual database backup process. if multi: in_file = open(DB_NAME,"r") flength = len(in_file.readlines()) in_file.close() p = 1 dbfile = open(DB_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 + "/" + 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 + "/" + db + ".sql" os.system(dumpcmd) print "Backup script completed" print "Your backups has been created in '" + TODAYBACKUPPATH + "' directory" print "current path: " + os.getcwd() os.chdir(BACKUP_PATH) print "current path: " + os.getcwd() compress_file = TODAYBACKUPPATH + ".tar.gzip" print compress_file compress_cmd = "tar -czvf " + compress_file + " " + DATETIME print "compress_cmd " + compress_cmd os.system(compress_cmd) print "Compress success" remove_cmd = "rm -rf " + TODAYBACKUPPATH print "remove_cmd " + remove_cmd os.system(remove_cmd) print "Remove success" print "Start put OSS" import shutil import oss2 access_key_id = '' access_key_secret = '' bucket_name = 'x-backup' endpoint = 'oss-cn-hangzhou-internal.aliyuncs.com' bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name) bucket.put_object_from_file('mysql/'+DATETIME+'.tar.gzip',compress_file) print "Put OSS success"
再加上crontab 就搞定了
crontab -e
*/5 * * * * python /home/dbbackup/dbbackup.py >> /home/dbbackup/mylog.log 2>&1
相关文章推荐
- Python脚本实现自动将数据库备份到 Dropbox
- Python实现简单的文件传输与MySQL备份的脚本分享
- Linux Shell 脚本 自动备份 Mysql 数据库
- 备份网站数据库文件的Python脚本
- Python备份Mysql脚本
- mysql定时备份所需权限、shell脚本、OSS备份
- 【脚本语言系列】关于Python数据库处理MySQL, 你需要知道的事
- mysql - 数据库自动备份脚本
- Mysql 数据库自动备份Shell脚本
- mysql全备脚本,此脚本可以备份多个数据库,单独文件夹
- 备份 mysql 数据库脚本
- mysql 定时备份数据库脚本
- 使用python脚本实现mysql目录的备份
- python备份mysql脚本
- mysql全备脚本,此脚本可以备份多个数据库,单独文件夹
- python + crontab 实现 MySQL 数据库自动备份
- MySQL :: Linux 下自动备份数据库的 shell 脚本
- Python Mysql自动备份脚本
- Python 数据库备份脚本(邮件通知+日志记录)
- 利用MySQL的mysqlhotcopy实现数据库自动备份的SH脚本源码