MySQL Whole Library Single table backup for Python script

Source: Internet
Author: User
Tags python script

#!/usr/bin/python
#! _*_ Coding:utf-8 _*_

From datetime import Date, datetime, Timedelta
Import Os,time

user = ' Dbuser '
PASSW = ' Password '
ip = ' 10.8.8.8 '
dir = '/data/backup/db/tmp '
Mysqlbase = '/usr/local/mysql/bin/'

If Os.path.isdir (dir):
Print ("dir OK")
Else
Os.makedirs (dir)
dbname=[' db1 ', ' db2 ', ' DB3 ']
Os.chdir (dir)
For dbname in dbname:
Print (PASSW)
AD = Os.popen (mysqlbase+ ' mysql-h ' +ip+ '-u ' +user+ '-P ' +passw+ '-e ' use ' + dbname + '; show tables ' |grep-v tables_in '). Re AD ()
Ad1=ad.replace (' \ n ', ', '). Rstrip (', '). Split (', ')
For tabname in AD1:
Os.system (mysqlbase+ ' mysqldump-h ' +ip+ '-u ' +user+ '-P ' +passw+ '--set-gtid-purged=off--single-transaction-r ' +dbname + ' + tabname+ ' > ' +dir+ '/' +dbname + '. ' + tabname + '. sql ')
DAT = time.strftime ('%y%m%d_%h%m ', Time.localtime (Time.time ()))
Os.system (' CD ' +dir+ '; tar zcvf ' +dat+dbname+ '. tar.gz *sql; rm-f *.sql ')


Set up scheduled tasks to be backed up once per hour easy to recover single table:
0 */1 * * */root/python/mysqldump_table.py >/dev/null 2>&1

Welcome to communicate with you.

MySQL Whole Library Single table backup for Python script

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.