Shell 学习(七):简单实用脚本

10 篇文章 0 订阅
4 篇文章 0 订阅

重要文件备份归档

将文件备份到/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
            
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值