Help using tar!

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

Post Reply
AimsB
Forum Newbie
Posts: 12
Joined: Thu Jul 08, 2004 9:39 am
Location: UK

Help using tar!

Post by AimsB »

Hi all,

Sorry if this is in the wrong place, I wasn't sure what category "tar" would come under. I have used tar before to backup an entire site in one go, just specifying to start in the route directory. But now I need to just backup a few different files from various places within the directory structure. So first of all I just want to check, it is possible to just add singular files to the archive file isn't it? Just by listing off the filenames instead of giving one directory?

Assuming I'm right about being able to do that, my other problem is that when I put these individual files into the archive, I need to put them into directories. So it may be in /this/handy/folder on the website, but needs to be in /this/backup/folder in my archive. So is it possible to state the filename to archive followerd by the folder to put it in when it's archived?

Or, if that's not possible, can I at least rename the files before they are put into the archive, but still preserving the names of the original files? So it's called myfile.php on the server, but mybackupFile.php in my archive.

If I can't do any of this then the only thing I can think of is to create the directories that I need, copy the files into those relevant directories, add the directories to the tar file, and then delete those copied files and new directories. Much more messy though, so any suggestions are welcome!

Thanks
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

there's --append (-r) option.
Why would you need to have different names in your archive?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Meaby you want to have a look at bacula or duplicity. Those are programs that use tar and diff to make backups...
AimsB
Forum Newbie
Posts: 12
Joined: Thu Jul 08, 2004 9:39 am
Location: UK

Post by AimsB »

It needs to be 'on the fly' unfortunately, so the user enters into a form what they want to back-up, and it simply returns a link on the next page to their nicely backed-up file!
The archive needs to contain extra information about the selections the user made for the backup. It's a bit long winded, but for example maybe I gave them a selection of fruit and they selected "apples" and "oranges" - in which case some of the files in their archive need to either have "apples_" added to the front of the filename, or need to be in a folder called "apples" and equally some need to either have "oranges_" added to the front of the filename, or need to be put in a folder called "oranges". But on the server, all of these files may be in a folder called /my/downloads/. Weird I know...

So if I was to use --append on a file in a certain folder on the server, would it preserve the folder it was in when it adds it to the archive file? And if that folder already exists in the archive, would it just add the file without deleting the rest of the existing content? And is there a way to rename it at the same time? If the answer to all those things is yes, that would be perfect!
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

relative file paths are preserved in tar (there's a way to store absolute paths as well). As for rename option, consult the tar manpage.
AimsB
Forum Newbie
Posts: 12
Joined: Thu Jul 08, 2004 9:39 am
Location: UK

Post by AimsB »

Thanks for everyones advice! If anyone else ever needs to do something similar, I found the best option was to not use tar at all! There doesn't seem to be a way of renaming things with tar which was a big draw back. In the end I found a class in Zend for creating Zip files which allows you to create folders within a zip file and add data with any name to those folders. Exactly what I needed! It's at http://www.zend.com/codex.php?id=696&single=1
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

For the record, "tar -c folder/ > tarball.tar"

Then you can "gzip -9 tarball.tar" or better yet, use bzip2 and "bzip2 tarball.tar" to get a tarball.tar.bz2
Post Reply