我在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 用, 分得比较清楚。 以后可以按各自的需要修改。 

你可能感兴趣的:(我在Linux下的备份方法(三))