1 #!/bin/bash 2 # 3 #This is a test in book.thanks for Richard Blum. 4 #Please put this file to crontab,thanks. 5 #Please touch a new configuration file. 6 #Daily_Archive - Archive designated files & directories 7 # 8 ########################################################## 9 # 10 #Gather Current Date.the format of date is only " date +%d%m%y". 11 # 12 # 13 DATE=`date +%y%m%d` 14 # 15 echo $DATE 16 #Set Archive File Name 17 # 18 FILE=backup$DATE.tar.gz 19 # 20 #Set Configuration and Destination File.The configuration file include directories that you want to backup. 21 # 22 CONFIG_FILE=/home/ach/test/shell-practices/backup-daily.conf 23 DESTINATION=/home/ach/backup/$FILE 24 # 25 ############ Main Script ################################## 26 # 27 #Check Backup Config file exists 28 # 29 if [ -f $CONFIG_FILE ] #Make sure the config file still exists. 30 then #If it existd,do nothing but continue on. 31 echo 32 else #If it doesn't exist,issue error & exit script. 33 echo 34 echo "$CONFIG_FILE does not exist." 35 echo "Backup not completed due to missing Configuration File." 36 echo 37 exit 38 fi 39 # 40 #Build the names of all the files to backup 41 # 42 FILE_NO=1 #Start on Line 1 of Config File 43 exec < $CONFIG_FILE #Redirect Std Input to name of Config File. 44 # 45 read FILE_NAME #read 1st record. 46 # 47 while [ $? -eq 0 ] #Create list of files to backup 48 do 49 #Make sure the file or directories exists. 50 if [ -f $FILE_NAME -o -d $FILE_NAME ] 51 then 52 #If file exists, add its name to the list. 53 FILE_LIST="$FILE_LIST $FILE_NAME" 54 else 55 #If file does not exist,issue warning. 56 echo 57 echo "FILE_NAME does not exist." 58 echo "Obviously,I will not include it in this archive." 59 echo "It is listed on line $FILE_NO of the config file." 60 echo "Continuing to build archive list......" 61 echo 62 fi 63 # 64 FILE_NO=$[FILE_NO+1] #Increase Line/File number by one. 65 read FILE_NAME #read next record. 66 done 67 # 68 ############################################################ 69 # 70 #Backup the files and compress archive 71 # 72 tar -czf $DESTINATION $FILE_LIST 2> /dev/null 73 #To decompress use command : -xf (extract) 74 #END with the backup-daily 75 #Next is backup-hour,which will use a new method that can level directories. 76 # 77 #DAY=`date +%d` 78 #MONTH=`date +%m` 79 #TIME=`date +%M` 80 # 81 #mkdir -p $BASEDEST/$MONTH/$DAY 82 # 83 #DESTINATION=$BASEDEST/$MONTH/$DAY/$TIME.tar.gz 84 # 85 # 86 # 87 ##END