在 Ubuntu 下直接用 apt-get install rsnapshot.
在 FreeBSD 下請到 /usr/ports/sysutils/rsnapshot 下 make install clean. (在我的 FreeBSD 實體及虛擬環境中並沒有實際安裝測試, 以下僅用 Ubuntu 作為範例.)
2. 調整設定檔 /etc/rsnapshot.conf
# diff /etc/rsnapshot.conf /etc/rsnapshot.conf.20120322
27c27
< snapshot_root /.rsnapshot/
---
> snapshot_root /var/cache/rsnapshot/
97c97
< retain hourly 8
---
> retain hourly 6
100c100
< retain monthly 6
---
> #retain monthly 3
124c124
< logfile /var/log/rsnapshot.log
---
> #logfile /var/log/rsnapshot.log
230c230
< #backup /home/ localhost/
---
> backup /home/ localhost/
232,235c232
< backup /usr/local/etc/ localhost/
< backup /var/log/ localhost/
< backup /var/mail/ localhost/
< backup /root localhost/
---
> backup /usr/local/ localhost/
註 : 因為 /home 已將大部分資料放上 NFS, 已經有 ZFS snapshot. 就不浪費時間多做 snapshot.3. 設定定期執行
0 */3 * * * /usr/bin/rsnapshot hourly
0 23 * * * /usr/bin/rsnapshot daily
15 23 * * 1 /usr/bin/rsnapshot weekly
30 23 1 * * /usr/bin/rsnapshot monthly
0 0 * * 1 /PATH/TO/REPORT/PROG/report_CHK_ETC_diff.sh
4. CHK_ETC_diff.sh
#!/bin/bash
PATH=$PATH
CHKPARA=0
if [ -n "$1" ]; then
CHKPATH="$1"
shift
else
CHKPARA=1
fi
if [ -n "$1" ]; then
SSKEYWORD="$1"
shift
else
CHKPARA=1
fi
if [ -n "$1" ]; then
SSBASE="$1"
shift
else
CHKPARA=1
fi
if [ $CHKPARA -eq 1 ]; then
echo "Usage: $0 check_path snapshot_keyword snapshot_base_path"
echo "Example: $0 /etc Weekly /SNAPSHOT/System"
exit 1
else
echo "Collect difference between $CHKPATH and $SSBASE/${SSKEYWORD}*$CHKPATH."
fi
date +"%Y/%m/%d %H:%M:%S"
LAST_DIR="/"
CHKPATH=`echo $CHKPATH | sed -e 's/\///'`
for CONF in `find $LAST_DIR$CHKPATH -type f -name '*'`
do
if [ `ls $SSBASE | grep ${SSKEYWORD} |wc -l` -lt 1 ]; then
echo "$SSBASE/${SSKEYWORD}* does not existed!"
exit 1
fi
ls -d $SSBASE/${SSKEYWORD}* | while read DIR
do
if [ $LAST_DIR = "/" ]; then
HOST=""
else
HOST="/localhost"
fi
if [ -f ${LAST_DIR}${HOST}${CONF} ]&&[ -f ${DIR}/localhost${CONF} ]; then
if [ $LAST_DIR = "/" ]; then
LAST_DIR=""
fi
DIFFC=`diff ${LAST_DIR}${HOST}${CONF} ${DIR}/localhost${CONF} | wc -l`
if [ $DIFFC -gt 0 ]; then
echo "[[[ Difference between ${LAST_DIR}${HOST}${CONF} and ${DIR}/localhost${CONF} ]]]"
diff ${LAST_DIR}${HOST}${CONF} ${DIR}/localhost${CONF}
echo ""
fi
DIFFC=0
else
if [ ! -f ${LAST_DIR}${HOST}${CONF} ]; then
echo "File ${LAST_DIR}${HOST}${CONF} does not existed."
fi
fi
LAST_DIR=$DIR
done
done
echo ""
date +"%Y/%m/%d %H:%M:%S"
5. report_CHK_ETC_diff.sh
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:${PATH}
CHKPROG="/PATH/TO/PROG/CHK_ETC_diff.sh /etc daily /.rsnapshot"
REPORTPATH="/PATH/TO/STORE/REPORT/report_CHK_ETC_diff"
REPORTKEYWORD="etcdiff"
REPORTTITLE="Check Difference of ETC Report"
REMAILADDR="ACCOUNT@DOMAIN.NAME"
mkdir -p $REPORTPATH/`date +"%Y"`
mv $REPORTPATH/${REPORTKEYWORD}_*.log $REPORTPATH/`date +"%Y"`
$CHKPROG > $REPORTPATH/${REPORTKEYWORD}_`hostname`_`date +"%Y%m%d"`.log
cat $REPORTPATH/${REPORTKEYWORD}_`hostname`_`date +"%Y%m%d"`.log | mail -s "$REPORTTITLE" $REMAILADDR
參考資料:
淺談linux下的快照備份軟體—rsnapshot
No comments:
Post a Comment