LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
12
返回列表 发新帖
楼主: NeoAnderson

系统备份脚本

[复制链接]
 楼主| 发表于 2005-1-24 15:58:08 | 显示全部楼层

系统备份脚本 [ 第2次更新}

首先非常感谢kiron提出的意见。根据这些意见,代码进行了改动,增加了-h, --help选项,输出完整的帮助文档;增加了log文档,也就是说在备份的时候会自动生成一个backup-日期-时间.log的文档来记录本次备份的信息(不是用户自己写的注释文档!);如果备份过程中出现任何错误都会输出该错误并执行相应命令的help;如果检测到用户目录下没有配置文件存在,会提示用户创建,并要求用户对其进行更改;解决了分段备份中的几个小bug。
下面是代码:

  1. #!/bin/bash

  2. # This script makes the backup of the system. For more details, use
  3. # backup.sh --help
  4. # Written by Neo Anderson <ZeeGeek@gmail.com>
  5. # Jan 18 2005

  6. # the following four variables are specified by users
  7. MTOGGLE=$1
  8. # set DEST to the destination which you want all the backup files go.
  9. # CAUTION, there's no default value for this variable, so you MUST
  10. # specify it in the config file.
  11. DEST=''
  12. # PART_TOGGLE specifies the size of each part of the target backup
  13. # file, 0 means don't cut, any number bigger than 0 will be treated as
  14. # Kilobytes of each part of the file.(default is 0)
  15. PART_TOGGLE=0
  16. # LOG_TOGGLE means whether or not you want to have a log when you
  17. # backup. (default is no)
  18. LOG_TOGGLE='no'

  19. asroot() {
  20.         # check if the user is running this script as root
  21.         if [ "$(whoami)" != 'root' ]
  22.         then
  23.                 echo "Please run this script as root!"
  24.                 exit
  25.         fi
  26. }

  27. usercheck() {
  28.         echo "DEST is $DEST"
  29.         echo "PART_TOGGLE is $PART_TOGGLE"
  30.         echo "LOG_TOGGLE is $LOG_TOGGLE"
  31.         echo -n "are they correct? (y/n)"
  32.         read -n 1 CHECK_ANS
  33.         echo
  34.         if [ $CHECK_ANS != 'y' ]
  35.         then
  36.                 echo "please edit the config file"
  37.                 exit
  38.         fi
  39. }

  40. initiate() {
  41.         # check if the destination directory exists
  42.         if [ ! -d $DEST ]
  43.         then
  44.                 echo "Destination directory not exists. Please create it before backup"
  45.                 exit
  46.         fi
  47.         cd /
  48. }

  49. config() {
  50.         echo -n "Config file not exist. Do you want me to create it for        you? (y/n)"
  51.         read CONFIG_ANS

  52.         if [ $CONFIG_ANS == 'y' ]
  53.         then
  54.                 echo "DEST: /path/to/put/the/backup/file" > $HOME/.backuprc
  55.                 echo "SP:" >> $HOME/.backuprc
  56.                 echo "freq /things/you/want/to/backup [options for tar]" >> $HOME/.backuprc
  57.                 echo "The config file has been created, it's called .backuprc in your home directory. YOU MUST EDIT IT BEFORE USE!"
  58.                 echo -n "Edit now? (y/n)"
  59.                 read EDIT_ANS
  60.                 if [ $EDIT_ANS == 'y' ]
  61.                 then
  62.                         $EDITOR $HOME/.backuprc
  63.                         readconf
  64.                 else
  65.                         exit
  66.                 fi
  67.         else
  68.                 exit
  69.         fi
  70. }

  71. readconf() {
  72.         # whether or not source path part has reached
  73.         SP='false'
  74.        
  75.         exec 6<> $HOME/.backuprc        # redirect FD 6 to the file .backup
  76.        
  77.         while read INPUT <&6        # read the config file line by line
  78.         do
  79.                 if [[ "`echo $INPUT | grep SP:`" || $SP == 'true' ]]
  80.                 then
  81.                         # read one more time to skip "SP:"
  82.                         if [ $SP == 'false' ]
  83.                         then
  84.                                 read INPUT <&6
  85.                                 # ask user to check
  86.                                 usercheck
  87.                                 # record log file
  88.                                 echo "All options correct" >> $BACKUP_LOG
  89.                                 # initiating
  90.                                 initiate
  91.                                 # record log file
  92.                                 echo "Destination directory exists" >> $BACKUP_LOG
  93.                                 # toggle on
  94.                                 SP='true'
  95.                         fi
  96.                        
  97.                         # get the frequency
  98.                         TOGGLE="`echo $INPUT | awk '{ print $1 }'`"
  99.                         # get the source path and options passed to tar
  100.                         SOURCE=${INPUT#$TOGGLE}
  101.                        
  102.                         # check the toggle provided by user when running this
  103.                         # script
  104.                         case $MTOGGLE in
  105.                                 weekly)
  106.                                         # if the toggle specified by user
  107.                                         # matches the toggle for each source,
  108.                                         # then back it up
  109.                                         if [ $MTOGGLE == $TOGGLE ]
  110.                                         then
  111.                                                 backup
  112.                                         else
  113.                                                 continue
  114.                                         fi
  115.                                         ;;
  116.                                 monthly)
  117.                                         if [ $MTOGGLE == $TOGGLE ]
  118.                                         then
  119.                                                 backup
  120.                                         else
  121.                                                 continue
  122.                                         fi
  123.                                         ;;
  124.                                 all)
  125.                                         backup
  126.                                         ;;
  127.                                 *)
  128.                                         usage
  129.                                         exit
  130.                                         ;;
  131.                         esac
  132.                 else
  133.                         TEMP="`echo $INPUT | awk '{ print $1 }' | sed s/.$//`"
  134.                         TEMP_V="`echo $INPUT | awk '{ print $2 }'`"
  135.                         case $TEMP in
  136.                                 DEST)
  137.                                         DEST=$TEMP_V
  138.                                         BACKUP_LOG="$DEST/backup-`date +%b%d%Y-%H%M`.log"
  139.                                         # record log file
  140.                                         echo "----- Start of log file -----" >> $BACKUP_LOG
  141.                                         echo "Config file exists" >> $BACKUP_LOG
  142.                                         echo "DEST read" >> $BACKUP_LOG
  143.                                         ;;
  144.                                 PART_TOGGLE)
  145.                                         PART_TOGGLE=$TEMP_V
  146.                                         # record log file
  147.                                         echo "PART_TOGGLE read" >> $BACKUP_LOG
  148.                                         ;;
  149.                                 LOG_TOGGLE)
  150.                                         LOG_TOGGLE=$TEMP_V
  151.                                         # record log file
  152.                                         echo "LOG_TOGGLE read" >> $BACKUP_LOG
  153.                                         ;;
  154.                                 RM_OLD)
  155.                                         RM_OLD=$TEMP_V
  156.                                         # record log file
  157.                                         echo "RM_OLD read" >> $BACKUP_LOG
  158.                                         ;;
  159.                                 *)
  160.                                         echo "unrecognized option $TEMP"
  161.                                         usage
  162.                                         exit
  163.                                         ;;
  164.                         esac
  165.                 fi
  166.         done
  167.         # record log file
  168.         echo >> $BACKUP_LOG
  169.         echo >> $BACKUP_LOG
  170.         echo "all jobs done successfully" >> $BACKUP_LOG
  171. }

  172. backup() {
  173.         # record log file
  174.         echo >> $BACKUP_LOG
  175.         echo "----------" >> $BACKUP_LOG
  176.         echo >> $BACKUP_LOG
  177.         SOURCE_PATH="`echo $SOURCE | awk '{ print $1 }'`"
  178.         # record log file
  179.         echo "SOURCE PATH is $SOURCE_PATH" >> $BACKUP_LOG
  180.         DEST_NAME="$DEST/`basename $SOURCE_PATH`"
  181.         # record log file
  182.         echo "DEST NAME is $DEST_NAME" >> $BACKUP_LOG
  183.         DEST_TAR="$DEST_NAME-`date +%b%d%Y-%H%M`.tar.gz"
  184.         # record log file
  185.         echo "DEST TAR is $DEST_TAR" >> $BACKUP_LOG
  186.         SIZE=$PART_TOGGLE

  187.         if [ $PART_TOGGLE == 0 ]
  188.         then
  189.                 tar czvfp $DEST_TAR $SOURCE >> $BACKUP_LOG 2>&1        # backup
  190.                 if [ $? != 0 ]
  191.                 then
  192.                         echo "Error making backup. Errors are stored in $BACKUP_LOG"
  193.                         tar --help
  194.                         exit
  195.                 fi
  196.                 md5sum $DEST_TAR > $DEST_NAME.md5 2>>$BACKUP_LOG        # make md5sum
  197.                 if [ $? != 0 ]
  198.                 then
  199.                         echo "Error making md5sum of backup. Errors are stored in $BACKUP_LOG"
  200.                         md5sum --help
  201.                         exit
  202.                 fi
  203.         else
  204.                 COUNTS=$[$PART_TOGGLE * 2]
  205.                 # record log file
  206.                 echo "COUNTS is $COUNTS" >> $BACKUP_LOG
  207.                 for ((a=1;;a++))
  208.                 do
  209.                         # record log file
  210.                         echo >> $BACKUP_LOG
  211.                         if [ "$SIZE" -ge "$PART_TOGGLE" ]
  212.                         then
  213.                                 TEMP_DEST_TAR="$DEST_TAR"."$a"
  214.                                 # record log file
  215.                                 echo "TEMP DEST TAR is $TEMP_DEST_TAR" >> $BACKUP_LOG
  216.                                 tar czvfp - $SOURCE | dd of=$TEMP_DEST_TAR \
  217.                                 skip=$[$COUNTS * $[$a - 1]] \
  218.                                 count=$COUNTS >> $BACKUP_LOG 2>&1
  219.                                 if [ $? != 0 ]
  220.                                 then
  221.                                         echo "Error making backup. Errors are stored in $BACKUP_LOG"
  222.                                         dd --help
  223.                                         exit
  224.                                 fi
  225.                                 md5sum $TEMP_DEST_TAR >> $DEST_NAME.md5 2>>$BACKUP_LOG
  226.                                 if [ $? != 0 ]
  227.                                 then
  228.                                         echo "Error making md5sum of backup. Errors are stored in $BACKUP_LOG"
  229.                                         md5sum --help
  230.                                         exit
  231.                                 fi
  232.                                 # size of last backuped part of file
  233.                                 SIZE="`ls -lk $TEMP_DEST_TAR | awk '{ print $5}'`"
  234.                                 # record log file
  235.                                 echo "SIZE is $SIZE" >> $BACKUP_LOG
  236.                         else
  237.                                 break
  238.                         fi
  239.                 done
  240.         fi
  241.        
  242.         # record log file
  243.         echo "backup succeeded" >> $BACKUP_LOG
  244.         ls -lRa $SOURCE_PATH > $DEST_NAME.list        # keep a list of this source
  245.         # record log file
  246.         echo "file list made" >> $BACKUP_LOG
  247.        
  248.         # make a log file
  249.         if [[ $LOG_TOGGLE == 'yes' || $LOG_TOGGLE == 'Yes' || \
  250.         $LOG_TOGGLE == 'YES' || $LOG_TOGGLE == 'Y' || \
  251.         $LOG_TOGGLE == 'y' ]]
  252.         then
  253.                 echo "# Log file for $DEST_NAME" > $DEST_NAME
  254.                 echo "# Written by `whoami`" >> $DEST_NAME
  255.                 echo "# `date +"%b %d %Y"`" >> $DEST_NAME
  256.                 $EDITOR $DEST_NAME.log
  257.                 # record log file
  258.                 echo "log file made" >> $BACKUP_LOG
  259.         fi
  260. }

  261. usage() {
  262.         echo 'Usage: backup.sh [OPTION]'
  263.         echo 'Options:'
  264.         echo '  all                backup all the entries specified in the config file'
  265.         echo '  weekly        backup entries start with 'weekly' in the config file'
  266.         echo '  monthly        backup entries start with 'monthly' in the config file'
  267.         echo '  -h, --help        display the help and exit'
  268.         echo
  269.         echo 'Config file:'
  270.         echo 'config file must be present in your home directory with
  271. the name of .backuprc. There are four options you can specify in
  272. it. They are DEST, PART_TOGGLE, LOG_TOGGLE and SP. The meaning
  273. for them are "destination directory you want all backup files
  274. in", "the size of each part of the target backup file, 0 means
  275. do not cut, any number bigger than 0 will be treated as
  276. Kilobytes of each part of the file.(default is 0)", "whether or
  277. not you want to have a log when you backup. (default is no)",
  278. "the sign of telling the script the following lines are pathes
  279. you want to be backuped", respectively. Each option name must be
  280. followed by a colon and a space, then the value. The options
  281. DEST and SP have to exist in order to have the script work. The DEST
  282. option must comes at the beginning and SP must be put at the end of the
  283. config file.'
  284.         echo
  285.         echo 'Example of .backuprc:'
  286.         echo '        DEST: /mnt/usbc'
  287.         echo '        PART_TOGGLE: 10240'
  288.         echo '        LOG_TOGGLE: yes'
  289.         echo '        SP:'
  290.         echo '        weekly /root'
  291.         echo '        monthly /etc'
  292. }

  293. # Start of the script

  294. # check if the script is run as root
  295. asroot

  296. # check the existance of the config file
  297. if [ ! -f $HOME/.backuprc ]
  298. then
  299.         config
  300. fi

  301. # help
  302. if [[ $1 == '--help' || $1 == '-h' ]]
  303. then
  304.         usage
  305. elif [[ $1 == 'all' || $1 == 'weekly' || $1 == 'monthly' ]]
  306. then
  307.         readconf
  308. else
  309.         usage
  310. fi
