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. |