Git 用于备份Web服务器和MySQL数据库的Mac/UNIX Shell脚本的错误处理
我为自己编写了一个shell脚本来备份我们的web服务器,并使其正常工作。它导航到备份目录,执行rsync、mysqldump,将所有内容提交到git存储库进行版本控制,并将其推送到远程git服务器。我已经了解到,编写计划的UNIX脚本有很多陷阱,所以我想知道我是否遗漏了什么,或者为自己设置了灾难。在整个过程中,我如何让it通过电子邮件通知我任何错误Git 用于备份Web服务器和MySQL数据库的Mac/UNIX Shell脚本的错误处理,git,bash,shell,unix,Git,Bash,Shell,Unix,我为自己编写了一个shell脚本来备份我们的web服务器,并使其正常工作。它导航到备份目录,执行rsync、mysqldump,将所有内容提交到git存储库进行版本控制,并将其推送到远程git服务器。我已经了解到,编写计划的UNIX脚本有很多陷阱,所以我想知道我是否遗漏了什么,或者为自己设置了灾难。在整个过程中,我如何让it通过电子邮件通知我任何错误 #!/bin/bash #Exit on any error set -e LOGFILE=~/backups/web_backup-$(da
#!/bin/bash
#Exit on any error
set -e
LOGFILE=~/backups/web_backup-$(date +"%m.%d.%Y_%H.%M.%S").log
touch $LOGFILE
echo "Backup started for "$(date +"%m.%d.%Y_%H.%M.%S") | tee -a $LOGFILE
############################################################
###### BACKUP WEBSITE
############################################################
echo "Backing up Website..." 2>&1 | tee -a $LOGFILE
cd ~/backups/example.org
if [ "$?" != "0" ]; then
echo "Cannot change directory!" 1>&2
exit 1
fi
rsync -Pav --size-only --delete --filter='P .git' --filter='P example.com.sql' backups@example.com:/path/to/example.org/ ~/backups/example.org/ 2>&1 | tee -a $LOGFILE
echo "Backing up Example.com DB..." 2>&1 | tee -a $LOGFILE
ssh backups@example.com "mysqldump -u user -hlocalhost -ppassword dbname > example.com.sql
echo "Adding files to Git Repository..." 2>&1 | tee -a $LOGFILE
git add . 2>&1 | tee -a $LOGFILE| tee -a $LOGFILE
echo "Commiting files to Git Repository..." 2>&1 | tee -a $LOGFILE
git commit -m "Backup as of "$(date +"%m.%d.%Y_%H.%M.%S") 2>&1 | tee -a $LOGFILE
echo "Pusing Git Repo to Remote Location..." 2>&1 | tee -a $LOGFILE
git push origin master 2>&1 | tee -a $LOGFILE
echo "Finished backing up Website..." 2>&1 | tee -a $LOGFILE
echo "Backup Finished for "$(date +"%m.%d.%Y_%H.%M.%S")
exit
在脚本中声明一个报告文件,并将所有输出定向到该文件。检查copy和rsynch命令的退出状态($?),如果退出状态不等于零,则使用mail命令将邮件发送到报告文件的地址
mail -s "subject " -c "cc address" "to-address" < reportfile name
mail-s“subject”-c“cc address”to address”