复制代码


有个问题没解决,想请大家指教:当进行分段备份时,为何重定向不起作用,输出仍然会跑到屏幕上,而不是我指定的log文件里。如果不是分段备份就没这个问题。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-1-26 01:11:27 | 显示全部楼层
改了备份跟目录时无法正确创建文件名的问题,就是加了个判断。


  1. #!/bin/bash

  2. # This script makes the backup of the system. For more details, use
  3. # backup.sh --help
  4. # Written by Neo Anderson <ZeeGeek@gmail.com>
  5. # Jan 18 2005

  6. # the following four variables are specified by users
  7. MTOGGLE=$1
  8. # set DEST to the destination which you want all the backup files go.
  9. # CAUTION, there's no default value for this variable, so you MUST
  10. # specify it in the config file.
  11. DEST=''
  12. # PART_TOGGLE specifies the size of each part of the target backup
  13. # file, 0 means don't cut, any number bigger than 0 will be treated as
  14. # Kilobytes of each part of the file.(default is 0)
  15. PART_TOGGLE=0
  16. # LOG_TOGGLE means whether or not you want to have a log when you
  17. # backup. (default is no)
  18. LOG_TOGGLE='no'

  19. asroot() {
  20.         # check if the user is running this script as root
  21.         if [ "$(whoami)" != 'root' ]
  22.         then
  23.                 echo "Please run this script as root!"
  24.                 exit
  25.         fi
  26. }

  27. usercheck() {
  28.         echo "DEST is $DEST"
  29.         echo "PART_TOGGLE is $PART_TOGGLE"
  30.         echo "LOG_TOGGLE is $LOG_TOGGLE"
  31.         echo -n "are they correct? (y/n)"
  32.         read -n 1 CHECK_ANS
  33.         echo
  34.         if [ $CHECK_ANS != 'y' ]
  35.         then
  36.                 echo "please edit the config file"
  37.                 exit
  38.         fi
  39. }

  40. initiate() {
  41.         # check if the destination directory exists
  42.         if [ ! -d $DEST ]
  43.         then
  44.                 echo "Destination directory not exists. Please create it before backup"
  45.                 exit
  46.         fi
  47.         cd /
  48. }

  49. config() {
  50.         echo -n "Config file not exist. Do you want me to create it for        you? (y/n)"
  51.         read CONFIG_ANS

  52.         if [ $CONFIG_ANS == 'y' ]
  53.         then
  54.                 echo "DEST: /path/to/put/the/backup/file" > $HOME/.backuprc
  55.                 echo "SP:" >> $HOME/.backuprc
  56.                 echo "freq /things/you/want/to/backup [options for tar]" >> $HOME/.backuprc
  57.                 echo "The config file has been created, it's called .backuprc in your home directory. YOU MUST EDIT IT BEFORE USE!"
  58.                 echo -n "Edit now? (y/n)"
  59.                 read EDIT_ANS
  60.                 if [ $EDIT_ANS == 'y' ]
  61.                 then
  62.                         $EDITOR $HOME/.backuprc
  63.                         readconf
  64.                 else
  65.                         exit
  66.                 fi
  67.         else
  68.                 exit
  69.         fi
  70. }

  71. readconf() {
  72.         # whether or not source path part has reached
  73.         SP='false'
  74.        
  75.         exec 6<> $HOME/.backuprc        # redirect FD 6 to the file .backup
  76.        
  77.         while read INPUT <&6        # read the config file line by line
  78.         do
  79.                 if [[ "`echo $INPUT | grep SP:`" || $SP == 'true' ]]
  80.                 then
  81.                         # read one more time to skip "SP:"
  82.                         if [ $SP == 'false' ]
  83.                         then
  84.                                 read INPUT <&6
  85.                                 # ask user to check
  86.                                 usercheck
  87.                                 # record log file
  88.                                 echo "All options correct" >> $BACKUP_LOG
  89.                                 # initiating
  90.                                 initiate
  91.                                 # record log file
  92.                                 echo "Destination directory exists" >> $BACKUP_LOG
  93.                                 # toggle on
  94.                                 SP='true'
  95.                         fi
  96.                        
  97.                         # get the frequency
  98.                         TOGGLE="`echo $INPUT | awk '{ print $1 }'`"
  99.                         # get the source path and options passed to tar
  100.                         SOURCE=${INPUT#$TOGGLE}
  101.                        
  102.                         # check the toggle provided by user when running this
  103.                         # script
  104.                         case $MTOGGLE in
  105.                                 weekly)
  106.                                         # if the toggle specified by user
  107.                                         # matches the toggle for each source,
  108.                                         # then back it up
  109.                                         if [ $MTOGGLE == $TOGGLE ]
  110.                                         then
  111.                                                 backup
  112.                                         else
  113.                                                 continue
  114.                                         fi
  115.                                         ;;
  116.                                 monthly)
  117.                                         if [ $MTOGGLE == $TOGGLE ]
  118.                                         then
  119.                                                 backup
  120.                                         else
  121.                                                 continue
  122.                                         fi
  123.                                         ;;
  124.                                 all)
  125.                                         backup
  126.                                         ;;
  127.                                 *)
  128.                                         usage
  129.                                         exit
  130.                                         ;;
  131.                         esac
  132.                 else
  133.                         TEMP="`echo $INPUT | awk '{ print $1 }' | sed s/.$//`"
  134.                         TEMP_V="`echo $INPUT | awk '{ print $2 }'`"
  135.                         case $TEMP in
  136.                                 DEST)
  137.                                         DEST=$TEMP_V
  138.                                         BACKUP_LOG="$DEST/backup-`date +%b%d%Y-%H%M`.log"
  139.                                         # record log file
  140.                                         echo "----- Start of log file -----" >> $BACKUP_LOG
  141.                                         echo "Config file exists" >> $BACKUP_LOG
  142.                                         echo "DEST read" >> $BACKUP_LOG
  143.                                         ;;
  144.                                 PART_TOGGLE)
  145.                                         PART_TOGGLE=$TEMP_V
  146.                                         # record log file
  147.                                         echo "PART_TOGGLE read" >> $BACKUP_LOG
  148.                                         ;;
  149.                                 LOG_TOGGLE)
  150.                                         LOG_TOGGLE=$TEMP_V
  151.                                         # record log file
  152.                                         echo "LOG_TOGGLE read" >> $BACKUP_LOG
  153.                                         ;;
  154.                                 RM_OLD)
  155.                                         RM_OLD=$TEMP_V
  156.                                         # record log file
  157.                                         echo "RM_OLD read" >> $BACKUP_LOG
  158.                                         ;;
  159.                                 *)
  160.                                         echo "unrecognized option $TEMP"
  161.                                         usage
  162.                                         exit
  163.                                         ;;
  164.                         esac
  165.                 fi
  166.         done
  167.         # record log file
  168.         echo >> $BACKUP_LOG
  169.         echo >> $BACKUP_LOG
  170.         echo "all jobs done successfully" >> $BACKUP_LOG
  171. }

  172. backup() {
  173.         # record log file
  174.         echo >> $BACKUP_LOG
  175.         echo "----------" >> $BACKUP_LOG
  176.         echo >> $BACKUP_LOG
  177.         SOURCE_PATH="`echo $SOURCE | awk '{ print $1 }'`"
  178.         # record log file
  179.         echo "SOURCE PATH is $SOURCE_PATH" >> $BACKUP_LOG
  180.         if [ $(basename $SOURCE_PATH) == '/' ]
  181.         then
  182.                 DEST_NAME="$DEST/root"
  183.         else
  184.                 DEST_NAME="$DEST/`basename $SOURCE_PATH`"
  185.         fi
  186.         # record log file
  187.         echo "DEST NAME is $DEST_NAME" >> $BACKUP_LOG
  188.         DEST_TAR="$DEST_NAME-`date +%b%d%Y-%H%M`.tar.gz"
  189.         # record log file
  190.         echo "DEST TAR is $DEST_TAR" >> $BACKUP_LOG
  191.         SIZE=$PART_TOGGLE

  192.         if [ $PART_TOGGLE == 0 ]
  193.         then
  194.                 tar czvfp $DEST_TAR $SOURCE >> $BACKUP_LOG 2>&1        # backup
  195.                 if [ $? != 0 ]
  196.                 then
  197.                         echo "Error making backup. Errors are stored in $BACKUP_LOG"
  198.                         tar --help
  199.                         exit
  200.                 fi
  201.                 md5sum $DEST_TAR > $DEST_NAME.md5 2>>$BACKUP_LOG        # make md5sum
  202.                 if [ $? != 0 ]
  203.                 then
  204.                         echo "Error making md5sum of backup. Errors are stored in $BACKUP_LOG"
  205.                         md5sum --help
  206.                         exit
  207.                 fi
  208.         else
  209.                 COUNTS=$[$PART_TOGGLE * 2]
  210.                 # record log file
  211.                 echo "COUNTS is $COUNTS" >> $BACKUP_LOG
  212.                 for ((a=1;;a++))
  213.                 do
  214.                         # record log file
  215.                         echo >> $BACKUP_LOG
  216.                         if [ "$SIZE" -ge "$PART_TOGGLE" ]
  217.                         then
  218.                                 TEMP_DEST_TAR="$DEST_TAR"."$a"
  219.                                 # record log file
  220.                                 echo "TEMP DEST TAR is $TEMP_DEST_TAR" >> $BACKUP_LOG
  221.                                 tar czvfp - $SOURCE | dd of=$TEMP_DEST_TAR \
  222.                                 skip=$[$COUNTS * $[$a - 1]] \
  223.                                 count=$COUNTS >> $BACKUP_LOG 2>&1
  224.                                 if [ $? != 0 ]
  225.                                 then
  226.                                         echo "Error making backup. Errors are stored in $BACKUP_LOG"
  227.                                         dd --help
  228.                                         exit
  229.                                 fi
  230.                                 md5sum $TEMP_DEST_TAR >> $DEST_NAME.md5 2>>$BACKUP_LOG
  231.                                 if [ $? != 0 ]
  232.                                 then
  233.                                         echo "Error making md5sum of backup. Errors are stored in $BACKUP_LOG"
  234.                                         md5sum --help
  235.                                         exit
  236.                                 fi
  237.                                 # size of last backuped part of file
  238.                                 SIZE="`ls -lk $TEMP_DEST_TAR | awk '{ print $5}'`"
  239.                                 # record log file
  240.                                 echo "SIZE is $SIZE" >> $BACKUP_LOG
  241.                         else
  242.                                 break
  243.                         fi
  244.                 done
  245.         fi
  246.        
  247.         # record log file
  248.         echo "backup succeeded" >> $BACKUP_LOG
  249.         ls -lRa $SOURCE_PATH > $DEST_NAME.list        # keep a list of this source
  250.         # record log file
  251.         echo "file list made" >> $BACKUP_LOG
  252.        
  253.         # make a log file
  254.         if [[ $LOG_TOGGLE == 'yes' || $LOG_TOGGLE == 'Yes' || \
  255.         $LOG_TOGGLE == 'YES' || $LOG_TOGGLE == 'Y' || \
  256.         $LOG_TOGGLE == 'y' ]]
  257.         then
  258.                 echo "# Log file for $DEST_NAME" > $DEST_NAME
  259.                 echo "# Written by `whoami`" >> $DEST_NAME
  260.                 echo "# `date +"%b %d %Y"`" >> $DEST_NAME
  261.                 $EDITOR $DEST_NAME.log
  262.                 # record log file
  263.                 echo "log file made" >> $BACKUP_LOG
  264.         fi
  265. }

  266. usage() {
  267.         echo 'Usage: backup.sh [OPTION]'
  268.         echo 'Options:'
  269.         echo '  all                backup all the entries specified in the config file'
  270.         echo '  weekly        backup entries start with 'weekly' in the config file'
  271.         echo '  monthly        backup entries start with 'monthly' in the config file'
  272.         echo '  -h, --help        display the help and exit'
  273.         echo
  274.         echo 'Config file:'
  275.         echo 'config file must be present in your home directory with
  276. the name of .backuprc. There are four options you can specify in
  277. it. They are DEST, PART_TOGGLE, LOG_TOGGLE and SP. The meaning
  278. for them are "destination directory you want all backup files
  279. in", "the size of each part of the target backup file, 0 means
  280. do not cut, any number bigger than 0 will be treated as
  281. Kilobytes of each part of the file.(default is 0)", "whether or
  282. not you want to have a log when you backup. (default is no)",
  283. "the sign of telling the script the following lines are pathes
  284. you want to be backuped", respectively. Each option name must be
  285. followed by a colon and a space, then the value. The options
  286. DEST and SP have to exist in order to have the script work. The DEST
  287. option must comes at the beginning and SP must be put at the end of the
  288. config file.'
  289.         echo
  290.         echo 'Example of .backuprc:'
  291.         echo '        DEST: /mnt/usbc'
  292.         echo '        PART_TOGGLE: 10240'
  293.         echo '        LOG_TOGGLE: yes'
  294.         echo '        SP:'
  295.         echo '        weekly /root'
  296.         echo '        monthly /etc'
  297. }

  298. # Start of the script

  299. # check if the script is run as root
  300. asroot

  301. # check the existance of the config file
  302. if [ ! -f $HOME/.backuprc ]
  303. then
  304.         config
  305. fi

  306. # help
  307. if [[ $1 == '--help' || $1 == '-h' ]]
  308. then
  309.         usage
  310. elif [[ $1 == 'all' || $1 == 'weekly' || $1 == 'monthly' ]]
  311. then
  312.         readconf
  313. else
  314.         usage
  315. fi
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-1-26 13:42:04 | 显示全部楼层
由于一时疏忽,那个新增的判断语句漏了点东西,现在重贴一遍。

  1. #!/bin/bash

  2. # This script makes the backup of the system. For more details, use
  3. # backup.sh --help
  4. # Written by Neo Anderson <ZeeGeek@gmail.com>
  5. # Jan 18 2005

  6. # the following four variables are specified by users
  7. MTOGGLE=$1
  8. # set DEST to the destination which you want all the backup files go.
  9. # CAUTION, there's no default value for this variable, so you MUST
  10. # specify it in the config file.
  11. DEST=''
  12. # PART_TOGGLE specifies the size of each part of the target backup
  13. # file, 0 means don't cut, any number bigger than 0 will be treated as
  14. # Kilobytes of each part of the file.(default is 0)
  15. PART_TOGGLE=0
  16. # LOG_TOGGLE means whether or not you want to have a log when you
  17. # backup. (default is no)
  18. LOG_TOGGLE='no'

  19. asroot() {
  20.         # check if the user is running this script as root
  21.         if [ "$(whoami)" != 'root' ]
  22.         then
  23.                 echo "Please run this script as root!"
  24.                 exit
  25.         fi
  26. }

  27. usercheck() {
  28.         echo "DEST is $DEST"
  29.         echo "PART_TOGGLE is $PART_TOGGLE"
  30.         echo "LOG_TOGGLE is $LOG_TOGGLE"
  31.         echo -n "are they correct? (y/n)"
  32.         read -n 1 CHECK_ANS
  33.         echo
  34.         if [ $CHECK_ANS != 'y' ]
  35.         then
  36.                 echo "please edit the config file"
  37.                 exit
  38.         fi
  39. }

  40. initiate() {
  41.         # check if the destination directory exists
  42.         if [ ! -d $DEST ]
  43.         then
  44.                 echo "Destination directory not exists. Please create it before backup"
  45.                 exit
  46.         fi
  47.         cd /
  48. }

  49. config() {
  50.         echo -n "Config file not exist. Do you want me to create it for        you? (y/n)"
  51.         read CONFIG_ANS

  52.         if [ $CONFIG_ANS == 'y' ]
  53.         then
  54.                 echo "DEST: /path/to/put/the/backup/file" > $HOME/.backuprc
  55.                 echo "SP:" >> $HOME/.backuprc
  56.                 echo "freq /things/you/want/to/backup [options for tar]" >> $HOME/.backuprc
  57.                 echo "The config file has been created, it's called .backuprc in your home directory. YOU MUST EDIT IT BEFORE USE!"
  58.                 echo -n "Edit now? (y/n)"
  59.                 read EDIT_ANS
  60.                 if [ $EDIT_ANS == 'y' ]
  61.                 then
  62.                         $EDITOR $HOME/.backuprc
  63.                         readconf
  64.                 else
  65.                         exit
  66.                 fi
  67.         else
  68.                 exit
  69.         fi
  70. }

  71. readconf() {
  72.         # whether or not source path part has reached
  73.         SP='false'
  74.        
  75.         exec 6<> $HOME/.backuprc        # redirect FD 6 to the file .backup
  76.        
  77.         while read INPUT <&6        # read the config file line by line
  78.         do
  79.                 if [[ "`echo $INPUT | grep SP:`" || $SP == 'true' ]]
  80.                 then
  81.                         # read one more time to skip "SP:"
  82.                         if [ $SP == 'false' ]
  83.                         then
  84.                                 read INPUT <&6
  85.                                 # ask user to check
  86.                                 usercheck
  87.                                 # record log file
  88.                                 echo "All options correct" >> $BACKUP_LOG
  89.                                 # initiating
  90.                                 initiate
  91.                                 # record log file
  92.                                 echo "Destination directory exists" >> $BACKUP_LOG
  93.                                 # toggle on
  94.                                 SP='true'
  95.                         fi
  96.                        
  97.                         # get the frequency
  98.                         TOGGLE="`echo $INPUT | awk '{ print $1 }'`"
  99.                         # get the source path and options passed to tar
  100.                         SOURCE=${INPUT#$TOGGLE}
  101.                        
  102.                         # check the toggle provided by user when running this
  103.                         # script
  104.                         case $MTOGGLE in
  105.                                 weekly)
  106.                                         # if the toggle specified by user
  107.                                         # matches the toggle for each source,
  108.                                         # then back it up
  109.                                         if [ $MTOGGLE == $TOGGLE ]
  110.                                         then
  111.                                                 backup
  112.                                         else
  113.                                                 continue
  114.                                         fi
  115.                                         ;;
  116.                                 monthly)
  117.                                         if [ $MTOGGLE == $TOGGLE ]
  118.                                         then
  119.                                                 backup
  120.                                         else
  121.                                                 continue
  122.                                         fi
  123.                                         ;;
  124.                                 all)
  125.                                         backup
  126.                                         ;;
  127.                                 *)
  128.                                         usage
  129.                                         exit
  130.                                         ;;
  131.                         esac
  132.                 else
  133.                         TEMP="`echo $INPUT | awk '{ print $1 }' | sed s/.$//`"
  134.                         TEMP_V="`echo $INPUT | awk '{ print $2 }'`"
  135.                         case $TEMP in
  136.                                 DEST)
  137.                                         DEST=$TEMP_V
  138.                                         BACKUP_LOG="$DEST/backup-`date +%b%d%Y-%H%M`.log"
  139.                                         # record log file
  140.                                         echo "----- Start of log file -----" >> $BACKUP_LOG
  141.                                         echo "Config file exists" >> $BACKUP_LOG
  142.                                         echo "DEST read" >> $BACKUP_LOG
  143.                                         ;;
  144.                                 PART_TOGGLE)
  145.                                         PART_TOGGLE=$TEMP_V
  146.                                         # record log file
  147.                                         echo "PART_TOGGLE read" >> $BACKUP_LOG
  148.                                         ;;
  149.                                 LOG_TOGGLE)
  150.                                         LOG_TOGGLE=$TEMP_V
  151.                                         # record log file
  152.                                         echo "LOG_TOGGLE read" >> $BACKUP_LOG
  153.                                         ;;
  154.                                 RM_OLD)
  155.                                         RM_OLD=$TEMP_V
  156.                                         # record log file
  157.                                         echo "RM_OLD read" >> $BACKUP_LOG
  158.                                         ;;
  159.                                 *)
  160.                                         echo "unrecognized option $TEMP"
  161.                                         usage
  162.                                         exit
  163.                                         ;;
  164.                         esac
  165.                 fi
  166.         done
  167.         # record log file
  168.         echo >> $BACKUP_LOG
  169.         echo >> $BACKUP_LOG
  170.         echo "all jobs done successfully" >> $BACKUP_LOG
  171. }

  172. backup() {
  173.         # record log file
  174.         echo >> $BACKUP_LOG
  175.         echo "----------" >> $BACKUP_LOG
  176.         echo >> $BACKUP_LOG
  177.         SOURCE_PATH="`echo $SOURCE | awk '{ print $1 }'`"
  178.         # record log file
  179.         echo "SOURCE PATH is $SOURCE_PATH" >> $BACKUP_LOG
  180.         if [ $(basename $SOURCE_PATH) == '/' ]
  181.         then
  182.                 DEST_NAME="$DEST/rootpartition"
  183.         else
  184.                 DEST_NAME="$DEST/`basename $SOURCE_PATH`"
  185.         fi
  186.         # record log file
  187.         echo "DEST NAME is $DEST_NAME" >> $BACKUP_LOG
  188.         DEST_TAR="$DEST_NAME-`date +%b%d%Y-%H%M`.tar.gz"
  189.         # record log file
  190.         echo "DEST TAR is $DEST_TAR" >> $BACKUP_LOG
  191.         SIZE=$PART_TOGGLE

  192.         if [ $PART_TOGGLE == 0 ]
  193.         then
  194.                 tar czvfp $DEST_TAR $SOURCE >> $BACKUP_LOG 2>&1        # backup
  195.                 if [ $? != 0 ]
  196.                 then
  197.                         echo "Error making backup. Errors are stored in $BACKUP_LOG"
  198.                         tar --help
  199.                         exit
  200.                 fi
  201.                 md5sum $DEST_TAR > $DEST_NAME.md5 2>>$BACKUP_LOG        # make md5sum
  202.                 if [ $? != 0 ]
  203.                 then
  204.                         echo "Error making md5sum of backup. Errors are stored in $BACKUP_LOG"
  205.                         md5sum --help
  206.                         exit
  207.                 fi
  208.         else
  209.                 COUNTS=$[$PART_TOGGLE * 2]
  210.                 # record log file
  211.                 echo "COUNTS is $COUNTS" >> $BACKUP_LOG
  212.                 for ((a=1;;a++))
  213.                 do
  214.                         # record log file
  215.                         echo >> $BACKUP_LOG
  216.                         if [ "$SIZE" -ge "$PART_TOGGLE" ]
  217.                         then
  218.                                 TEMP_DEST_TAR="$DEST_TAR"."$a"
  219.                                 # record log file
  220.                                 echo "TEMP DEST TAR is $TEMP_DEST_TAR" >> $BACKUP_LOG
  221.                                 tar czvfp - $SOURCE | dd of=$TEMP_DEST_TAR \
  222.                                 skip=$[$COUNTS * $[$a - 1]] \
  223.                                 count=$COUNTS >> $BACKUP_LOG 2>&1
  224.                                 if [ $? != 0 ]
  225.                                 then
  226.                                         echo "Error making backup. Errors are stored in $BACKUP_LOG"
  227.                                         dd --help
  228.                                         exit
  229.                                 fi
  230.                                 md5sum $TEMP_DEST_TAR >> $DEST_NAME.md5 2>>$BACKUP_LOG
  231.                                 if [ $? != 0 ]
  232.                                 then
  233.                                         echo "Error making md5sum of backup. Errors are stored in $BACKUP_LOG"
  234.                                         md5sum --help
  235.                                         exit
  236.                                 fi
  237.                                 # size of last backuped part of file
  238.                                 SIZE="`ls -lk $TEMP_DEST_TAR | awk '{ print $5}'`"
  239.                                 # record log file
  240.                                 echo "SIZE is $SIZE" >> $BACKUP_LOG
  241.                         else
  242.                                 break
  243.                         fi
  244.                 done
  245.         fi
  246.        
  247.         # record log file
  248.         echo "backup succeeded" >> $BACKUP_LOG
  249.         ls -lRa $SOURCE_PATH > $DEST_NAME.list        # keep a list of this source
  250.         # record log file
  251.         echo "file list made" >> $BACKUP_LOG
  252.        
  253.         # make a log file
  254.         if [[ $LOG_TOGGLE == 'yes' || $LOG_TOGGLE == 'Yes' || \
  255.         $LOG_TOGGLE == 'YES' || $LOG_TOGGLE == 'Y' || \
  256.         $LOG_TOGGLE == 'y' ]]
  257.         then
  258.                 echo "# Log file for $DEST_NAME" > $DEST_NAME
  259.                 echo "# Written by `whoami`" >> $DEST_NAME
  260.                 echo "# `date +"%b %d %Y"`" >> $DEST_NAME
  261.                 $EDITOR $DEST_NAME.log
  262.                 # record log file
  263.                 echo "log file made" >> $BACKUP_LOG
  264.         fi
  265. }

  266. usage() {
  267.         echo 'Usage: backup.sh [OPTION]'
  268.         echo 'Options:'
  269.         echo '  all                backup all the entries specified in the config file'
  270.         echo '  weekly        backup entries start with 'weekly' in the config file'
  271.         echo '  monthly        backup entries start with 'monthly' in the config file'
  272.         echo '  -h, --help        display the help and exit'
  273.         echo
  274.         echo 'Config file:'
  275.         echo 'config file must be present in your home directory with
  276. the name of .backuprc. There are four options you can specify in
  277. it. They are DEST, PART_TOGGLE, LOG_TOGGLE and SP. The meaning
  278. for them are "destination directory you want all backup files
  279. in", "the size of each part of the target backup file, 0 means
  280. do not cut, any number bigger than 0 will be treated as
  281. Kilobytes of each part of the file.(default is 0)", "whether or
  282. not you want to have a log when you backup. (default is no)",
  283. "the sign of telling the script the following lines are pathes
  284. you want to be backuped", respectively. Each option name must be
  285. followed by a colon and a space, then the value. The options
  286. DEST and SP have to exist in order to have the script work. The DEST
  287. option must comes at the beginning and SP must be put at the end of the
  288. config file.'
  289.         echo
  290.         echo 'Example of .backuprc:'
  291.         echo '        DEST: /mnt/usbc'
  292.         echo '        PART_TOGGLE: 10240'
  293.         echo '        LOG_TOGGLE: yes'
  294.         echo '        SP:'
  295.         echo '        weekly /root'
  296.         echo '        monthly /etc'
  297. }

  298. # Start of the script

  299. # check if the script is run as root
  300. asroot

  301. # check the existance of the config file
  302. if [ ! -f $HOME/.backuprc ]
  303. then
  304.         config
  305. fi

  306. # help
  307. if [[ $1 == '--help' || $1 == '-h' ]]
  308. then
  309.         usage
  310. elif [[ $1 == 'all' || $1 == 'weekly' || $1 == 'monthly' ]]
  311. then
  312.         readconf
  313. else
  314.         usage
  315. fi
