A shell script that periodically backs up MySQL database under Linux

Source: Internet
Author: User

A shell script that periodically backs up MySQL database under Linux

For any web site that is already on-line, data backup is a must. Whether it's a version update or a server migration, the importance of backing up your data goes without saying. The way you manually back up your data is not only time-consuming and energy-intensive, it's often unprofessional to say. As a result, the following script appears. Referring to many tutorials on the Web, plus your own tests, the following scripts are available for testing.

#!/bin/bash
#Shell Command For Backup MySQL Database Everyday Automatically By Crontab
#Author : Carlos Wong
#Date : 2010-08-24
#配置参数
USER=root #数据库用户名
PASSWORD=××××× #数据库用户密码
DATABASE=TIENIUZAI #数据库名称
[email protected] #管理员邮箱地址,用以发送备份失败消息提醒
BACKUP_DIR=/var/www/Data_Backup/topons/ #备份文件存储路径
LOGFILE=/var/www/Data_Backup/topons/data_backup.log #日记文件路径
DATE=`date ‘+%Y%m%d-%H%M’` #日期格式(作为文件名)
DUMPFILE=$DATE.sql #备份文件名
ARCHIVE=$DATE.sql.tgz #压缩文件名
OPTIONS=”-u$USER -p$PASSWORD –opt –extended-insert=false –triggers=false -R –hex-blob –flush-logs –delete-master-logs -B $DATABASE” #mysqldump 参数 详情见帮助 mysqldump -help

#判断备份文件存储目录是否存在, otherwise create the directory
if [!-D $BACKUP _dir];
Then
Mkdir-p "$BACKUP _dir"
Fi

#开始备份之前, writes the backup information header to the journal file
echo "" >> $LOGFILE
echo "" >> $LOGFILE
echo "——————————————— –" >> $LOGFILE
echo "BACKUP Date:" $ (date + "%y-%m-%d%h:%m:%s") >> $LOGFILE
echo "——————————————— –" >> $LOGFILE

#切换至备份目录
CD $BACKUP _dir
#使用mysqldump command Backup to make a database and name the backup file with a formatted timestamp
Mysqldump $OPTIONS > $DUMPFILE
#判断数据库备份是否成功
if [[$ = = 0]]; Then
#创建备份文件的压缩包
Tar czvf $ARCHIVE $DUMPFILE >> $LOGFILE 2>&1
#输入备份成功的消息到日记文件
echo "[$ARCHIVE] Backup successful! >> $LOGFILE
#删除原始备份文件, just keep the archive of your database backup files
Rm-f $DUMPFILE
Else
echo "Database Backup fail!" >> $LOGFILE

#备份失败后向网站管理者发送邮件提醒,需要mailutils或者类似终端下发送邮件工具的支持
#mail -s “Database:$DATABASE Daily Backup Fail” $WEBMASTER
fi
#输出备份过程结束的提醒消息
echo “Backup Process Done”


Use:

    1. Save the above code to:/usr/sbin/databackup (the file name is arbitrary, as long as the system does not have the same name as the original command, the code can be placed anywhere, placed in the Sbin directory just for the convenience of execution, sbin directory of files/directories can be directly called in the terminal, Similar to the directory specified by the path variable under Windows)
    2. Add executable permissions to the script: sudo chmod +x/usr/sbin/databackup
    3. Execute script: sudo databackup
    4. If you need to perform a backup command on a timed basis, simply place the following code in the crontab file (sudo vim/etc/crontab):

01 3 * * * root /usr/sbin/DataBackup #它代表着将于每天3点执行DataBackup脚本
Attention:

    1. The shell script definition variables under Linux are in the format: key=value, note that they cannot have spaces before or after the "=" between them, or the system cannot confirm the variable.
    2. The line marked in red, the first character "'" that resembles a single quotation mark is not a single quotation mark, and its input key is below the keyboard Esc key.
    3. This script is only suitable for backups of some small sites because it is a full backup of the database instead of an incremental backup, and is not suitable for large-capacity database backups.

Update:

2010-08-24: Because the socket entry value in the MySQL configuration file on the server is "/tmp/mysql.sock", mysqldump will look for the MySQL server when it is linked to/var/lib/mysql/ The Mysql.sock file under the directory. If not, the error will be:
mysqldump: Got error: 2002: Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’
The workaround is to:

First, modify the value of the socket in the MySQL configuration file is '/var/lib/mysql/mysql.sock ', but this method needs to restart the MySQL service, very unkind.

Second, create a soft link for/tmp/mysql.sock to/var/lib/mysql/mysql.sock:
ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock

A shell script that periodically backs up MySQL database under Linux

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.