开发者

Bash script: Tar not working properly

I've just put together the below shown bash script in order to make continuous backups from a set folder.

It appears however that the find call seems to not only include /home/myfoldertobackup/ in the backup, but also the tar it checks file age of. Why on earth is this and what can I do to only make sure that the previously mentioned folder only is put into the tar?

if [ -e $filename ];
then
    # Tar exist. Check if tar 开发者_如何学Cis 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


If "$filename" last modification time is older than 5 days, find runs the following command:

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

which puts /home/myfoldertobackup and $filename (probably truncated since tar is going to write on it) in $filename; if you don't want $filename just write

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

or better (because more readable)

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

UPDATE

Here is your code with my proposed change:

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


that is because find replaces {} for the name of the file it found. Just drop {} and you are done.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