MySQL backup script python written

Source: Internet
Author: User
Tags mysql backup

Tag:ase   sub   rgs   int    Data sync     highlight   upm   log    backup    

#!/usr/bin/env python#-*-coding:utf-8-*-################################################################ Date: 2017/09/22 # filename:backupmysql.py# description:backup mysql files,base percona xtrabackup## back up MySQL database data, Data synchronization Backup in the main library: 10.99.10.22# backup data store directory:/data/backup/mysqlbak/# backup policy is a daily backup, named after the current day of the directory, such as: 20170922# retain the last 7 days of backup data, configurable # # # ########################################################### import required python librariesimport os Import sys Import timeimport Logging import datetime import subprocess logging.basicconfig (level=logging.                 DEBUG, format= ' [% (asctime) s] [% (levelname) s]% (message) s ', datefmt= '%y-%m-%d%h:%m:%s ', Filename= '/software/scrpits/backupmysql/backupmysql.log ', filemode= ' a ') # Configure database connection information Db_host = ' 10.99.10.22 ' db_user = ' bakuser ' db_user_pass = ' xxxxxxxx ' # configure local reserved data backup for how many days, default reserved for 7 days Datasave = 7# Configuration Backup base directory Backuppath = '/d ata/backup/mysqlbak/' daytime = Time.strftime ('%y%m%d ') Todaybackuppath = Backuppath + DayTimedef check (): "' Pre-backup checks, if directory exists then exit, otherwise create backup directory ' if Os.path.exists (todaybackuppath): res = ' the backup Directo        Ry already exists:%s. Exit ... '% Todaybackuppath print res logging.error (res) sys.exit () Else: Os.makedirs (todaybackuppath) res1 = "Creating backup folder%s"% Todaybackuppath logging.info (res1) d    EF backupdb (): "Backup database, define backup instructions, Parameters ' Check () logging.info (' Start backing up ')                     Iargs = "--slave-info--no-timestamp" backupcmd = "/usr/bin/innobackupex%s--host=%s--user=%s--password=%s%s" % (Iargs, Db_host, Db_user, Db_user_pass, todaybackuppath) p = subprocess. Popen (Backupcmd, stdout=subprocess. PIPE, Stderr=subprocess. PIPE, shell=true) Stdout,stderr = P.communicate () logging.info (stdout) logging.info (stderr) logging.info (' The Database backup is complete ') def gettimepoint (days): ' Returns the point in time ' that needs to be deleted ' Currtime = Time.time () DelTime = 3600*24* Int (days) Timepoint = currtime-deltime return timepointdef checkdir (cdir): ' Delete folder function ' ' Try:if os.path.i Sdir (Cdir): Os.rmdir (cdir) s = ' Remove dir%s succ ... '% cdir logging.info (s) except Exception as E:s = ' Remove dir%s FAIL!!! %s '% (Cdir, E) logging.error (s) def cleanold (Beforetime, path): ' Traverse the backup directory, get directory mtime time, compare timestamp, delete before directory ' L Ogging.warn (' Start cleaning up old backup data ... ') for Eachdir in Os.listdir (path): F = path + Eachdir l Astmtime = Os.stat (f). st_mtime if Lastmtime <= beforetime:checkdir (f) if __name__ = =  ' __main__ ': backupdb () t = Gettimepoint (Datasave) cleanold (T, backuppath)

  

MySQL backup script python written

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.