linux-tutorial/codes/linux/tool/Daily_Archive.sh

68 lines
1.5 KiB
Bash
Raw Normal View History

2019-10-29 18:22:19 +08:00
#!/usr/bin/env bash
2019-10-10 08:56:31 +08:00
2019-05-10 11:17:57 +08:00
#
# Daily_Archive - Archive designated files & directories
######################################################
#
# Gather Current Date
#
DATE=`date +%y%m%d`
#
# Set Archive File Name
#
FILE=archive$DATE.tar.gz
#
# Set Configuration and Destination File
2019-10-10 08:56:31 +08:00
#
2019-05-10 11:17:57 +08:00
CONFIG_FILE=/home/tiandi/archive/Files_To_Backup
DESTINATION=/home/tiandi/archive/$FILE
#
##################### Main Script ###############
#
# Check Backup Config file exists
#
2019-10-10 08:56:31 +08:00
if [ -f $CONFIG_FILE ] #Make sure the config file still exists
2019-05-10 11:17:57 +08:00
then
2019-10-29 18:22:19 +08:00
echo
2019-05-10 11:17:57 +08:00
else
2019-10-29 18:22:19 +08:00
echo
echo "$CONFIG_FILE does not exist."
echo "Backup not completed due to missing Configuration file"
echo
exit
2019-05-10 11:17:57 +08:00
fi
#
# Build the names of all the files to backup
#
2019-10-10 08:56:31 +08:00
FILE_NO=1 # Start on Line 1 of Config file.
exec < $CONFIG_FILE # Redirect Std Input to name of Config File
2019-05-10 11:17:57 +08:00
#
2019-10-10 08:56:31 +08:00
read FILE_NAME # Read 1st record
2019-05-10 11:17:57 +08:00
#
while [ $? -eq 0 ]
do
2019-10-29 18:22:19 +08:00
# Make sure the file or directory exists.
if [ -f $FILE_NAME -o -d $FILE_NAME ]
then
# If file exists, add its name to the lists
FILE_LIST="$FILE_LIST $FILE_NAME"
else
# If file doesn't exist, issue warning
echo
echo "$FILE_NAME, does not exist."
echo "Obviously, I will not include it in this archive."
echo "It is listed on line $FILE_NO of the config file."
echo "Continuing to build archive file."
echo
fi
#
FILE_NO=$[ $FILE_NO + 1 ] # Increase Line/File number by one
read FILE_NAME # Read next record.
2019-05-10 11:17:57 +08:00
done
###########################################################
2019-10-10 08:56:31 +08:00
#
2019-05-10 11:17:57 +08:00
# Backup the files and Compress Archive
#
tar -czf $DESTINATION $FILE_LIST 2> /dev/null
#