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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值