一個好用的Python備份mysql的指令碼

來源:互聯網
上載者:User

標籤:

前幾天打算用Python寫一個mysql指令碼,上Google看了下老外寫的,寫的挺好的,原地址在http://tecadmin.net/python-script-for-mysql-database-backup/#,所以就給 copy過來了

 1 #!/usr/bin/python 2 ########################################################### 3 # 4 # This python script is used for mysql database backup 5 # using mysqldump utility. 6 # 7 # Written by : Rahul Kumar 8 # Website: http://tecadmin.net 9 # Created date: Dec 03, 201310 # Last modified: Dec 03, 201311 # Tested with : Python 2.6.612 # Script Revision: 1.113 #14 ##########################################################15 16 # Import required python libraries17 import os18 import time19 import datetime20 21 # MySQL database details to which backup to be done. Make sure below user having enough privileges to take databases backup.22 # 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.23 24 DB_HOST = ‘localhost‘25 DB_USER = ‘root‘26 DB_USER_PASSWORD = ‘_root_user_password_‘27 #DB_NAME = ‘/backup/dbnames.txt‘28 DB_NAME = ‘db_name‘29 BACKUP_PATH = ‘/backup/dbbackup/‘30 31 # Getting current datetime to create seprate backup folder like "12012013-071334".32 DATETIME = time.strftime(‘%m%d%Y-%H%M%S‘)33 34 TODAYBACKUPPATH = BACKUP_PATH + DATETIME35 36 # Checking if backup folder already exists or not. If not exists will create it.37 print "creating backup folder"38 if not os.path.exists(TODAYBACKUPPATH):39     os.makedirs(TODAYBACKUPPATH)40 41 # Code for checking if you want to take single database backup or assinged multiple backups in DB_NAME.42 print "checking for databases names file."43 if os.path.exists(DB_NAME):44     file1 = open(DB_NAME)45     multi = 146     print "Databases file found..."47     print "Starting backup of all dbs listed in file " + DB_NAME48 else:49     print "Databases file not found..."50     print "Starting backup of database " + DB_NAME51     multi = 052 53 # Starting actual database backup process.54 if multi:55    in_file = open(DB_NAME,"r")56    flength = len(in_file.readlines())57    in_file.close()58    p = 159    dbfile = open(DB_NAME,"r")60 61    while p <= flength:62        db = dbfile.readline()   # reading database name from file63        db = db[:-1]         # deletes extra line64        dumpcmd = "mysqldump -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + TODAYBACKUPPATH + "/" + db + ".sql"65        os.system(dumpcmd)66        p = p + 167    dbfile.close()68 else:69    db = DB_NAME70    dumpcmd = "mysqldump -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + TODAYBACKUPPATH + "/" + db + ".sql"71    os.system(dumpcmd)72 73 print "Backup script completed"74 print "Your backups has been created in ‘" + TODAYBACKUPPATH + "‘ directory"
# chmod +x dbbackup.py
# python dbbackup.py
做定時任務執行:
0 2 * * * /usr/bin/python dbbackup.py

一個好用的Python備份mysql的指令碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.