简体   繁体   English

Linux Shell脚本如何使用相同的输出进行第二次操作

[英]Linux shell script how to use the same output for second operation

This script has been create to backup files 24 hours before from now (midnight crontab), but my concern is, while the first tar is being created (first find) a lot of "" files are being created, so when the second find is executed, it would remove more files than the first one, so the removed files would be more than the backed up. 此脚本已创建,以从现在开始24小时备份文件(午夜crontab),但是我担心的是,在创建第一个tar(第一次查找)的同时,正在创建许多“”文件,因此当第二次查找是执行后,它将删除比第一个文件更多的文件,因此删除的文件将比备份的更多。

Script edited on February 4th 2014, focus on loop for removing the listed files on temp file 脚本于2014年2月4日编辑,着重于删除临时文件中列出文件的循环

    #!/bin/sh
    DATE=`date '+%Y%m%d'`
    LOCATION=/home/see/rbirun/send/VPN/Mobile/
    FILE="cdr_backup_${DATE}.tar.gz"
    TEMPFILE="tempfile_${DATE}"
    ERROR="Error:Tar file not created nor removed the source call records"
    cd "${LOCATION}"
    find . -name 'VPN_CALLRECORD*' -type f -ctime 1 -print > ${TEMPFILE}
    tar -czf ${FILE} -T ${TEMPFILE}
    if [ -f $FILE ];
    then

            number=$(more ${TEMPFILE} |wc -l)
            echo "$DATE:Number of files backed up-->$number ">>log_after_cdr_backup.txt
            while IFS= read -r line
            do
                rm -f $line
            done < "${TEMPFILE}"
    else
            echo "$DATE:$ERROR">>log_after_cdr_backup.txt
    fi             

Is there a way to ensure that the same source backed up is removed ? 有没有办法确保删除相同的备份源? maybe using 'xargs' ? 也许使用“ xargs”?

thanks in advance for your valuable help 在此先感谢您的宝贵帮助

如果您有足够的空间,请首先将找到的文件移动到工作文件夹中,然后在备份后删除该工作文件夹。

Your problem is you run find twice. 您的问题是您两次运行find Between the first invocation and the second anything could happen. 在第一次调用和第二次调用之间,可能发生任何事情。

Do something like this instead: 做这样的事情:

#!/bin/sh
DATE=`date '+%Y%m%d'`
FILE="cdr_backup_${DATE}.tar.gz"
DESTINATION=/root/patrick_temp/
ERROR="Tar file has not been created nor removed the source call records"

BACKUP_FILES=$(find . -name 'VPN_CALLRECORD*' -type f -ctime 1 -print)
tar -czvf ${FILE} ${BACKUP_FILES}
if [ -f $FILE ];
then
    mv cdr_backup* ${DESTINATION}
    rm -rf ${BACKUP_FILES}
else
    echo $ERROR 
fi

If you might have LOTS of files to backup you can write the file names to a temp file, use that file for the tar invocation, and then use a loop to remove all of the files listed in the temp file. 如果可能有很多文件要备份,则可以将文件名写入临时文件,将该文件用于tar调用,然后使用循环删除临时文件中列出的所有文件。

EDIT based on your recent update. 根据您最近的更新进行编辑。

#!/bin/sh
DATE=`date '+%Y%m%d'`
LOCATION=/home/see/rbirun/send/VPN/Mobile/
FILE="cdr_backup_${DATE}.tar.gz"
# use mktemp to make the temp file. This is more secure and better practice.
TEMPFILE=$(mktemp)  # --tmpdir lets you place the created temp file wherever you need it
ERROR="Error:Tar file not created nor removed the source call records"

cd "${LOCATION}"

find . -name 'VPN_CALLRECORD*' -type f -ctime 1 -print > ${TEMPFILE}

tar -czf ${FILE} -T ${TEMPFILE}

# first, cleanup temps

# no need for `more` here. Also, `cat` is more typically used when this is needed
number=$(wc -l ${TEMPFILE})  
echo "$DATE:Number of files backed up-->$number ">>log_after_cdr_backup.txt
for line in $(cat ${TEMPFILE})
do
    rm -f $line
done
rm ${TEMPFILE}

# now process activity

if [ ! -f $FILE ];
then
    echo "$DATE:$ERROR">>log_after_cdr_backup.txt
    exit 1
fi

I hope this settles your questions. 我希望这能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM