Forum Moderators: bakedjake
I'm trying to schedule a backup that will look in a directory and stick everything with a certain file extension into a tar archive but I can't quite get the syntax right, I'm trying something like this:
tar -cvzf ./backups_012006.tgz ¦ grep -l '' *.txt
But that doesn't do the trick, any ideas?
Cheers
I'm trying to schedule a backup that will look in a directory and stick everything with a certain file extension into a tar archive but I can't quite get the syntax right, I'm trying something like this:
tar -cvzf ./backups_012006.tgz ¦ grep -l '' *.txt
But that doesn't do the trick, any ideas?
Cheers
Your command is a little backwards. Not need to use grep to do anything, just use tar and find:
find . -name "*.txt" -print ¦ tar czf files.tar.gz -T -
To add other file types to the backup, use:
find . -name "*.txt" -o -name "*.doc" -print ¦ tar czf files.tar.gz -T -
Cheers,
MM
That command you provided runs great, so thanks for that. I have tried to extend it to add the -remove-files option of tar but I can't get it right, I'm obviously sticking it in the wrong place, how should I adjust this:
find ./ -name "*.txt" -print ¦ tar czf --remove-files archive/db_012006.tar.gz -T -
Many thanks
Thanks for dropping by again, not much in there really:
--remove-files
remove files after adding them to the archive
Heres the usage:
Usage: tar [-AcdrtuxGknOSUWmpsMBijzZhPvRwo?] [-g FILE] [-f ARCHIVE] [-F NAME]
[-L NUMBER] [-b BLOCKS] [-H FORMAT] [-V TEXT] [-C DIR]
[-K MEMBER-NAME] [-N DATE-OR-FILE] [-T FILE-OF-NAMES] [-X FILE]
[--catenate] [--concatenate] [--create] [--diff] [--compare]
[--delete] [--append] [--list] [--update] [--extract] [--get]
[--listed-incremental=FILE] [--incremental] [--ignore-failed-read]
[--keep-old-files] [--keep-newer-files] [--no-overwrite-dir]
[--seek] [--occurrence[=NUMBER]] [--overwrite] [--to-stdout]
[--recursive-unlink] [--remove-files] [--sparse] [--unlink-first]
[--verify] [--atime-preserve] [--group=NAME] [--mode=CHANGES]
[--touch] [--no-same-owner] [--no-same-permissions]
[--numeric-owner] [--owner=NAME] [--preserve-permissions]
[--same-permissions] [--preserve] [--same-owner]
[--preserve-order] [--same-order] [--file=ARCHIVE] [--force-local]
[--info-script=NAME] [--new-volume-script=NAME]
[--tape-length=NUMBER] [--multi-volume] [--rmt-command=COMMAND]
[--rsh-command=COMMAND] [--volno-file=FILE]
[--blocking-factor=BLOCKS] [--read-full-records] [--ignore-zeros]
[--record-size=NUMBER] [--format=FORMAT] [-- gnu] [-- oldgnu] [--
pax] [-- posix] [-- ustar] [-- v7] [--bzip2] [--old-archive]
[--portability]
[--pax-option=keyword[[:]=value][,keyword[[:]=value], ...]]
[--posix] [--use-compress-program=PROG] [--label=TEXT] [--gzip]
[--gunzip] [--ungzip] [--compress] [--uncompress]
[--after-date=DATE] [--anchored] [--backup[=CONTROL]]
[--directory=DIR] [--exclude=PATTERN] [--exclude-caches]
[--dereference] [--ignore-case] [--starting-file=MEMBER-NAME]
[--newer-mtime=DATE] [--no-anchored] [--no-ignore-case]
[--no-recursion] [--no-wildcards] [--no-wildcards-match-slash]
[--null] [--newer=DATE-OR-FILE] [--one-file-system]
[--absolute-names] [--recursion] [--strip-components=NUMBER]
[--suffix=STRING] [--files-from=FILE-OF-NAMES] [--wildcards]
[--wildcards-match-slash] [--exclude-from=FILE] [--checkpoint]
[--verbose] [--check-links] [--index-file=FILE] [--block-number]
[--show-defaults] [--show-omitted-dirs] [--totals] [--utc]
[--interactive] [--confirmation] [--help] [--license] [--usage]
[--version] [FILE]...
The only thing I can think of right now is that the tar command doesn't remove anything that is piped to it... (the "-" at the end indicates teh list will come from either a pipe (the "¦") or some other form of list, like being typed in).
What you could do is something of this kind:
find ./ -name "*.txt" -print ¦ tar czf --remove-files archive/db_012006.tar.gz -T - && find ./ -name "*.txt" -exec rm {} \;
The problem with this is that you're running 2 find's for the same data... Might not be so bad with only a few files, but once you start reaching hundreds, it may make a difference.
If you do that, be sure to use the "&&", and not just running the second find independently. The reason for this is that the "&&" means "only run the following command if the previous command was successful". The reason I mention this is because your .txt files would be nuked if the first command didn't complete successfully (ie: tar failed for some reason).
You may want to consider building this into a more complex script as well; one that generates timestamps for you as well... If you need to start something like that, just let me know and I can write up a quick sample for you.
Some searching around seemed to bring up some bug reports with regard to the --remove-file so I'll go with your solution.
The point about the && is understood, I think you have given me the final piece of my automated backup jigsaw!
Many thanks