Shell 学习:简单实用脚本

Posted 漂亮姐姐1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Shell 学习:简单实用脚本相关的知识,希望对你有一定的参考价值。

重要文件备份归档

将文件备份到/archive目录下,注意修改/archive目录的权限
在/archive目录下的Files_To_Backup中添加需要备份的文件/目录的绝对路径

#!/bin/bash
# Daily_Archive - Archive files & directories
######################################################

# Gather current date
DATE=$(date +%y%m%d)

# Set archive file name
FILE=archive$DATE.tar.gz

# Set configuraion and desination file
CONFIG_FILE=/archive/Files_To_Backup
DESTINATION=/archive/$FILE

######################  Main Scrip  ###################

# Check backup config file exists
if [ -f $CONFIG_FILE ]
then
    echo
else
    echo
    echo "$CONFIG_FILE does not exist."
    echo "Backup not completed due to missing Configuration File"
    echo
    exit
fi

# Build the names of all the files to backup
FILE_NO=1                       		# Record the current line number
exec < $CONFIG_FILE                     # Change STDIN to config file                         
#
while read FILE_NAME                    # Read the first file name
do
    # Make sure the file or directory exists
    if [ -f $FILE_NAME -o -d $FILE_NAME ]
    then
        # If file exists, add its name to the list
        FILE_LIST="$FILE_LIST $FILE_NAME"
    else
        # If file doesn't exist, issue warning
        echo "Warning : $FILE_NAME,does not exist"
    fi
#
    FILE_NO=$[$FILE_NO+1]
done
#######################################################
# Backup the files and compress archive

echo "Starting the archive..."


# Create finger file under current dir
finger_file=$(mktemp  finger.txtXXXXXX)
find $FILE_LIST|xargs md5sum >$finger_file 

# archive and compress
FILE_LIST="$FILE_LIST $finger_file"
tar -zchf $DESTINATION $FILE_LIST 2>/dev/null

# remove the finger.txt file
rm -f $finger_file
echo 'Archive complete!'
echo "Archive file : $DESTINATION"
echo
exit
            

以上是关于Shell 学习:简单实用脚本的主要内容,如果未能解决你的问题,请参考以下文章

shell基础学习

高效编写Linux Shell脚本的9个小技巧

shell expect的简单实用

史上最全高效编写Linux Shell脚本小技巧

简单 Shell编程学习

高效实用k8s运行状态自动检测shell脚本