I wrote a MySQL backup script in Linux. This is the first version. Only one database can be backed up. Every night at, I run the script with a timer and it will be automatically backed up. In the future, multiple databases can be backed up. If the backup fails, text messages will be sent to the mobile phone and expired backups will be cleared automatically.
MySQL automatic backup script in Linux-Version 2
- #! /Bin/bash
- # Script: backup_mysql
- # Version: Release 1.0
- # Author: www.dba-china.com
- # Date: 2011-10-26
- Backup_time = 'date + % Y % m % d % H % m' # define the backup detailed time
- Backup_Ymd = 'date + % Y % m % d' # define the year, month, and day time in the Backup Directory
- Backup_7ago = 'date-d' 7 days ago '+ % Y % m % d' # date before seven days
- Backup_dir =/backup/mysql/$ backup_Ymd # Full backup folder path
- Backup_database = "dba-china" # Name of the database to be backed up
- Mysql_user = "root"
- Mysql_password = "root"
- # Mysql_host = "localhost"
- # MySQL _ Port = "3306"
- # Determine whether MYSQL is started. If mysql is not started, the backup will exit.
- Mysql_ps = 'ps-ef | grep mysql | wc-l'
- Mysql_listen = 'netstat-an | grep LISTEN | grep $ mysql_port | wc-l'
- If [[$ mysql_ps = 0]-o [$ mysql_listen = 0]; then
- Echo "ERROR: MySQL is not running! Backup stop! "
- Exit;
- Else
- Echo "MySQL running OK! "
- Fi
- # Connect to the mysql database. If the connection fails, the backup will exit.
- Mysql-u $ mysql_user-p $ mysql_password <end
- Use mysql;
- Select host, user from user where user = 'root' and host = 'localhost ';
- Exit
- End
- Flag = 'echo $? '
- If [$ flag! = "0"]; then
- Echo "error: Can't connect mysql server! Backup stoped! "
- Exit;
- Else
- Echo "MySQL connect OK! Backup start ......"
- # Db = 'echo $ backup_database | cut-d', '-f1'
- Db = $ backup_database
- If [$ db = '']; then
- Echo "ERROR: no database to backup! "
- Else
- Echo "database $ db backup start ..."
- 'Mkdir-p $ backup_dir'
- 'Mysqldump-u $ mysql_user-p $ mysql_password $ db> $ backup_dir/$ db $ backup_time. SQL'
- Echo "database $ db backup success! "
- Fi
- Fi