日期:2014-05-16  浏览次数:20784 次

Linux下的文件备份脚本

#!/bin/sh
# backup selected partitions
#
## Note: added --exclude '*.iso' to tar command to exclude ISO DVDs & CDs.

#
# SITE SPECIFIC CONTENT
#

DEST="/home/backup"
# SOURCE="/home/www/cgi-bin /home/www/html/abbot /home/somebody"
cd /home
SOURCE=`ls /home |fgrep -v docs| fgrep -v lost | fgrep -v downloads |fgrep -v AL.tar.gz|fgrep -v MZ.tar.gz| fgrep -vi software | fgrep -v tmp|fgrep -v opt|fgrep -v recycle`
SOURCE="${SOURCE} /var/www/html"

## Daily updates only have files modified in last 2 days?
## so they take up less space

#
# Check for ARGC
# $0 -> daily backup
# $0 weekly -> weekly backup
# actually we just check for the existance of any argument and assume weekly
if (expr $# '>' 0 >/dev/null) then {
? PREFIX="weekly"
? rm -f ${DEST}/*daily*
? FILELIST="."
? UPDATE=""
} else {
? PREFIX="daily_update"
? UPDATE="/tmp/filez"
? FILELIST="-T /tmp/filez"
? DAYS=2
} fi

#
# Get the date
DATE=`/bin/date '+%Y%m%d'`

# cleanup caches and stuff
#rm -f ${i}/.netscape/cache/??/*
#rm -f ${i}/.netscape/cache/??/*
#rm -f ${i}/.kde/share/cache/http/?/*

# renice
# /usr/bin/renice +12 $$

#
# Archive directories listed in ${SOURCE}
for i in ${SOURCE}
do

? cd "/home/$i" || continue
? BASE=`basename $i`
? FILE="${DEST}/${DATE}_${PREFIX}_${BASE}";
? ## Daily Updates: non-cache, non-dir files modified in the last 2 days
? test -n "${UPDATE}" &&
? find -mtime -$DAYS \! -type d | fgrep -i -v cache > "$UPDATE"
? test -n "${UPDATE}" -a \! -s "${UPDATE}" &&
? echo "No updates for directory $i" &&
? continue
? echo "Creating archive ${FILE}"
? tar czf - ${FILELIST} >"${FILE}.tar.gz" --exclude '*.iso' --exclude MyPictures
? # sleep 240
? test -f "${UPDATE}" && rm -f "${UPDATE}"
done

# backup databases in mysql
DB="dbone dbtwo "
for i in ${DB}
do
? FILE="${DEST}/${DATE}_${PREFIX}_mysql_${i}.sql"
? echo "Creating SQL archive ${FILE}"
? mysqldump -u username "${i}" >"${FILE}"
? gzip "${FILE}"
done