Python writes MySQL automatic backup script

Source: Internet
Author: User
Tags comments mysql automatic backup

#!/usr/bin/env python # -*- coding: utf-8 -*-  # filename: mysql_dump.py import os import time import string ‘‘‘ defined variable ‘‘‘ databases = ‘--database kbss_kcas reportdb koacif sequence infocenter‘ sql_user = ‘root‘ sql_pwd = ‘123456‘ ‘‘‘ Defining the remote backup variables ‘‘‘ jv_test01_dir = "/opt/mysql" ‘‘‘ Create the backup file directory ‘‘‘ mkdir_dir = "/opt/" + time.strftime( ‘%Y%m‘ ) + "/" if not os.path.exists(mkdir_dir):          os.mkdir(mkdir_dir)          print ‘Successfully created directory‘ , mkdir_dir ‘‘‘ Start backup of database to the specified directory ‘‘‘ database_name  = ‘databak31‘ os.chdir(mkdir_dir) today_sql = mkdir_dir + database_name + ‘_‘ + time.strftime( ‘%Y%m%d‘ ) + ‘.sql‘ sql_comm = "mysqldump -u %s -p‘%s‘ -R --events --quick --single-transaction %s > %s" % (sql_user,sql_pwd,databases,today_sql) if os.system(sql_comm)  = =  0 :          print database_name, ‘is backup successfully!‘ else :          print database_name, ‘is backup Failed!!‘ time.sleep(  3 ) scp_comm = "cp %s %s " % (today_sql,jv_test01_dir)  #这里我使用了一个挂在盘,所以直接拷贝到挂在盘就可以做备份文件二次保护啦! if os.system(scp_comm)  = =  0 :          print today_sql, ‘This file backup to jv_test01 success!‘ else :          print today_sql, ‘This file backup to jv_test01  Failed!!‘

Python writes MySQL automatic backup 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.