PDA

View Full Version : How-to : Using Tar (Taring)



ewdi
19th February 2004, 01:50 AM
By SuperHornet from http://www.fluidgravity.com/ (http://www.fluidgravity.com/)

Ok well here is a short listing on how to use the command tar to backup you data..
Tar is solely an archiving app. Tar by its self wont compress files.

But you say "then what is a .tar.gz"

It’s a tar file that has been compressed with a different compression utility. The .gz=gzip is the compression app use to compress it.

Here is tar in its simplest form




tar -cvf filename.tar /path/to/files



-c means create
-f means filename (-f should always be last when you using syntax)
-v Verbose will display all the files its puts in the tar and error you might have incurred
You should see the filename.tar file in what ever directory you ran tar from.

You say "But I want to make the tarball compressed"

Well then -z is the option you want to include in your syntax




tar -zvcf filename.tar.gz /path/to/files


#notice I had to add the .gz extension.
-Z( no not -z) will run it thru the old compress app.

Now when I make a tarball I like to keep all the path's from which the file is in.
For this use the -P (absolute path)




tar -zPvcf filename.tar.gz /path/to/file



When I extract it I will see a new directory called /path
and under that I will see the "to" directory, and the "file" is under "to"

Now you say "I want to backup ALL my files in my home directory EXCEPT the temp directory I use". No problem.




tar -zPvcf myhomebackup.tar.gz --exclude /home/erik/temp /home/erik



The --exclude will give you this option, just slip it in between the tar filename and the path your going to backup. This will exclude the whole temp directory.

You say "Ok this tar thing is pretty cool but I want to backup only single files from all around the drive.

No problem, this requires a bit more work, but hey this is UNIX, get used to it.

Make a file called locations (call it anything you like). In locations place the full path to each file you want to backup on a new line. Please be aware that you have to have read rights to the files you are going to backup.





/etc/mail/sendmail.cf
/usr/local/apache/conf/httpd.conf
/home/erik/scripts



Now with the -T option I can tell it to use the locations file.




tar -zPvcf backup.tar.gz -T locations