复制代码
回复 支持 反对

使用道具 举报

发表于 2005-1-26 16:34:39 | 显示全部楼层
恕我直言,我很难理解为什么要这么复杂
执行频率是cron的职责
这是我自己用的:
[php]
#!/bin/sh
DIR_BAK=/backup
today=`date '+%Y-%m-%d'`

cd /

### keep 3 newest files
rm -f $(ls -t1 /backup/full* | sed -e 1,3d)
rm -f $(ls -t1 /backup/sql* | sed -e 1,3d)

### backup files
tar jclfp $DIR_BAK/full-system-$today.bz2 /
### backup sql
/usr/local/mysql/bin/mysqldump -u auser -ppass db > $DIR_BAK/sql-$today.sql

[/php]

crontab -l
[php]
1  1  *  *  *  /backup/script/rc.backup
[/php]
回复 支持 反对

使用道具 举报

发表于 2005-1-26 16:51:09 | 显示全部楼层
还有如果一定要有个config文件的话,source或 . 就好了
例:
backup.conf:
[php]
SRC=。。。
DST=。。。
。。。
[/PHP]
[PHP]
source backup.conf
[/php]

root user?
[php]
test $UID && echo "lease run this script as root!" && exit
[/php]
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-1-26 17:55:18 | 显示全部楼层
呵呵,谢谢ServerOnly。
因为我刚开始写脚本,觉得好玩就弄了这个东东,纯属练习吧,你的方法很好!
回复 支持 反对

使用道具 举报

发表于 2006-5-30 10:52:53 | 显示全部楼层
这个脚本在自动执行的时候有问题呀,配置文件应该放在什么地方?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-5-30 17:12:22 | 显示全部楼层
放在自己的home目录下就行了
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表