Skip to content

实例

创建归档脚本

bash
  1 #!/bin/bash                                                                           
  2 DATE=$(date +%y%m%d)
  3 
  4 FILE=archive$DATE.tar.gz
  5 CONFIG_FILE=/archive/File_To_Back
  6 DESTINATION=/archive/$FILE
  7 
  8 if [ -f $CONFIG_FILE ]   # 检查需要存储的文件用的目录存在不存在
  9 then
 10     echo
 11 else
 12     echo
 13     echo "$CONFIG_FILE dose not exits"
 14     echo "Backup not completed due to missing Configuration File" 
 15     echo
 16     exit
 17 fi
 18 
 19 FILE_NO=1
 20 exec < $CONFIG_FILE
 21 read FILE_NAME
 22 while [ $? -eq 0 ]
 23 do
 24     if [ -f $FILE_NAME -o -d $FILE_NAME ]
 25     then
 26         FILE_LIST="$FILE_LIST $FILE_NAME"		# 生成打包的文件的列表
 27 
 28     else                                                                              
 29         echo
 30         echo "$FILE_NAME doesn't exit"
 31         echo "Obviously, I will not include it in this archive."
 32         echo "It is listed on line $FILE_NO of the config file."
 33         echo "Continuing to build archive list..."
 34         echo
 35     fi
 36     FILE_NO=$[ $FILE_NO + 1 ]
 37     read FILE_NAME
 38 done
 39 
 40 echo "Starting archive..."
 41 echo 
 42 tar -czf $DESTINATION $FILE_LIST 2> /dev/null		# 打包
 43 echo "$FILE_LIST"
 44 echo "Archive complete"
 45 echo "Resulting archive file is: $DESTINATION"	
 46 echo
 47 exit