最近做服务器运维,用rsync进行两机备份的话比较好,对数据保全比较好。因此写了这个脚本,加进crontab当中,定期对服务器备份数据库以及www目录下的网站。 脚本如下,使用前请先针对自己的服务器环境设置变量值,错误之处还请指出,谢谢!原创脚本,转载请
最近做服务器运维,用rsync进行两机备份的话比较好,对数据保全比较好。因此写了这个脚本,加进crontab当中,定期对服务器备份数据库以及www目录下的网站。
脚本如下,使用前请先针对自己的服务器环境设置变量值,错误之处还请指出,谢谢!原创脚本,转载请保留原作者信息!
[bash]
#/bin/bash
# This script is used to do backup and rsync works for servers.
# ./backup.sh (db_rsync|www_rsync)
# By Chen Zhidong
# http://sillydong.com
# 获取可执行文件路径
MYSQL=”$(which mysql)”
MYSQLDUMP=”$(which mysqldump)”
GZIP=”$(which gzip)”
#定义数据库dump目录
DB_DUMP=”/home/www/database/”
#定义MySQL认证
DB_USER=”"
DB_PASS=”"
DB_HOST=”127.0.0.1″
#定义www目录
LOCAL_WWW=”"
RSYNC_WWW=”"
#定义本地rsync密码文件位置
PASSFILE=”"
RSYNC_NAME=”"
RSYNC_IP=”"
#获取本地AccessIP用作后面rsync的文件夹
IP=`ifconfig | grep “venet0:0″ –after-context=1 | sed -n ‘s/..*addr:\(..*\) P-t-P..*/\1/p’ | sed ‘/127.0.0.1/d’`
function db_dump{
DBS=”$($MYSQL -u’$DB_USER’ -h $DB_HOST -p’$DB_PASS’ -Bse ‘show databases’)”
for db in $DBS
do
case $db in
“information_schema”)
;;
“mysql”)
;;
“test”)
;;
*)
echo “Dump $db begin at $(date)” >> $DB_DUMP/db_dump.log
$MYSQLDUMP –opt $db -u’$DB_USER’ -h $DB_HOST -p’$DB_PASS’ > $DB_DUMP/$(date +%F)-$db.sql
esac
done
echo “Dump $db done at $(date)” >> $DB_DUMP/db_dump.log
}
function db_rsync{
echo “db_rsync begin at $(date)!” >> db_rsync.log
rsync -rvlHpogDtS –delete –password-file=$PASSFILE /home/database –exclude *\log\* –exclude *\backup\* –exclude *.rar –exclude *.zip rsync://$RSYNC_NAME@$RSYNC_IP/$IP_db_lio >> $DB_DUMP/db_rsync.log
echo “db_rsync done at $(date)!” >> db_rsync.log
}
function www_rsync{
echo “www_rsync begin at $(date)!” >> www_rsync.log
rsync -rvlHpogDtS –delete –password-file=$PASSFILE /home/www –exclude *\log\* –exclude *\backup\* –exclude *.rar –exclude *.zip rsync://$RSYNC_NAME@$RSYNC_IP/$IP_www_lio >> $DB_DUMP/db_rsync.log
echo “www_rsync done at $(date)!” >> www_rsync.log
}
case $1 in
db_rsync)
db_dump 2>> /home/rsync_error.log
db_rsync 2>> /home/rsync_error.log
;;
www_rsync)
www_rsync 2>> /home/rsync_error.log
;;
*)
echo “$(date)You didn’t add parameter in your crontab.” >> crontab.log
echo “Syntax: backup.sh (db_rsync|www_rsync)” >> crontab.log
;;
esac
[/bash]
另有一脚本,用于在备份机上rsyncd.conf中添加记录,如下:
[bash]
#/bin/bash
# ./rsyncd.sh IP
# for db
echo “”
echo “[$1_db_lio]” >> /etc/rsyncd.conf
echo “path = /backup/mysql/$1″ >> /etc/rsyncd.conf
echo “uid = root” >> /etc/rsyncd.conf
echo “gid = root” >> /etc/rsyncd.conf
echo “read only = false” >> /etc/rsyncd.conf
echo “ignore errors” >> /etc/rsyncd.conf
echo “secrets file = /etc/rsyncd.pas” >> /etc/rsyncd.conf
echo “host allow $1″ >> /etc/rsyncd.conf
#for www
echo “”
echo “[$1_www_lio]” >> /etc/rsyncd.conf
echo “path = /backup/vhosts/rsyncfiles/$1″ >> /etc/rsyncd.conf
echo “uid = root” >> /etc/rsyncd.conf
echo “gid = root” >> /etc/rsyncd.conf
echo “read only = false” >> /etc/rsyncd.conf
echo “ignore errors” >> /etc/rsyncd.conf
echo “secrets file = /etc/rsyncd.pas” >> /etc/rsyncd.conf
echo “host allow $1″ >> /etc/rsyncd.conf
tail -n 17 /etc/rsyncd.conf
[/bash]
原文地址:用rsync进行两机备份的shell脚本, 感谢原作者分享。