一、添加SMTP服务器
bin/mail会默认使用本地sendmail发送邮件,而通过修改配置文件可以使用外部SMTP服务器,可以达到不使用sendmail而用外部的smtp服务器发送邮件的目的。
在需要发送自动报警的服务器上修改如下文件,增加如下两行
# vim /etc/mail.rc set from=doteyplay@126.com smtp=smtp.126.com set smtp-auth-user=doteyplay smtp-auth-password=*** smtp-auth=login
使用redhat的时候,在/etc/mail.rc里改了一下外部smtp的设置,就可以使用外部邮箱发邮件了,但在ubuntu下可能没有/etc/mail.rc这个文件 buntu上: ubunut上mailx这个命令对应的deb包有bsd-mailx bsd-mailx对应的配置文件是/etc/mail.rc跟centos一样 apt-get install sendmail apt-get install mailutils apt-get install bsd-mailx
有时,设置完上面的,当echo一卦邮件时,接收邮箱仍然不能接收到邮件,此时可能并不是服务器上设置有问题,而是被反垃圾邮件给做掉了,如果用126的smtp服务器,发给腾讯和sina,就会出现550的错误(在服务器上mail中查看),腾讯说是群发了大量邮件,即使你新申请一个,依然是这个报错。而sina就很直接了“退信原因:外发IP历史信誉度低”,很有可能是之前测试一些什么太多了,所以导致了这个问题。然而直接发给126的就没有问题。所以,单位测试环境有问题,部到线上的时候不一定有问题
当然,这里的SMTP服务器也可以使用别的,比如QQ的,但是QQ只能发几卦,在测试的时候,总报错:smtp-server: 454 Error: authentication failed, system busy,万恶的TX啊!所以,后来就改126的SMTP服务器了,用起来还是相当不错的。对于邮箱嘛,就用139邮箱吧,因为有短信提醒,同样,在测试的时候,QQ邮箱那个慢呀……
二、禅道自动备份脚本
1、自动备份脚本
#vim chandao_backup.sh #!/bin/bash #Auto backup for chandao #Date: first write on 2014.7.22 #Vertion: v1.1 date=`date +%Y-%m-%d` tarball="tar -jcf /opt/backup/chandao-$date.tar.bz2 chandao/" user1=13910892224@139.com user2=15048088251@139.com if [ ! -d "/opt/backup/" ]; then mkdir -p /opt/backup/ fi cd /var/www/html/ && $tarball judge=`echo $?` if [ $judge == 0 ];then echo "backup is ok" | mail -s "Auto backup for chandao" $user1 $user2; else echo "backup is failure" | mail -s "Auto backup for chandao" $user1 $user2; fi find /opt/backup/ -type f -mtime +30 -name "*.bz2" -exec rm -rf {} \;
2、赋予脚本执行权限
chmod +x chandao_backup.sh
2、任务计划自动执行
#crontab -e 0 0 * * * /bin/sh /opt/scripts/chandao_backup.sh > /dev/null 2>&1
三、数据库自动备份脚本
1、自动备份脚本
#vim db_backup.sh #!/bin/bash #Auto backup for mariadb #Date: first write on 2014.7.22 #Vertion: v1.1 date=`date +%Y-%m-%d` datadir="tar -jcf /opt/backup/datadir-$date.tar.bz2 /data" #alldb="mysqldump --all-databases > /opt/backup/alldb-$date.sql" #zentaodb="mysqldump --databases zentao > /opt/backup/zentao-$date.sql" user1=13910892224@139.com user2=15048088251@139.com if [ ! -d "/opt/backup/" ]; then mkdir -p /opt/backup/ fi mysqldump --all-databases > /opt/backup/alldb-$date.sql && mysqldump --databases zentao > /opt/backup/zentao-$date.sql judge=`echo $?` if [ $judge == 0 ];then cd /opt/backup/ && tar -jcf ALLDB-$date.tar.bz2 alldb-$date.sql zentao-$date.sql --remove-files $datadir; judge1=`echo $?` if [ $judge1 == 0 ];then echo "Backup is ok!" | mail -s "Auto backup for db" $user1 $user2; else echo "Db is ok but datadir is failure!" | mail -s "Auto backup for db" $user1 $user2; fi else echo "Backup is failure,because of db backup is faild!" | mail -s "Auto backup for db" $user1 $user2; fi find /opt/backup/ -type f -mtime +30 -name "*.bz2" -exec rm -rf {} \;
2、赋予脚本执行权限
chmod +x db_backup.sh
3、任务计划自动执行
#crontab -e 0 0 * * * /bin/sh /opt/scripts/db_backup.sh > /dev/null 2>&1
四、更新脚本版本:
数据库备份脚本v1.2
#!/bin/bash #Auto backup for mariadb #Date: first write on 2014.7.22 #Vertion: v1.2 date=`date +%Y-%m-%d` user1=13910892224@139.com user2=15048088251@139.com main(){ tar -jcf /opt/backup/datadir-$date.tar.bz2 /data; if [ $? -eq 0 ];then /usr/local/mysql/bin/mysqldump --all-databases > /opt/backup/alldb-$date.sql; if [ $? -eq 0 ];then sleep 2 /usr/local/mysql/bin/mysqldump --databases zentao > /opt/backup/zentao-$date.sql; if [ $? -eq 0 ];then cd /opt/backup/ tar -jcf ALLDB-$date.tar.bz2 alldb-$date.sql zentao-$date.sql --remove-files \ && echo "Backup is ok!" | mail -s "Auto backup for db" $user2; else echo "Zentao db backup failure" | mail -s "Auto backup for db" $user2; fi else echo "All databases backup failure" | mail -s "Auto backup for db" $user2; fi else echo "Datadir backup failure,it's first step" | mail -s "Auto backup for db" $user2; fi find /opt/backup/ -type f -mtime +30 -name "*.bz2" -exec rm -rf {} \; } if [ ! -d "/opt/backup/" ]; then mkdir -p /opt/backup/; main; else main; fi
转载于:https://blog.51cto.com/nmshuishui/1529829