Tarball

Place to discuss Fedora and/or Red Hat
Post Reply
kovax
scripter
scripter
Posts: 85
Joined: Mon Jan 24, 2005 9:47 am
Location: Jacksonville, USA
Contact:

Tarball

Post by kovax » Sun Jan 30, 2005 4:47 pm

What is the best way to create a tarball?
I have a few directories that i would like to back up and i hear that a tarball is the best way to do it.

Thanks

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Sun Jan 30, 2005 5:02 pm

I assume in addition to creating a tar file that you would also like to have it compressed (this is the normal way to do it). Say wanted to back up everything under /home/fred and /home/mary. Also say you don't want the "/home" part actually included in the directories that get archived. Say you want the resulting file to be created in the /backup directory and you want it compressed with gzip. These would be your commands (as root):

Code: Select all

# cd /home
# tar -cvzf /backup/userbackup.tgz fred mary
You can list the files in the archive you just made by:

Code: Select all

$ tar -tvzf /backup/userbackup.tgz
The "-cvzf" and "-tvzf" are flags. The "c" means "create", the "t" means "list", the "v" means "verbose", the "z" means "use gzip compression", the "f" means "file" or which archive file name are we working with. If you wanted to extract files from the archive you would use "x" instead of "t" or "c", just be aware of where you are and where the files will go when you extract them.

It would be best that you practice creating files without being root first, just so if you make a mistake you won't do a lot of damage. :)


Here's the man page:
http://voidmain.is-a-geek.net/man/?para ... r&mode=man

kovax
scripter
scripter
Posts: 85
Joined: Mon Jan 24, 2005 9:47 am
Location: Jacksonville, USA
Contact:

Post by kovax » Sun Jan 30, 2005 5:10 pm

Thanks!!
What if i wanted to store the file on a different server? Here is what i am looking to do:
I have a web server with a bunch of sites \websites
I would like to back that file up with a script or cronjob. Then move the file to my backup server.

Thanks is advance.

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Sun Jan 30, 2005 5:19 pm

For small things you can create a script to do your backups and have it run from a cron job. You could then transfer the backup files via scp. But if you have a lot of data I would suggest looking at rsync. It is much more efficient because it only transfers just the little bits that change.

Having said that I actually use tar to backup my important data on this site (it's small). I have a simple script that I run from root's cron called "bkupweb" that looks like this:

Code: Select all

#!/bin/sh

# Change into the directory where the backup files are stored:
cd /workspace

# Keep a history of 4 backups:
rm -f bkupweb.tgz.3
mv -f bkupweb.tgz.2 bkupweb.tgz.3
mv -f bkupweb.tgz.1 bkupweb.tgz.2
mv -f bkupweb.tgz bkupweb.tgz.1

# Back up important stuff
cd /
/bin/tar -cvzf /workspace/bkupweb.tgz etc/mrtg etc/httpd/conf home/voidmain var/www/error var/www/html var/www/voidmain
My cron job looks like this:

Code: Select all

0 4 * * * /root/bin/bkupweb > /workspace/bkupweb.log 2>&1
As you can see it runs at 4:00am and logs the output. I also have a script that runs under my own users cron to dump the MySQL database to a file so it will get inclued in the above backup. Then on another machine I have a cron job set to run to copy the latest backup file to a directory via scp (I have an ssh certificate set up on an ID that will allow the copy to occur without requiring a password). I can expand on that if you need it.

worker201
guru
guru
Posts: 668
Joined: Sun Jun 13, 2004 6:38 pm
Location: Hawaii

Post by worker201 » Mon Jan 31, 2005 12:44 am

Just a reminder to everyone that tar also supports bzip2, which compresses much more than gzip. Use the -j flag instead of the -z flag:

Code: Select all

tar -cvjf whatever
tar -xvjf whatever

caveman
programmer
programmer
Posts: 130
Joined: Sun Feb 09, 2003 1:08 pm
Location: Midrand Gauteng, South Africa

Post by caveman » Mon Jan 31, 2005 5:57 pm

and just a reminder...;)

Not to start the backup from the root directory ie. with the leading "/"
in front. eg.
# cd whereever
# tar -cvzf /backup/userbackup.tgz /home/fred /home/mary
rather
# cd /
# tar -cvzf /backup/userbackup.tgz home/fred home/mary

There are many reasons -
but the main one - you cannot restore the backup to a different place other
than the root directory - and this can overwrite something that you
may want to keep ('specially system directories) - specially if you
want to restore to a different server and the directory structure is not
exactly the same...

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Mon Jan 31, 2005 6:30 pm

Hey caveman, that's great advice except that now GNU tar strips the leading "/" off automatically. It's not a bad habit to be in of not including it though. I still never use the leading "/" as I have been bit many years ago. I suspect too many others were getting bit so they just changed the behavior of tar. I think the proprietary versions of tar (on Solaris, AIX, HP-UX, etc) still do not strip the leading slash, but who still uses that crap anyway? :) Even when I am using those system I still install and use the GNU utilities as they work much better than the proprietary versions.

caveman
programmer
programmer
Posts: 130
Joined: Sun Feb 09, 2003 1:08 pm
Location: Midrand Gauteng, South Africa

Post by caveman » Mon Jan 31, 2005 7:24 pm

my non-Linux sites are mostlly AIX and Solaris.. and the System guys
enjoy forcing the systems to stay "vanilla" (can understand their reasons).

So - yes - they still don't strip out the leading "/", and me working on
unix type systems since the middle eighties, I make bloody sure there
are no leading "/" when I make tarballs.

I made the comment 'cause I frequently have to tell the young guys
on the systems to watch out for it. Mostly they only forget once,
get bitten really hard, and then it becomes second nature.....

Haven't tried it, but I think the newer GNU CPIO program reads tar
archives and maybe - due to the way cpio works - it can fix the problem
of tarballs with a leading "/".
Anybody tried it yet? Is it possible?

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Mon Jan 31, 2005 8:33 pm

I was an AIX systems guy in the early 90s and I remember being able to extract archives with the leading "/" safely. I don't remember off the top of my head how it was done but it's surely out there in Internet land for Google to find. I install GNU file utils and loads of other goodies on all our Sun servers (www.sunfreeware.com is a good site with prebuilt stuff). IBM is moving the right direction and has RPM utilies on AIX and we install a lot of the same on our RS/6000 boxen. Once you get used to the flexibility of the GNU tools it's hard to not use them everywhere.

Post Reply