日归档脚本

来源:互联网 发布:stm32单片机教程 pdf 编辑:程序博客网 时间:2024/06/01 19:21
声明:允许转载,转载请注明链接,谢谢合作!
下面分享一个我在工作中使用的每天归档文件的脚本。
主要的功能就是备份一个文件夹下的所有需要备份的文件达成tar包
脚本 如下:
#!/bin/sh
#daily_archive.sh    脚本名称
#Daily_Archive - Archive designated files &directories
#日归档脚本,定向备份文件和目录
#OPERATE_DATE        操作数据日期
#author:wangxin   version:1.0 
#create_date:20160114
##################################################################
#Gather Current Date
#获取当前日期
OPERATE_DATE=`date +%y%m%d`
#
#Set Archive File Name
#设置归档文件的文件名
FILE=archive29_$OPERATE_DATE.tar.gz
#
# Set Configuration and Destination File
#设置配置文件和目标目录
#
FILE_NAME=archive_filename.txt
CONFIG_FILE=/data01/backup/$FILE_NAME
DESTINATION=/data01/backup/$FILE
#
##############   Main  Script  #########################
#主脚本
find  /home/hadoop/ -name '*.*' -exec ls {}\; > /data01/backup/$FILE_NAME
#使用find 得出需要备份的目录的所有符合正则表达式的文件
# Check Backup Config file exists
#检查需要备份的配置文件是否存在
#
if [ -f $CONFIG_FILE ]  # Make sure theconfig file still exists
then               # If it exists. do nothingbut continue on.
    echo
else               # If it doesn't exist.issueerror & exit script.
    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     #Start on line1 of Config File.
exec < $CONFIG_FILE  # Redirect Std Inputto name of Config File
#
read FILE_NAME   # Read 1st record
#
while [ $? -eq 0 ]   # Create list of filesto backup
do
               # Make surethe file or directory exists.
        if [ -f $FILE_NAME -o -d$FILE_NAME ]
        then
               # If fileexists. add its name to the list.
              FILE_LIST="$FILE_LIST $FILE_NAME"
        else
               # If filedosen't exist. issue warning
           echo
           echo "$FILE_NAME does not exist."
           echo "Obviously. I will not include it in thisarchive."
           echo "It is listed on line $FILE_NO of configfile."
           echo "Continuing to build archive list ..."
           echo
        fi
#
        FILE_NO=$[FILE_NO + 1] #Increase Line/File number by one.
        read FILE_NAME       # Read next record.
done
#
#####################################################################
#
#  Backup the files and CompressArchive
#
tar -czf $DESTINATION $FILE_LIST 2> /dev/null
#