Bash脚本:Tar无法正常工作

时间:2011-01-21 15:07:27

标签: bash shell ubuntu sh

我刚刚将下面显示的bash脚本放在一起,以便从设置文件夹中进行连续备份。

然而,似乎find调用不仅在备份中包含/home/myfoldertobackup/,而且还在其检查文件年龄的tar中包含if [ -e $filename ]; then # Tar exist. Check if tar is older than five days and if so, update it. find $filename -mtime +5 -exec tar -czvf $filename /home/myfoldertobackup/ {} \; else #Tar doesn't exist. Force creation of tar! tar -czvPf $filename /home/myfoldertobackup/ fi 。为什么到底是这样,我该怎么做才能确保前面提到的文件夹只被放入tar?

{{1}}

2 个答案:

答案 0 :(得分:3)

如果“$ filename”的上次修改时间超过5天,则find会运行以下命令:

tar -cvfz $filename /home/myfoldertobackups/ $filename

将/ home / myfoldertobackup $ filename(可能会在tar写入时截断)$ filename;如果你不想要$ filename就写

find $filename -mtime +5 -exec tar -cvz /home/myfoldertobackups -f {} \;

或更好(因为更具可读性)

if [ -n "$(find $filename -mtime +5)" ]; then
    tar -cvzf "$filename" /home/myfoldertobackups;
fi

更新

以下是我建议更改的代码:

if [ -e $filename ];
then
    # Tar exist. Check if tar is older than five days and if so, update it.
    # find $filename -mtime +5 -exec tar -czvf $filename /home/myfoldertobackup/ {} \;
    if [ -n "$(find $filename -mtime +5)" ]; then
        tar -cvzf "$filename" /home/myfoldertobackups;
    fi
else
    #Tar doesn't exist. Force creation of tar!
    tar -czvPf $filename /home/myfoldertobackup/
fi

答案 1 :(得分:2)

这是因为find{}替换为找到的文件的名称。 只需删除{}即可完成。