|

楼主 |
发表于 2005-1-23 22:04:12
|
显示全部楼层
系统备份脚本 [更新]
我按yongjian所说,搜索了一下以前的一些关于系统备份的帖子,发现分段备份是个好东西,于是收录进来。这次代码改动很大,增加了几个功能:1、放备份文件的目录可以直接在配置文件中指定;2、支持分段备份;3、用户可以选择是否对备份进行注释。
具体如下:
新增加了三个选项可以写在配置文件中,DEST,PART_TOGGLE还有LOG_TOGGLE。第一个DEST变量的内容是放备份文件的目录(注意,该变量并没有默认值,所以请用户一定要在配置文件中对该变量进行设定才能使用!!!);PART_TOGGLE则说明是否需要分段备份,0代表不分段(这也是默认值),任何大于0的值都将被看做是以Kilobytes为单位的每个分段的大小。最后那个LOG_TOGGLE变量表示是否在备份结束时对该次备份进行注释,有yes和no这两种值(默认为no),如果选yes的话将会在备份文件所在目录添加一个.log为扩展名的文件并打开vim来书写注释。除了DEST变量以外,其他两个可有可无。最后关键字SP:表示以下所有行都将当作需要备份的路径来处理。所以需要备份的路径一定要放到配置文件的最后来写。每个变量名后面跟一个冒号和空格之后就可以写相应的值。下面给出一个配置文件的例子:
- DEST: /mnt/usbc
- PART_TOGGLE: 10240
- LOG_TOGGLE: yes
- SP:
- weekly /home/neo
- weekly /etc
- weekly /var
- monthly / --exclude=/etc/* --exclude=/home/* --exclude=/var/* --exclude=/tmp/* --exclude=/proc/*
复制代码
这是更改后的脚本文件,请大家帮忙测试,确保在你机器上正常运行再使用!并多提意见和建议!谢谢
- #!/bin/bash
- # This script makes the backup of the system, the config file should be
- # in the home directory, called ".backuprc". The config file should
- # filled with options and several lines of the source pathes which you
- # want to be backed up. The options starts with the option name and then
- # followed by the values, for example, "DEST: /mnt/usbc" without quotes.
- # The name SP tells the script the following lines are source pathes.
- # Remember, source pathes always come at the end of the config file.
- # Each line has to in the format of this: "freq /source/path options".
- # the "freq" is the frequency you want this source to be backed up, and
- # "options" are the options you want to pass to tar. when running this
- # script you can specify the frequency you want to make backups this
- # time, for example: "backup.sh weekly". Then the script will pick the
- # matching source to backup. Currently there are three types of
- # frequency, which are "all", "weekly" and "monthly".
- # Written by Neo Anderson <ZeeGeek@gmail.com>
- # Jan 18 2005
- MTOGGLE=$1
- # set DEST to the destination which you want all the backup files go.
- # CAUTION, there's no default value for this variable, so you MUST
- # specify it in the config file.
- DEST=''
- # PART_TOGGLE specifies the size of each part of the target backup
- # file, 0 means don't cut, any number bigger than 0 will be treated as
- # Kilobytes of each part of the file.(default is 0)
- PART_TOGGLE=0
- # LOG_TOGGLE means whether or not you want to have a log when you
- # backup. (default is no)
- LOG_TOGGLE='no'
- initiate() {
- # check if the user is running this script as root
- if [ "$(whoami)" != 'root' ]
- then
- echo "Please run this script as root!"
- exit
- fi
- # check if the destination directory exists
- if [ ! -d $DEST ]
- then
- echo "Destination directory not exists. Please create it before backup"
- exit
- fi
- cd /
- }
- readconf() {
- # whether or not source path part has reached
- SP='false'
-
- exec < $HOME/.backuprc # redirect the input to the file .backup
-
- while read INPUT # read the config file line by line
- do
- if [ "`echo $INPUT | grep SP:`" ]
- then
- # read one more time to skip "SP:"
- if [ $SP == 'false' ]
- then
- read INPUT
- fi
-
- # do checking
- initiate
- # toggle on
- SP='true'
- # get the frequency
- TOGGLE="`echo $INPUT | awk '{ print $1 }'`"
- # get the source path and options passed to tar
- SOURCE=${INPUT#$TOGGLE}
-
- # check the toggle provided by user when running this
- # script
- case $MTOGGLE in
- weekly)
- # if the toggle specified by user
- # matches the toggle for each source,
- # then back it up
- if [ $MTOGGLE == $TOGGLE ]
- then
- backup
- else
- continue
- fi
- ;;
- monthly)
- if [ $MTOGGLE == $TOGGLE ]
- then
- backup
- else
- continue
- fi
- ;;
- all)
- backup
- ;;
- *)
- usage
- exit
- ;;
- esac
- else
- TEMP="`echo $INPUT | awk '{ print $1 }' | sed s/.$//`"
- TEMP_V="`echo $INPUT | awk '{ print $2 }'`"
- case $TEMP in
- DEST)
- DEST=$TEMP_V
- ;;
- PART_TOGGLE)
- PART_TOGGLE=$TEMP_V
- ;;
- LOG_TOGGLE)
- LOG_TOGGLE=$TEMP_V
- ;;
- RM_OLD)
- RM_OLD=$TEMP_V
- ;;
- *)
- echo "unrecognized option $TEMP"
- exit
- ;;
- esac
- fi
- done
- }
- backup() {
- SOURCE_PATH="`echo $SOURCE | awk '{ print $1 }'`"
- DEST_NAME="$DEST/`basename $SOURCE_PATH`"
- DEST_TAR="$DEST_NAME."`date +%b%d%Y-%H%M`".tar.gz"
- SIZE=$PART_TOGGLE
- if [ $PART_TOGGLE == 0 ]
- then
- tar czvfp $DEST_TAR $SOURCE # backup
- md5sum $DEST_TAR > $DEST_NAME.md5 # make md5sum
- else
- COUNTS=$[$PART_TOGGLE * 2]
- for ((a=1;;a++))
- do
- if [ "$SIZE" -ge "$PART_TOGGLE" ]
- then
- TEMP_DEST_TAR="$DEST_TAR"."$a"
- tar czvfp - $SOURCE | dd of=$DEST_TAR \
- skip=$[$COUNTS * $[$a - 1]] count=$COUNTS
- md5sum $DEST_TAR >> $DEST_NAME.md5
- # size of last backuped part of file
- SIZE="`ls -lk $DEST_TAR | awk '{ print $5}'`"
- else
- break
- fi
- done
- fi
-
- ls -lRa $SOURCE_PATH > $DEST_NAME.list # keep a list of this source
-
- # make a log file
- if [[ $LOG_TOGGLE == 'yes' || $LOG_TOGGLE == 'Yes' || \
- $LOG_TOGGLE == 'YES' || $LOG_TOGGLE == 'Y' || \
- $LOG_TOGGLE == 'y' ]]
- then
- echo "# Log file for $DEST_NAME" > $DEST_NAME
- echo "# Written by `whoami`" >> $DEST_NAME
- echo "# `date +"%b %d %Y"`" >> $DEST_NAME
- vi $DEST_NAME.log
- fi
- }
- usage() {
- echo "Usage: backup.sh [all | weekly | monthly]"
- }
- # check the existance of the config file
- if [ ! -f $HOME/.backuprc ]
- then
- echo "Config file not exist"
- exit
- fi
- readconf
复制代码 |
|