Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
130 lines (100 sloc) 3.29 KB
#!/bin/bash
# This script is used to do backup and rsync works for servers. Please finish the config part first before using it.
# By Chen Zhidong
# http://sillydong.com
# Email:njutczd+gmail.com
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH
#########config begin####################
BINPATH="/usr/local/mysql/bin"
ROOTDIR="/root"
DAY_WEEK="5"
DAY_MONTH="26"
DB_DUMP="/home/dbdump/"
LOCAL_WWW="/home/www/"
DB_USER=
DB_PASS=
DB_HOST=
PASSFILE="/etc/rsyncd.pas"
RSYNC_NAME="zzz"
RSYNC_IP="yyy.yyy.yyy.yyy"
TAG=$(date +%Y%m%d)
#pay attention to the sed part of this sentence, check it before using!
IPS=`ifconfig | sed '/Link/d' | sed '/127.0.0.1/d' | grep addr | awk '{print $2}' | sed 's/addr://g' | sed '/^$/d'`
IP=`echo $IPS | sed 's/ .*//'`
#########config end######################
readset(){
grep $1 $2 | sed "s/$1=//"
}
db_dump(){
echo "dumping $1 at $(date)!"
$BINPATH/mysqldump --opt $1 --user=$DB_USER --host=$DB_HOST --password=$DB_PASS > $DB_DUMP/${1}_${TAG}.sql
echo "finish dumping $1 at $(date)"
}
db_rsync(){
#echo "Doing db_rsync..."
echo "db_rsync begin at $(date)!"
rsync -auvHzS --password-file=$PASSFILE $DB_DUMP rsync://$RSYNC_NAME@$RSYNC_IP/${IP}_db_lio
echo "db_rsync done at $(date)!"
}
www_rsync(){
srcdir=`echo "$1" | sed "s/\/tar.set$//"`
#echo "$srcdir"
echo "www_rsync begin at $(date)!"
rsync -auvHzS --delete --password-file=$PASSFILE --exclude */backups/* --exclude */cache/* --exclude */logs/* --exclude */log/* --exclude */templates_c/* --exclude */errors/* --exclude */Thumbs.db $srcdir rsync://$RSYNC_NAME@$RSYNC_IP/${IP}_www_lio
echo "www_rsync done at $(date)!"
}
#################### Script begin here ##########################
if [ -e $ROOTDIR/rsyncback_$(date +%Y%m%d --date="5 days ago").log ];then
rm -f $ROOTDIR/rsyncback_$(date +%Y%m%d --date="5 days ago").log
fi
if [ -d $DB_DUMP ];then
rm -rf $DB_DUMP
fi
mkdir $DB_DUMP
echo -e "=========================================\nRsyncback work begin at $(date)" >> $ROOTDIR/rsyncback_${TAG}.log
echo "IP: $IP" >> $ROOTDIR/rsyncback_${TAG}.log
#rsync the /home/www
LIST=`find $LOCAL_WWW -maxdepth 2 -name tar.set`
for setfile in $LIST
do
echo "------Using $setfile------" >> $ROOTDIR/rsyncback_${TAG}.log
frequency=`readset TARSET $setfile`
#echo "$frequency"
case $frequency in
daily)
#echo "do daily"
www_rsync $setfile >> $ROOTDIR/rsyncback_${TAG}.log
;;
weekly)
#echo "do weekly"
if [ $(date +%w) == $DAY_WEEK ];then
www_rsync $setfile >> $ROOTDIR/rsyncback_${TAG}.log
else
echo "TARSET is $frequency, $(date +%w) is not $DAY_WEEK." >> $ROOTDIR/rsyncback_${TAG}.log
fi
;;
monthly)
#echo "do monthly"
if [ $(date +%d) == $DAY_MONTH ];then
www_rsync $setfile >> $ROOTDIR/rsyncback_${TAG}.log
else
echo "TARSET is $frequency, $(date +%d) is not $DAY_MONTH." >> $ROOTDIR/rsyncback_${TAG}.log
fi
;;
*)
echo "Warning!! Wrong frequency set in $setfile" >> $ROOTDIR/rsyncback_${TAG}.log
;;
esac
db=`readset DBNAME $setfile`
if [ -z $db ];then
echo "No database with $setfile..." >> $ROOTDIR/rsyncback_${TAG}.log
else
db_dump $db >> $ROOTDIR/rsyncback_${TAG}.log
fi
done
db_rsync >> $ROOTDIR/rsyncback_${TAG}.log
rm -rf $DB_DUMP
echo -e "Done all at $(date)!\n===============================================" >> $ROOTDIR/rsyncback_${TAG}.log
exit 0
#End
Jump to Line
Something went wrong with that request. Please try again.