6

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

2
  • 1
    You have the wrong syntax for your tar options. First, you need a space between "-cvf" and "--newer-mtime=08-11-2013", '-cvf --newer-mtime=08-11-2013' and second the date syntax is 20130811 , see stackoverflow.com/questions/10367037/… . As an alternate, and perhaps simpler backup, consider rsync ;)
    – Panther
    Feb 18, 2014 at 17:59
  • I rsync everything from another server to this one to archive it.
    – troylatroy
    Feb 18, 2014 at 18:21

2 Answers 2

7

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).

1
  • +1 for pointing out use of tar filename just after -f option.
    – sourav c.
    Feb 18, 2014 at 18:52
2

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.

3
  • 1
    There was something wrong - there was no space the -cvf. Also, @bodhi was right in that the date syntax was wrong. Good anyway +1
    – Wilf
    Feb 18, 2014 at 18:25
  • 1
    yes you are correct. at the same time OP was trying to gzip a file that does not exist. is not it?
    – sourav c.
    Feb 18, 2014 at 18:30
  • Yep :D @souravc
    – Wilf
    Feb 18, 2014 at 18:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .