我在Linux下的備份方法(三)

爲了把系統數據與個人用戶數據分開, 我特意寫了一個共 root 使用的腳本,以備份系統文件。 


#! /bin/bash

# using `dar' to backup a whole directory.  There're two methods here: 
# full backup and diff backup
# Dai Yuwen
# 18 Dec, 2010, modified for backing up whole file system

# backup the root partition and exclude the mount points
# dar  -v -c /mnt/wd2/debian_full -R / -s 2000M -z -M  -P home/yuwen

# diff backup
# dar  -v -c /mnt/wd2/debian_diff -A /mnt/wd2/debian_full -R / -s 2000M -z -M  -P home/yuwen

# restore command:
# cd /; dar -v -x /mnt/wd2/debian_full

Usage ()
{
    echo  "Options:"
    echo "-h                   print this help"
    echo "-e                   dry run"
    echo "-f <file name>       full backup"
    echo "-d <reference file>  diff backup base on reference file"
    echo 
    echo "Examples:"
    echo "backup.sh [-e] -f <file name>"
    echo "backup.sh [-e] -d <reference file>"
}

# parse the options by using getopt
TEMP=`getopt -o ehf:d: -n 'backup.sh' -- "$@"`

# parse error
if [ $? != 0 ]; then
    Usage >&2 ; 
    exit 1 ; 
fi

eval set -- "$TEMP"
DRY_RUN=""
FULL=""
DIFF=""
while true; do
    case "$1" in
        -e) DRY_RUN=" -e ";shift;;
        -h) Usage >&2; exit 0;;
        -f) NAME="$2";FULL="y"; shift 2;;
        -d) REF="$2";DIFF="y";shift 2;;
        --) shift; break;;
    esac
done

# full backup and diff backup are exclusive
# check the options
if [ "$FULL" == "y" -a "$DIFF" == "y" ]; then
    echo full backup and diff backup are exclusive
    exit 1
fi

if [ "$FULL" == "" -a "$DIFF" == "" ]; then
    echo You must specify -f or -d option
    Usage >&2 ; 
    exit 1
fi

#
# Customized options that you can modify
#
# the BASE dir
BASE=/

# add your excluded files here
EXC_FILES="home tmp"

# other options for dar
OTHER_OPTIONS=" -v -s 2000M -z -M "
#
# End of customized options
#

# the timestamp in the file name
DATA=`date +%Y%m%d`

# compose the options
EXC_OPT=""
for i in $EXC_FILES; do
     EXC_OPT=" -P $i $EXC_OPT"
done

# full backup
if [ "$FULL" == "y" ]; then
    eval dar $DRY_RUN -c "$NAME"_"$DATA" -R $BASE $OTHER_OPTIONS $EXC_OPT
fi

# diff backup
if [ "$DIFF" == "y" ]; then
    eval dar $DRY_RUN -c "$REF"_diff_"$DATA" -A "$REF" -R $BASE $OTHER_OPTIONS $EXC_OPT
fi

其實與備份 Home 目錄的腳本差不多, 只是EXC_FILES 排除的是 home 和 tmp 目錄。  這樣一個給普通用戶用, 一個給 root 用, 分得比較清楚。 以後可以按各自的需要修改。 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章