What is going on with my TAR script?

This might do better at StackExchange but I use Ubuntu as my file server.

So I'm trying to use tar and gzip to only backup the last 6 months of changes on my file server and it isn't working. It doesn't gzip anything, it puts the tar file in the same directory as the script(not what i want), and it changes the name of the file to part of the tar string.

Here is the script:

#!/bin/bash


tod=$(date +%F_%H%M%S)
echo "start"
echo $tod

echo "testing tar, only the last 6 months"
tar -cvf--newer-mtime=08-11-2013 /homedepot/yellowsolo/xz/6months/xz$tod-last6months.tar /homedepot/yellowsolo/xz/official/official /homedepot/yellowsolo/xz/home/home

echo "now zipping"
gzip /homedepot/yellowsolo/xz/6months/xz$tod-last6months.tar.gz

echo $tod
echo "done"
exit

Thanks in advance


Try:

#!/bin/bash
tod=$(date +%F_%H%M%S)
echo "Start"
echo $tod

echo "testing tar, only the last 6 months"
tar --newer-mtime=20130811 -cvzf /homedepot/yellowsolo/xz/6months/xz$tod-last6months.tar /homedepot/yellowsolo/xz/official/official /homedepot/yellowsolo/xz/home/home

echo "Done"
exit

I have cleaned up the errors in the tar command - you need to have f option before the filename, and filtered it through gzip (the z option).


At first you have tried to create a .tar file. Apart from the syntax error bodhi.zazen already pointed out your tar file name should come just after -f option. As Wilf aptly pointed out in his answer.

tar --newer-mtime=08-11-2013 -cvf /homedepot/yellowsolo/xz/6months/xz$tod-last6months.tar /homedepot/yellowsolo/xz/official/official /homedepot/yellowsolo/xz/home/home

Next there is another error. You are going to gzip /homedepot/yellowsolo/xz/6months/xz$tod-last6months.tar.gz but this file does not exist.

Rather you have a file /homedepot/yellowsolo/xz/6months/xz$tod-last6months.tar

You should use,

gzip /homedepot/yellowsolo/xz/6months/xz$tod-last6months.tar

It will create /homedepot/yellowsolo/xz/6months/xz$tod-last6months.tar.gz.

Note:

You can do it directly,

tar --newer-mtime=08-11-2013 -cvzf /homedepot/yellowsolo/xz/6months/xz$tod-last6months.tar.gz /homedepot/yellowsolo/xz/official/official /homedepot/yellowsolo/xz/home/home

-z switch zip tar simultaneously.