1.Резервное копирование файлов (сайты, конфигурационные файлы)
1 |
# chown root:root /usr/local/scripts/backup-files-shell.sh |
1 |
# chmod 751 /usr/local/scripts/backup-files-shell.sh |
1 |
# nano /usr/local/scripts/backup-files-shell.sh |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
#!/bin/bash # Формат даты DATE="$(date +%Y-%m-%d)" # Каталог для хранения бекапов STORAGEDIR="/backup" # Список сайтов для копирования SITES='/home/users' # Список катадогов,исключенных из бекапа EXCLUDE1="/home/users/<user_to_exclude>" EXCLUDE2="/home/users/<username>/<sitename>/<folder_to_exclude>" #Cписок конфигурационных файлов и других служебных файлов(Nagios,Cacti,Munin) CONFIGS='/etc /usr/local/nagios /usr/lib64/nagios/plugins /usr/share/cacti /var/lib/cacti /var/www/html' # Журналируем ошибки в файл LOGFILE="/var/log/backup.log" # Как долго(кол-во дней) хранить бекапы LIMITTIME="+14" NICE="$(which nice)" IONICE="$(which ionice)" TAR="$(which tar)" FIND="$(which find)" RM="$(which rm)" ECHO="$(which echo)" [ ! -d $STORAGEDIR/sites ] && mkdir -p $STORAGEDIR/sites [ ! -d $STORAGEDIR/configs ] && mkdir -p $STORAGEDIR/configs $NICE -n 19 $IONICE -c2 -n7 $TAR -czf $STORAGEDIR/sites/sites-${DATE}.tar.gz $SITES --exclude=$EXCLUDE1 --exclude=$EXCLUDE2 exitcode=$? if [ "$exitcode" != "1" ] && [ "$exitcode" != "0" ]; then $ECHO "Backup sites is failed - $DATE" | tee -a $LOGFILE fi $NICE -n 19 $IONICE -c2 -n7 $TAR -czf $STORAGEDIR/configs/configs-${DATE}.tar.gz $CONFIGS exitcode=$? if [ "$exitcode" != "1" ] && [ "$exitcode" != "0" ]; then $ECHO "Backup configs is failed - $DATE" | tee -a $LOGFILE fi $NICE -n 19 $IONICE -c2 -n7 $TAR -cf $STORAGEDIR/backup-files-${DATE}.tar $STORAGEDIR/sites/*.tar.gz $STORAGEDIR/configs/*.tar.gz exitcode=$? if [ "$exitcode" != "1" ] && [ "$exitcode" != "0" ]; then $ECHO "Tar sites and configs is failed - $DATE" | tee -a $LOGFILE else $RM -rf $STORAGEDIR/sites/* $STORAGEDIR/configs/* $ECHO "Backup Sites and Configs is done! Backup up to $DATE " | tee -a $LOGFILE fi # Удаляем бекапы старше 14 дней $FIND $STORAGEDIR -maxdepth 1 -type f -name '*.tar' -mtime $LIMITTIME -exec $RM -rf {} \; |
2.Резервное копирование баз данных
1 |
# chown root:root /usr/local/scripts/backup-databases-shell.sh |
1 |
# chmod 751 /usr/local/scripts/backup-databases-shell.sh |
1 |
# nano /usr/local/scripts/backup-databases-shell.sh |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
#!/bin/bash # Формат даты DATE="$(date +"%Y-%m-%d")" # Место хранения бекапов STORAGEDIR='/backup' # Пользователь,пароль,хост для подключения MUSER="root" MPASS="123" MHOST="localhost" # Журналируем ошибки и успешное выполнение бекапа в файл LOGFILE="/var/log/backup.log" # Как долго(кол-во дней) хранить бекапы LIMITTIME="+14" MYSQL="$(which mysql)" MYSQLDUMP="$(which mysqldump)" NICE="$(which nice)" IONICE="$(which ionice)" GZIP="$(which gzip)" TAR="$(which tar)" FIND="$(which find)" MKDIR="$(which mkdir)" ECHO="$(which echo)" RM="$(which rm)" [ ! -d $STORAGEDIR/mysql ] && $MKDIR -p $STORAGEDIR/mysql DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases' | egrep -v '(information_schema|performance_schema)')" for db in $DBS do FILE=$STORAGEDIR/mysql/$db-$DATE.sql.gz $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS -EKR --single-transaction $db | $GZIP -c > $FILE if [ ${PIPESTATUS[0]} != "0" ]; then $ECHO "Backup database is failed $db - $DATE" | tee -a $LOGFILE fi done cd $STORAGEDIR/mysql $NICE -n 19 $IONICE -c2 -n7 $TAR -cf $STORAGEDIR/backup-databases-${DATE}.tar *.sql.gz exitcode=$? if [ "$exitcode" != "1" ] && [ "$exitcode" != "0" ]; then $ECHO "Tar databases is failed - $DATE" | tee -a $LOGFILE else $RM -rf $STORAGEDIR/mysql/* $ECHO "Backup Databases is done! Backup up to $DATE " | tee -a $LOGFILE fi $FIND $STORAGEDIR -maxdepth 1 -type f -name '*.tar' -mtime $LIMITTIME -exec $RM -rf {} \; |
3. Резервное копирование файлов и баз данных
1 |
# chmod 751 /usr/local/scripts/backup-all-shell.sh |
1 |
# chown root:root /usr/local/scripts/backup-all-shell.sh |
1 |
# nano /usr/local/scripts/backup-all-shell.sh |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
#!/bin/bash # Формат даты DATE="$(date +%Y-%m-%d)" # Каталог для хранения бекапов STORAGEDIR="/backup" # Список сайтов для копирования SITES='/home/users' EXCLUDE1="/home/users/<user_to_exclude>" EXCLUDE2="/home/users/<username>/<sitename>/<folder_to_exclude>" #Cписок конфигурационных файлов и других служебных файлов(Nagios,Cacti,Munin) CONFIGS='/etc /usr/local/nagios /usr/lib64/nagios/plugins /usr/share/cacti /var/lib/cacti /var/www/html' # Журналируем ошибки и успешное выполнение бекапа в файл LOGFILE="/var/log/backup.log" # Как долго(кол-во дней) хранить бекапы LIMITTIME="+14" # guess binary names MYSQL="$(which mysql)" MYSQLDUMP="$(which mysqldump)" NICE="$(which nice)" IONICE="$(which ionice)" GZIP="$(which gzip)" TAR="$(which tar)" FIND="$(which find)" MKDIR="$(which mkdir)" ECHO="$(which echo)" RM="$(which rm)" # Пользователь,пароль,хост для подключения MUSER="root" MPASS="123" MHOST="localhost" [ ! -d $STORAGEDIR/sites ] && $MKDIR -p $STORAGEDIR/sites [ ! -d $STORAGEDIR/configs ] && $MKDIR -p $STORAGEDIR/configs [ ! -d $STORAGEDIR/mysql ] && $MKDIR -p $STORAGEDIR/mysql $NICE -n 19 $IONICE -c2 -n7 $TAR -czf $STORAGEDIR/sites/sites-${DATE}.tar.gz $SITES --exclude=$EXCLUDE1 --exclude=$EXCLUDE2 exitcode=$? if [ "$exitcode" != "1" ] && [ "$exitcode" != "0" ]; then $ECHO "Backup sites is failed - $DATE" | tee -a $LOGFILE fi $NICE -n 19 $IONICE -c2 -n7 $TAR -czf $STORAGEDIR/configs/configs-${DATE}.tar.gz $CONFIGS exitcode=$? if [ "$exitcode" != "1" ] && [ "$exitcode" != "0" ]; then $ECHO "Backup configs is failed - $DATE" | tee -a $LOGFILE fi DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases' | egrep -v '(information_schema|performance_schema)')" for db in $DBS do FILE=$STORAGEDIR/mysql/$db-${DATE}.sql.gz $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS -EKR --single-transaction $db | $GZIP -c > $FILE if [ ${PIPESTATUS[0]} != "0" ]; then $ECHO "Backup database is failed $db - $DATE" | tee -a $LOGFILE fi done $NICE -n 19 $IONICE -c2 -n7 $TAR -cf $STORAGEDIR/backup-all-${DATE}.tar $STORAGEDIR/sites/*.tar.gz $STORAGEDIR/configs/*.tar.gz $STORAGEDIR/mysql/*.sql.gz exitcode=$? if [ "$exitcode" != "1" ] && [ "$exitcode" != "0" ]; then $ECHO "Tar sites,config,databases is failed - $DATE" | tee -a $LOGFILE else $RM -rf $STORAGEDIR/sites/* $STORAGEDIR/configs/* $STORAGEDIR/mysql/* $ECHO "Backup sites,configs,databases is done! Backup up to $DATE " | tee -a $LOGFILE fi # Удаляем бекапы старше 14 дней $FIND $STORAGEDIR -maxdepth 1 -type f -name '*.tar' -mtime $LIMITTIME -exec $RM -rf {} \; |
Мониторинг лога с бекапом с помощью Monit/Nagios
Monit(https://kamaok.org.ua/?p=361)
1 |
# nano /etc/monit.conf |
1 2 3 4 5 6 7 |
check file backup with path /var/log/backup.log if match 'Backup sites is failed' then alert if match 'Backup configs is failed' then alert if match 'Tar sites and configs is failed' then alert if match 'Tar sites,config,databases is failed' then alert if match 'Backup database is failed' then alert if match 'Tar databases is failed' then alert |
Nagios(https://kamaok.org.ua/?p=1487)
1 |
# nano /etc/nagios/log-files.cfg |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$seekfilesdir = '/var/tmp/check_logfiles'; # where the state information will be saved. $protocolsdir = '/tmp'; # where protocols with found patterns will be stored. $scriptpath = '/var/tmp'; # where scripts will be searched for. @searches = ( { tag => 'backup', logfile => '/var/log/backup.log', rotation => 'centos', warningpatterns => ['Backup sites is failed', 'Backup configs is failed', 'Tar sites and configs is failed', 'Tar sites,config,databases is failed', 'Backup database is failed', 'Tar databases is failed'], } ); |
4.Добавление в cron задания на ежедневный бекап
1 |
# nano /etc/cron.d/backup |
1 2 3 4 |
# Закомментируйте все за исключением того, что будет бекапить 10 4 * * * root /usr/local/scripts/backup-all-shell.sh > /dev/null 2>&1 # 10 4 * * * root /usr/local/scripts/backup-databases-shell.sh > /dev/null 2>&1 # 10 4 * * * root /usr/local/scripts/backup-files-shell.sh > /dev/null 2>&1 |
Если не используются Monit/Nagios для мониторинга логов, то можно отправлять оповещения об успешном выполнении/ошибках в самом скрипте
1 |
# chmod 751 /usr/local/scripts/scripts/backup-all-mail-shell.sh |
1 |
# chown root:root /usr/local/scripts/scripts/backup-all-mail-shell.sh |
1 |
# nano /usr/local/scripts/backup-all-mail-shell.sh |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
#!/bin/bash # Формат даты DATE="$(date +%Y-%m-%d)" # Каталог для хранения бекапов STORAGEDIR="/backup" # Список сайтов для копирования SITES='/home/users' EXCLUDE1="/home/users/<users_to_exclude>" EXCLUDE2="/home/users/<username>/<sitename><path_to_exclude>" #Cписок конфигурационных файлов и других служебных файлов(Nagios,Cacti,Munin) CONFIGS='/etc /usr/local/nagios /usr/lib64/nagios/plugins /usr/share/cacti /var/lib/cacti /var/www/html' # Журналируем ошибки и успешное выполнение бекапа в файл LOGFILE="/var/log/backup.log" # Как долго(кол-во дней) хранить бекапы LIMITTIME="+14" # guess binary names MYSQL="$(which mysql)" MYSQLDUMP="$(which mysqldump)" NICE="$(which nice)" IONICE="$(which ionice)" GZIP="$(which gzip)" TAR="$(which tar)" FIND="$(which find)" MKDIR="$(which mkdir)" ECHO="$(which echo)" RM="$(which rm)" MAIL="$(which mail)" MAILADDR="my@email" # Пользователь,пароль,хост для подключения MUSER="root" MPASS="123" MHOST="localhost" [ ! -d $STORAGEDIR/sites ] && $MKDIR -p $STORAGEDIR/sites [ ! -d $STORAGEDIR/configs ] && $MKDIR -p $STORAGEDIR/configs [ ! -d $STORAGEDIR/mysql ] && $MKDIR -p $STORAGEDIR/mysql $NICE -n 19 $IONICE -c2 -n7 $TAR -czf $STORAGEDIR/sites/sites-${DATE}.tar.gz $SITES --exclude=$EXCLUDE1 --exclude=$EXCLUDE2 exitcode=$? if [ "$exitcode" != "1" ] && [ "$exitcode" != "0" ]; then $ECHO "Backup sites is failed - $DATE" | $MAIL -s "Backup sites is failed - $DATE" $MAILADDR fi $NICE -n 19 $IONICE -c2 -n7 $TAR -czf $STORAGEDIR/configs/configs-${DATE}.tar.gz $CONFIGS exitcode=$? if [ "$exitcode" != "1" ] && [ "$exitcode" != "0" ]; then $ECHO "Backup configs is failed - $DATE" | $MAIL -s "Backup configs is failed - $DATE" $MAILADDR fi DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases' | egrep -v '(information_schema|performance_schema)')" for db in $DBS do FILE=$STORAGEDIR/mysql/$db-${DATE}.sql.gz $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS -EKR --single-transaction $db | $GZIP -c > $FILE if [ ${PIPESTATUS[0]} != "0" ]; then $ECHO "Backup database is failed $db - $DATE" | $MAIL -s "Backup database is failed $db - $DATE" $MAILADDR fi done $NICE -n 19 $IONICE -c2 -n7 $TAR -cf $STORAGEDIR/backup-all-${DATE}.tar $STORAGEDIR/sites/*.tar.gz $STORAGEDIR/configs/*.tar.gz $STORAGEDIR/mysql/*.sql.gz exitcode=$? if [ "$exitcode" != "1" ] && [ "$exitcode" != "0" ]; then $ECHO "Tar sites,config,databases is failed - $DATE" | $MAIL -s "Tar sites,config,databases is failed - $DATE" $MAILADDR else $RM -rf $STORAGEDIR/sites/* $STORAGEDIR/configs/* $STORAGEDIR/mysql/* $ECHO "Backup sites,configs,databases is done! Backup up to $DATE " | $MAIL -s "Backup sites,configs,databases is done - $DATE" $MAILADDR fi # Удаляем бекапы старше 14 дней $FIND $STORAGEDIR -maxdepth 1 -type f -name '*.tar' -mtime $LIMITTIME -exec $RM -rf {} \; |