Skip to content

Instantly share code, notes, and snippets.

@moolex
Created December 3, 2012 10:28
Show Gist options
  • Save moolex/4194097 to your computer and use it in GitHub Desktop.
Save moolex/4194097 to your computer and use it in GitHub Desktop.
自动执行本地网站增量备份的脚本(centos+rsync)
#!/bin/sh
# Moyo <dev@uuland.org> @ 2012/02/23 10:00
# @url http://moyo.uuland.org/system/rsync-local-auto-sh/
# DEFINED ~~
# 脚本所在目录
workbase=/home/backup/shell
# 需要备份的网站列表(文件名)
listf=web.rsync.list
# 备份存放位置
websf=/home/backup/web
# notify MAIL
smail="server.moyo@gmail.com"
# from MAIL
fmail="server@cenwor.com"
# 备份保存时间(天)
dbkeepd=7
# rsync bin
rsyncd=rsync
# backup start
cdate=`date "+%Y.%m.%d"`
ii=0
tsa=0
tmplog="$websf/$cdate.ops.log"
listf="$workbase/$listf"
while read line; do
ii=`expr $ii + 1`
# get config
domain=`echo $line | awk -F ':' '{print $1}'`
path=`echo $line | awk -F ':' '{print $2}'`
excfile="$workbase/rsync.exclude/$domain.file"
EXCLUDES=""
if [ -e $excfile ]
then
EXCLUDES=" --exclude-from=$excfile "
else
mkdir -p "$workbase/rsync.exclude"
touch $excfile
fi
bakdir="$websf/$domain.files"
[ ! -e $bakdir ] && mkdir -p $bakdir
archiver="$websf/archiver/$domain"
[ ! -e $archiver ] && mkdir -p $archiver
ibakdir="$archiver/$cdate.ir"
[ ! -e $ibakdir ] && mkdir -p $ibakdir
echo "rsyncing $domain ..." >> $tmplog
echo "::::::::::::::::::::" >> $tmplog
# start
time_start=`date "+%s"`
flinec=`$rsyncd -avzu --delete --backup --backup-dir=$ibakdir $EXCLUDES $path $bakdir | wc -l`
echo ":: $flinec files modifys" >> $tmplog
# package
pkgfile="$archiver/mirror.tar.gz"
pkgfileir="$archiver/ir.$cdate.tar.gz"
tar czf $pkgfile -C $websf $domain.files
tar czf $pkgfileir -C $archiver $cdate.ir
echo "::::::::::::::::::::" >> $tmplog
echo "package finish [$pkgfile] [$pkgfileir]" >> $tmplog
# delete tmp file
[ -d $ibakdir ] && rm -rf $ibakdir
# delete old backups
echo "::::::::::::::::::::" >> $tmplog
echo "detecting if old backups..." >> $tmplog
find $archiver -type f -mtime +$dbkeepd -name "*.tar.gz" -exec rm -f {} \; >> $tmplog
# time calc
time_stop=`date "+%s"`
time_use=`expr $time_stop - $time_start`
echo "::::::::::::::::::::" >> $tmplog
echo "--- (use $time_use s) ---" >> $tmplog
tsa=`expr $tsa + $time_use`
done < $listf
# send mail
msubject="[ "`date "+%Y-%m-%d"`" ] web ($ii) backuped | use $tsa (s)"
cat $tmplog | mail -s "$msubject" $smail -- -f $fmail
rm -f $tmplog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment