[root@host1 ~]
#!/bin/bash
DATE=`date '+%Y%m%d'`
DATE_log=`date '+%Y-%m-%d %H:%M:%S'`
DATE_filename=`date '+%Y%m%d_%H:%M:%S'`
DATE_backup_time=`date '+%Y-%m-%d %H:%M:%S'`
mysql_host="192.168.31.110"
mysql_user="root"
mysql_pass="123456"
Mysql_Backup="/backup/mysql_backup"
Full_Backup="$Mysql_Backup/Full_backup"
Inc_Backup="$Mysql_Backup/Inc_backup"
Mysql_index="$Mysql_Backup/mysql_index.txt"
Mysql_data="/var/lib/mysql"
Mysql_bin="/usr/bin"
Mysql_db="$Mysql_Backup/mysql_db.txt"
Mysql_logbin_name="mysql_bin"
Mysql_binlog="/usr/bin/mysqlbinlog"
MYSQL="/usr/bin/mysql"
MYSQLDUMP="/usr/bin/mysqldump"
MYSQLADMIN="/usr/bin/mysqladmin"
SOCKET="/var/lib/mysql/mysql.sock"
Error_log="$Mysql_Backup/backup_error_${DATE}.log"
Backup_log="$Mysql_Backup/backup_${DATE}.log"
[ ! -d "$Mysql_Backup" ] && mkdir -p "$Mysql_Backup"
[ ! -d "$Full_Backup" ] && mkdir -p "$Full_Backup"
[ ! -d "$Inc_Backup" ] && mkdir -p "$Inc_Backup"
cd $Mysql_Backup
$MYSQL -u"$mysql_user" -p"$mysql_pass" -Ne 'show databases' | grep -vw 'test' | grep -vw 'performance_schema' | grep -vw 'information_schema' | grep -vw 'mysql' > $Mysql_db
function Full() {
echo -e 'Full backup of MySQL database... Please wait.\n'
cd $Mysql_Backup
[ ! -d "$Full_Backup"/"$DATE" ] && mkdir -p "$Full_Backup"/"$DATE"
echo "$DATE_backup_time" > "$Mysql_Backup"/Full_backup_time.txt
$MYSQLDUMP --flush-logs --quick --opt --master-data=2 --tz-utc=true --ignore-table=mysql.event -u"$mysql_user" -p"$mysql_pass" --all-databases | gzip > "$Full_Backup"/"$DATE"/all_databases_backup_"$DATE_filename".sql.gz
if [ $? -eq 0 ];then
/bin/logger "[ "$DATE_log" ] - Databases all-databases has been backup successful."
echo -e "[ "$DATE_log" ] - Databases all-databases has been backup successful.\n" >> "$Backup_log"
/bin/sleep 5
fi
[ ! -s "$Mysql_db" ] && echo -e 'Error: Backup a single database. Do not back up mysql system database.[test|performance_schema|information_schema|mysql]\n' && echo -e 'Mysql database full backup success.' && exit 1
for DBNAME in `cat $Mysql_db`
do
$MYSQLDUMP --ignore-table=mysql.event --skip-lock-tables --quick --opt --master-data=2 --tz-utc=true -u"$mysql_user" -p"$mysql_pass" -S "$SOCKET" "$DBNAME" | gzip > "$Full_Backup"/"$DATE"/"$DBNAME"_backup_"$DATE_filename".sql.gz
if [ $? -eq 0 ];then
/bin/logger "[ "$DATE_log" ] - Databases "${DBNAME}" has been backup successful."
echo "[ "$DATE_log" ] - Databases "${DBNAME}" has been backup successful." >> "$Backup_log"
/bin/sleep 5
fi
done
echo -e '----------------------------------------\n' >> "$Backup_log"
cd $Mysql_Backup
find "$Full_Backup" -mtime +10 | xargs rm -rf {} \; &> /dev/null
echo 'Mysql database full backup success.'
}
function Inc() {
echo -e 'Increment backup of MySQL database... Please wait.\n'
[ ! -f "$Mysql_Backup"/Full_backup_time.txt ] && echo -e 'Error: Please perform a full backup first. command: sh mysql_back.sh Mysql_Fullbackup' && exit 1
[ ! -s "$Mysql_db" ] && echo -e "Error: file $Mysql_db is empey. There may only be a system database[test|performance_schema|information_schema|mysql]." && exit 1
cd $Mysql_Backup
"$MYSQLADMIN" -u"$mysql_user" -p"$mysql_pass" flush-logs
awk -F'/' '{print $NF}' "$Mysql_data"/mysql_bin.index > "$Mysql_index"
[ ! -s "$Mysql_index" ] && echo -e "Error: Please open the mysql binlog log. The command in the my.cnf file: log_bin=mysql_bin and binlog_format=Mixed" && exit 1
[ ! -d "$Inc_Backup"/"$DATE" ] && mkdir -p "$Inc_Backup"/"$DATE"
DATE_starttime=`cat "$Mysql_Backup"/Full_backup_time.txt`
start='--start-datetime'
for DBNAME in `cat $Mysql_db`
do
for index in `cat $Mysql_index`
do
"$Mysql_binlog" -u"$mysql_user" -p"$mysql_pass" -d "$DBNAME" "$start"="$DATE_starttime" "$Mysql_data"/"$index" >> "$Inc_Backup"/"$DATE"/"$DBNAME"_Inc_"$DATE_filename".sql
/bin/sleep 1
done
if [ $? -eq 0 ];then
cd "$Inc_Backup"/"$DATE"
/usr/bin/zip -q "$DBNAME"_Inc_"$DATE_filename".zip "$DBNAME"_Inc_"$DATE_filename".sql
/bin/logger "[ "$DATE_log" ] - Databases "${DBNAME}" has been Inc_backup successful."
echo "[ "$DATE_log" ] - Databases "${DBNAME}" has been Inc_backup successful." >> "$Backup_log"
rm -rf "$DBNAME"_Inc_"$DATE_filename".sql
fi
done
echo -e '----------------------------------------\n' >> "$Backup_log"
cd $Mysql_Backup
find "$Inc_Backup" -mtime +10 | xargs rm -rf {} \; &> /dev/null
echo 'Mysql database Increment backup success.'
}
case $1 in
Mysql_Fullbackup)
Full;;
Mysql_Incbackup)
Inc;;
*)
echo -e "Usage: please input Mysql_Fullbackup|Mysql_Incbackup\n";;
esac
[root@host1 ~]