Python + crontab for automatic MySQL Database Backup

Source: Internet
Author: User
Tags mysql commands
Python + crontab for automatic MySQL database backup [Copy link]
   
  Direct elevator

1#

Posted on 23:10:10| View only the author | view in reverse order

A long time ago, in order to realize automatic backup of MySQL databases, a script was written in SH under FreeBSD and put in crontab, which was easy to solve.
Today's whimsy, I wanted to use python to make a script to implement automatic MySQL database backup, so I had to go over it and the results were good.

Test environment: ubuntu704 + python2.5 + mysql5

I. program list: auto_backup.py
(Make sure you have certain permissions on the backup directory and log directory)
#*************************************** **************************************** ***************************************
#! /Usr/bin/ENV Python
#-*-Coding: utf8 -*-
# Mysql_backup @ Python
# Functions: automatically creates backup directories, logs, and compressed backup files for the current day.
# Created by fufay on 2007-04-30, version 0.1

Import sys, OS, time

# ----------------------------- Initial information --------------------------------------------
Mysql_usr = 'root' # MySQL user
Mysql_pwd = '780408ss' # MySQL password
Mysql_db = 'mysql' # MySQL database
Mysql_charset = 'gb2312' # Database Encoding
Bk_path = '/home/fufay/Python/backup/' # Backup Directory
Export _path = '/usr/bin/' # command mysqldump path
Logs_path = bk_path + 'logs' # Full Log File Path
# ----------------------------- Initial completion --------------------------------------------

# Log writing functions
Def writelogs (filename, contents ):
F = file (filename, 'aw ')
F. Write (contents)
F. Close ()

# The backup directory is named after the current day.
Today = bk_path + time. strftime ('% Y-% m-% D ')
# The Database Backup name is named after the backup time
Fname = Today + OS. SEP + time. strftime ('% H % m % s') + '.gz'

# Create the backup directory for the current day
If not OS. Path. exists (today ):
MSG = '-' * 30 + time. strftime ('% Y-% m-% d, % H: % m: % s') +'-'* 30 +' \ N'
If (OS. mkdir (today) = none:
MSG + = *** Backup directory created successfully: '+ today +' \ n \ N'
Writelogs (logs_path, MSG)
Else:
MSG + = '!! Failed to create Backup Directory: '+ today +'. Check whether the directory is writable! \ N \ N'
Writelogs (logs_path, MSG)
SYS. Exit ()

# Backing up MySQL commands
Export _dump = "% smysqldump-U % s-p % s -- default-character-set = % s -- opt % S | gzip> % s" % \
(Pai_path, mysql_usr, mysql_pwd, mysql_charset, mysql_db, fname)

# Run the BACKUP command
If OS. System (export _dump) = 0:
Writelogs (logs_path, 'Data backup: '+ fname +' \ n ')
Else:
Writelogs (logs_path, 'Data backup failed! \ N ')

# Over
#*************************************** **************************************** ***************************************

2. Related to crontab.
Open the terminal, copy auto_backup.py to a folder (such as/home/fufay/Python/), and set its permission to "allow execution ":
Chmod A + X./auto_backup.py

Set crontab:
Crontab-e
For example:
*/30 */home/fufay/Python/auto_backup.py
Save and exit
OK, so that the system runs auto_backup.py every 30 minutes (of course daily/weekly) to back up the database.

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.