Now if you want to backup the whole drive. Then you will have to exclude lots of files like /var/log/* and /usr/local/named/*

Using the -X option you can create an exclude file just like the locations file.




tar -zPvcf fullbackup.tar.gz -X /path/to/excludefile -T /path/to/locationsfile



Now a month has gone by and you need to update your myhomebackup.tar.gz with new or changed files.

This requires a extra step (quit your bitching I already told you why)
You have to uncompress it first but not untar it.




gunzip /path/to/myhomebackup.tar.gz


This will leave your myhomebackup.tar.gz mising the .gz.
Now we can update your tarball with -u and then we are going to compress it again.




tar -Puvf myhomebackup.tar /home/erik | gzip mybackup.tar


It will add the .gz for you.

Tar is a pretty old app and has lots of Fetchers.
I suggest reading the man pages to get a lits of all the options.


I have included a little perl script that I made so I can run it as cron job evernight and get a full backup each time.
It wouldn't be that hard to update the tarball but I just like full backups.
Feel free to use it.

If you want to extract the tarball that is compressed




tar -zxvf filename.tar.gz


-x extract

If it is not compressed then




tar -xvf filename.tar





#!/usr/bin/perl
#sysbkup.pl
#Created by Erik Mathis hornet@fluidgravity.com 7/02

#Change These paths to fix your needs.
my $filename="/home/sysbkup/backup";
my $exclude="/home/erik/exclude";
my $data="/home/erik/locations";
my $tar="\.tar";
my $gz="\.gz";


$file=$filename.$tar.$gz;

system ("tar -Pzcvf $file -X $exclude -T $data");

Bana
19th February 2004, 06:11 AM
Nice How-to, If I may add something:

BZ2 Extension
For those files that you may download that look like file.tar.bz2 tar can still take care of your needs, just use tar -xjf rather than xzf and it is as easy as that. No more wasting bandwith for you.

----

Now I have a question: is there a way to compress using the tar program? maybe tar -cJf ?

mhelios
19th February 2004, 09:19 AM
Now I have a question: is there a way to compress using the tar program? maybe tar -cJf ?

Yes:
$ tar cvjf archive.tar.bz2 archive/
Will do the trick. :)

ofeeley
19th February 2004, 10:12 PM
It'd be worth mentioning that there are three styles of options with tar.

To take the example of creating a gzipped tar as mentioned above:

1. Old style: single letters with no hyphens preceding them. This style is supported for backward compatibility and is supposed to be deprecated.

tar cvzf archive.tgz archive/

It's important in this style that arguments (such as archive.tgz) are passed on the commandline in the same as the clustered options (cvzf). In this example the "f" option is the only one that takes an argument and there's no confusion.

2. Mnemonic. Eliminates all possible confusion but is much longer to type out:

tar --create --gzip --verbose --file=archive.tgz archive/

3. Short.
tar -c -v -z -f archive.tgz archive/

In this form it's important to make sure that arguments are paired with their options, e.g.

tar -c -f archive.tgz -z -v archive/

Things get slightly more tricky when using just one dash/hyphen as only the last option can take an argument. In this situation it's very important to get the order correct:
e.g.
tar -czvf archive.tgz archive/
will work but
tar -czfv archive.tgz archive/
will not.

The full manual is available using "info tar"

mhelios
20th February 2004, 09:57 AM
Great addendum ofeeley. One point I'd just like to touch on though. These 3 different "styles" of option passing are colloquially known as:


No. 1.) "Old Style" comes from BSD-style options

No. 2) "Mnemonic" comes from GNU-style long options

No. 3.) "Short" comes from Unix98 standard options.



Just a bit of the history behind the differences.

Jongi
31st March 2007, 12:36 AM
I would like to add my experience of using tar to backup and restore. All the below is done as root.

Backup


# tar cvpjf backup.tar.bz2 --exclude=/proc --exclude=/lost+found --exclude=/backup.tar.bz2 --exclude=/mnt --exclude=/tmp --exclude=/sys --exclude=/media /

Note: The above assumes you are running the tar command from /

---------------------------------------------

Restore


# tar xvpjf [path/]backup.tar.bz2 -C /

What I have done from time to time is to backup another distro from another distro eg backup Kubuntu from Fedora. In this case things are a little bit different. Let's assume that I mount the Kubuntu distro under /mnt/kubuntu.

Backup


# tar cvpjf [path/]backup.tar.bz2 --exclude=/mnt/kubuntu/proc --exclude=/mnt/kubuntu/lost+found --exclude=/mnt/kubuntu/mnt --exclude=/mnt/kubuntu/tmp --exclude=/mnt/kubuntu/sys --exclude=/mnt/kubuntu/media [--exclude=/mnt/kubuntu/backup.tar.bz2] /mnt/kubuntu
You will include [--exclude=/mnt/kubuntu/backup.tar.bz2] (without the square brackets) if the path for your tar.bz file is /mnt/kubuntu ie /mnt/kubuntu/backup.tar.bz2.

---------------------------------------------

When you restore, it is important that you mount the device you are restoring to as /mnt/kubuntu

Restore


# tar xvpjf [path/]backup.tar.bz2 -C /

It is important you restore to / as the backup process will have saved the files as /mnt/kubuntu/...

After both processes you will need to recreate



# mkdir [/mnt/kubuntu]/proc
# mkdir [/mnt/kubuntu]/mnt
# mkdir [/mnt/kubuntu]/tmp
# mkdir [/mnt/kubuntu]/sys
# chmod -R 1777 [/mnt/kubuntu]/tmp


The [/mnt/kubuntu] is provided for in case you did the 2nd part. Make sure that your /boot/grub/menu.lst and /etc/fstab still references the correct devices.

JaksLax
4th April 2007, 02:50 PM
Is there a way I could write a script that would run each night and tar certain files I want and then put the tar file into a drive that I have mounted? If so how would I go about writing the script and getting it to run every night?