#!/bin/bash ############################################### ############################################### ## This script depends on the "tar", "zip", ## ## "rsync", "mysqldump", "lsb" and "mutt" ## ## packages. ## ## ## ## Make sure they are installed on the ## ## system. It is also necessary to configure ## ## an encrypted key for passwordless ## ## connection between the servers. ## ## ## ## This script was created by expert ## ## Henrique Fagundes. Please respect ## ## intellectual property. If you are ## ## going to use it, keep the due credits. ## ############################################### ############################################### ### Backup configuration data filename="ZABBIX" origin_path="/bkp/data" backup_server="192.168.0.1" source_disk="/dev/sda1" destination_path="/mnt/dados/backup" destination_disk="/dev/md1" backup_content="/srv /root /etc /usr/local/bin" retention_days="7" ### Settings for MySQL Server Backup. For the script ### to execute the database server backup, the ### "mysql_backup" variable must be set to "yes". mysql_backup="yes" dump_path="/bkp/dumps" mysql_host="localhost" mysql_user="root" mysql_pwd="SuaSenhaDificil" mysql_port="3306" ### Data for sending emails recipient="seu@email.com.br" ############################################ ############################################ ## Caution! Making any changes from this ## ## point forward may break the backup ## ## script. ## ############################################ ############################################ backup_file_date=$(date +%Y-%m-%d).tar.gz backup_directory_date=$(date +%Y/%m/%d) dump(){ find $dump_path/ -empty -exec rm -rf {} \; rm -r $dump_path/$(date +%Y/%m/%d -d "-1 days") 2>&- 1>&- mkdir -p $dump_path/$backup_directory_date databases=`mysql --host=$mysql_host \ --user=$mysql_user \ --password=$mysql_pwd \ --port=$mysql_port \ -e "SHOW DATABASES;" | tr -d "| " | grep -v Database` for db in $databases; do if [[ "$db" != "information_schema" && "$db" != "performance_schema" ]] && [[ "$db" != _* ]] ; then echo ; echo "Dumping database: $db" mysqldump --host=$mysql_host \ --user=$mysql_user \ --password=$mysql_pwd \ --port=$mysql_port \ --force --opt --single-transaction --events --routines --triggers \ --verbose $db > $dump_path/$backup_directory_date/$db.sql gzip -f $dump_path/$backup_directory_date/$db.sql fi done } exec_backup_with_mysql(){ find $origin_path -maxdepth 1 -mindepth 1 -type f -ctime +$retention_days -exec rm -rf {} \; tar -zcvf $origin_path/$filename-$backup_file_date $dump_path/$backup_directory_date $backup_content } exec_backup_without_mysql(){ find $origin_path -maxdepth 1 -mindepth 1 -type f -ctime +$retention_days -exec rm -rf {} \; tar -zcvf $origin_path/$filename-$backup_file_date $backup_content } if [ "`echo $mysql_backup`" != "yes" ]; then echo "This server does not have MySQL Server" > /tmp/dump.txt exec_backup_without_mysql 2>&1 >> /tmp/backup.txt 2>&1 else dump 2>&1 >> /tmp/dump.txt 2>&1 exec_backup_with_mysql 2>&1 >> /tmp/backup.txt 2>&1 fi exec_rsync(){ rsync --delete -avp $origin_path/ $backup_server:$destination_path/ } exec_rsync 2>&1 >> /tmp/rsync.txt 2>&1 md5sum $origin_path/$filename-$backup_file_date | awk '{print $1}' > /tmp/md5_source.txt ssh $backup_server md5sum $destination_path/$filename-$backup_file_date | awk '{print $1}' > /tmp/md5_destination.txt du -h $origin_path/$filename-$backup_file_date > /tmp/source_size.txt ssh $backup_server du -h $destination_path/$filename-$backup_file_date > /tmp/destination_size.txt create_mail(){ cat > /tmp/mail.txt << EOF Prezados, Esse e-mail contem os logs do backup do servidor $(hostname) no dia $(date +%d/%m/%Y). Voce deve conferir atentamente o resultado da verificação MD5 e os logs anexos para verificar se tudo saiu conforme o esperado. MD5 do arquivo de origem: `cat /tmp/md5_source.txt` `cat /tmp/source_size.txt` O espaço total do servidor de origem é `df -lh $source_disk | grep -E -v "Filesystem|Sist." | awk '{print $2}'`, encontra-se com `df -lh $source_disk | grep -E -v "Filesystem|Sist." | awk '{print $5}'` do seu espaço ocupado e com `df -lh $source_disk | grep -E -v "Filesystem|Sist." | awk '{print $4}'` de espaço livre. MD5 do arquivo de destino: `cat /tmp/md5_destination.txt` `cat /tmp/destination_size.txt` O espaço total do servidor de destination é `ssh $backup_server df -h $destination_disk | grep -E -v "Filesystem|Sist." | awk '{print $2}'`, encontra-se com `ssh $backup_server df -h $destination_disk | grep -E -v "Filesystem|Sist." | awk '{print $5}'` do seu espaço ocupado e com `ssh $backup_server df -h $destination_disk | grep -E -v "Filesystem|Sist." | awk '{print $4}'` de espaço livre. Essa e uma menssagem automatica, por favor nao responda! Atenciosamente, Servidor `lsb_release -d | cut -d':' -f2 | xargs` Kernel version `uname -r` EOF } clear_temps(){ rm -rf /tmp/backup.zip /tmp/backup.txt \ /tmp/dump.zip /tmp/dump.txt /tmp/rsync.zip \ /tmp/rsync.txt /root/sent /tmp/md5_source.txt \ /tmp/md5_destination.txt /tmp/source_size.txt \ /tmp/destination_size.txt /tmp/mail.txt } if [ "`cat /tmp/md5_source.txt`" != "`cat /tmp/md5_destination.txt`" ]; then subject_bkp_error="O backup do $(hostname -s) no dia $(date +%d/%m/%Y) FALHOU. O MD5 encontra-se diferente." zip -r /tmp/backup.zip /tmp/backup.txt zip -r /tmp/dump.zip /tmp/dump.txt zip -r /tmp/rsync.zip /tmp/rsync.txt echo "$subject_bkp_error" | mutt -s "$subject_bkp_error" -a /tmp/backup.zip /tmp/dump.zip -- $recipient clear_temps 2>&- 1>&- else create_mail subject_bkp_ok="O backup do $(hostname -s) para o $(ssh $backup_server hostname -s) foi concluído com sucesso no dia $(date +%d/%m/%Y) às $(date +%H:%M:%S) hs" zip -r /tmp/backup.zip /tmp/backup.txt zip -r /tmp/dump.zip /tmp/dump.txt zip -r /tmp/rsync.zip /tmp/rsync.txt cat /tmp/mail.txt | mutt -s "$subject_bkp_ok" -a /tmp/backup.zip /tmp/dump.zip -- $recipient clear_temps 2>&- 1>&- fi