Find and Tar Trouble

I’m trying to do a backup of documents that have changed in the last seven days.
The command I’m using to find them is

find /home/chris/Documents -mtime -7

This works fine. However doing the folloing

find /home/chris/Documents -mtime -7 | xargs tar -cvzf DocsBackup.tar.gz

starts to backup the correct files then ignores the find and copies everything AFTER the first file that find lists!!

find /home/chris/Documents -mtime -7 -exec tar -zvcf DocsBackup.tar.gz \;

just gives me errors complaining about not creating an empty tar.

My other idea was to copy it to a folder and then tar that. However the command

find /home/chris/Documents -mtime -7 -exec cp {} ~/DocsBackup \;

appears to work, but all the files within the folder are corrupt!

Can anyone point me at where I’m going wrong?
The find command is working fine!

EDIT:
They would be corrupt because the source files are corrupt somehow! :frowning:

dirty answer

find /home/chris/Documents -mtime -7 > /tmp/find.out
tar cvzf -I /tmp/find.out tarfile.tar.gz

probably a much easier way with “-exec cp {}” but I’m too busy to think right now :wink:

/edit: sure the find is working - shouldn’t it be “-mtime +7” ?

I did a google for the find as I wasn’t sure.

+7 finds files over 7 days old
-7 finds files changed within the last 7 days

Once I’ve copied all my documents back (so they aren’t corrupt), I’ll try that answer - however it may mean all the modified dates are now wrong so I’ll try on another folder.

EDIT: With -I it creates a file -I in my home. Looking through the tar usage, there’s no -I option.

Try adding -type f to the find so it doesnt add directories and thier contents if the directory has been updated.

Excellent, thanks for that. Now one step closer. It now adds all the correct files only.

However now it refuses to add anything with a space in in (Random Picture.jpg isn’t copied)
Short of removing all the spaces, is there an easier way?

[QUOTE=drezha;435030]I did a google for the find as I wasn’t sure.

EDIT: With -I it creates a file -I in my home. Looking through the tar usage, there’s no -I option.[/QUOTE]

odd, probably Solaris being “unique” again :ghey:

the -I option is to specify an input file… my idea was to find all files that match criteria and output that list to a text file. Then use that text file as the input file to the tar command, but different tar implementations behave slightly differently… man tar :wink:

regarding the invalid filename problem… there’s more than likely a way of doing something with inodes (hope this isn’t a solaris thing again)… each file should have a completely unique inode number to identify it.
There should be an option in find to display the inode number - or you could always “-exec ls -ail {} ;” at the end - man find / ls to see the options for displaying inodes. Then hopefully tar has a mechanism for tarring up files by inode number - man tar :wink:

I can’t check any of the above atm cos I don’t have access (without vpn’ing to work :o ) so I’ll check in the morning if I remember :slight_smile:

Ah ha! Got it!
Combination of man tar and man find and I think I’ve nailed it.

find /home/chris/Documents -type f -mtime -7 > /tmp/find.out
tar --files-from /tmp/find.out -cvzf tarfile.tar.gz

:trophy:

Now I’ve just changed it to one command by adding && so tar will run when find is done.

Now to find out how to make the tar file name take the current date (so DocsBackupDATE.tar.gz)
I believe I have to make it into a script and make the date a variable.

#!/bin/ksh
DT=date +20%y-%m-%d

tar --files-from /tmp/find.out -cvzf tarfile.$DT.tar.gz (or $DT.tarfile.tar.gz)

Sorted, Cheers for the help :smiley: