MySQL Database backup script:
#!/bin/bash# Defining variable Information user=rootpass=redhat12345 socket=/data/3306/mysql.sockcmd= "Mysql -u$USER -p$PASS -S $SOCKET "dump=" mysqldump --opt -u$user -p$pass -s $SOCKET " Des= "/usr/src/mysqlbackup" backuptime= "date +%f-%h:%m" #目录判断if [ ! -d "$DES/' date +%f ' ];thenmkdir -p $DES/' date +%f ' "Fi#mysqldump Backup Database echo "------------------------ ----Start backup mysql-------------------------------"for database in ' $CMD -e "show databases" | sed ' 1,2d ' | egrep -v "Mysql|schema" ' Do$dump $database -B|gzip> $DES/' date +%f '/${database}.sql.gzif [ $? -eq 0 ];thenecho "$database has been backup successful" elseecho "$database has been not successful "fi/bin/sleep 5doneecho "---------------------------backup is end -------------------------------------"
The
Execution results are as follows:
[[email protected] mysqlbackup]# pwd/usr/src/mysqlbackup[[email protected] Mysqlbackup]# ll Total dosage 4-rw-r--r-- 1 root root 805 12 month 6 18:50 backup.sh[[email protected] mysqlbackup]# bash backup.sh ---------------------- ------Start backup mysql-------------------------------dawnpro has been backup successfulhangzhou has been backup successfuljames has been backup Successfulwanlong has been backup successful---------------------------Backup is end -------------------------------------[[email protected] mysqlbackup]# ll Total dosage 8drwxr-xr-x 2 root root 4096 12 Month 6 18:56 2016-12-06- Rw-r--r-- 1 root root 805 12 Month 6 18:50 backup.sh[[email protected] mysqlbackup]# ll 2016-12-06/Total dosage 16-rw-r--r-- 1 root root 512 12 month 6 18:56 dawnpro.sql.gz-rw-r--r-- 1 root root 513 12 Month 6 18:56 Hangzhou.sql.gz-rw-r--r-- 1 root root 511 12 Month 6 18:56 James.sql.gz-rw-r--r-- 1 root root 511 12 Month 6 18:56 wanlong.sql.gzmysql> show databases;+--------------------+| database |+--------------------+| information_schema | | dawnpro | | hangzhou | | james | | mysql | | performance_schema | | wanlong |+--------------------+7 rows in set (0.00 SEC)
Make a simple record, then add a detailed note or optimize the script in the back.
This article is from the "Frozen vs watermelon" blog, so be sure to keep this source http://molewan.blog.51cto.com/287340/1880184
A MySQL backup script (can be adjusted with multiple instances)