Скрипт выполняет бэкап всех баз MySQL и делает ротацию бэкапов: бэкапы за последние 7 дней, недельные бэкапы и месячный бэкап.
Для использования скрипта необходимо установить Percona XtraBackup.
Сам скрипт:
#!/bin/bash DATADIR=/path/to/backups/ BK_NAME="BK_Name" PREFIX=`date +%F` # Setup variables $archive_file for the archive filename. ---------------------- START// day=$(date +%A) hostname=$(hostname -s) # Find which week of the month 1-4 it is. day_num=$(date +%d) if (( $day_num <= 7 )); then week_file="$hostname-week1.tgz" elif (( $day_num > 7 && $day_num <= 14 )); then week_file="$hostname-week2.tgz" elif (( $day_num > 14 && $day_num <= 21 )); then week_file="$hostname-week3.tgz" elif (( $day_num > 21 && $day_num < 32 )); then week_file="$hostname-week4.tgz" fi # Find if the Month is odd or even. month_num=$(date +%m) month=$(expr $month_num % 2) if [ $month -eq 0 ]; then month_file="$hostname-month2.tgz" else month_file="$hostname-month1.tgz" fi # Create archive filename. if [ $day_num == 1 ]; then archive_file=$month_file elif [ $day != "Saturday" ]; then archive_file="$hostname-$day.tgz" else archive_file=$week_file fi # -------------- Setup variables for the archive filename.------------- END// echo "[--------------------------------[`date +%F--%H-%M`]--------------------------------]" echo "[----------][`date +%F--%H-%M`] Start backup..." echo "[++--------][`date +%F--%H-%M`] Generate a database backup..." innobackupex --defaults-file=/etc/mysql/my.cnf --user=root --password=password $DATADIR/$PREFIX --no-timestamp 2> /dev/null if [[ $? -gt 0 ]];then echo "[++--------][`date +%F--%H-%M`] Aborted. Generate database backup failed." exit 1 fi echo "[+++-------][`date +%F--%H-%M`] Backup successfull." echo "[++++------][`date +%F--%H-%M`] 7z'ing backup..." #7z a $DATADIR/7z/$BK_NAME.$PREFIX.7z $DATADIR/$PREFIX > /dev/null 2>&1 7z a $DATADIR/7z/$BK_NAME.$archive_file.7z $DATADIR/$PREFIX > /dev/null 2>&1 if [[ $? -gt 0 ]];then echo "[++++------][`date +%F--%H-%M`] Aborted. 7z'ing failed." exit 1 fi echo "[+++++-----][`date +%F--%H-%M`] 7z'ing successfull." echo "[++++++----][`date +%F--%H-%M`] Stat datadir space (USED): `du -h $DATADIR | tail -n1`" echo "[+++++++---][`date +%F--%H-%M`] Free HDD space: `df -h /home|tail -n1|awk '{print $4}'`" echo "[++++++++--][`date +%F--%H-%M`] Removing old backups..." find $DATADIR -mtime +30 -exec rm {} \; -print exit 0
Часть скрипта позаимствована тут.