Fedora Linux Support Community & Resources Center
Sections ›› Home | Forums | Guidelines | Forum Help | Fedora FAQ | Fedora News 

Go Back   FedoraForum.org > Fedora Support > Guides & Solutions (No Questions)

Guides & Solutions (No Questions) Post your guides here. You can also add your comments to a guide, but don't start a thread to ask a question. Use another forum for that.

Closed Thread
 
Thread Tools Search this Thread Display Modes
  #1  
Old 2004-02-18, 05:50 PM CST
ewdi's Avatar
ewdi Offline
Retired Admin
 
Join Date: Jan 2004
Location: Penguin Land
Age: 59
Posts: 1,939
How-to : Using Tar (Taring)

By SuperHornet from 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

Code:
 
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

Code:
 
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)

Code:
 
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.

Code:
 
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.

Code:
 
 
/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.

Code:
 
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.

Code:
 
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.

Code:
 
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.

Code:
 
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

Code:
 
tar -zxvf filename.tar.gz
-x extract

If it is not compressed then

Code:
 
tar -xvf filename.tar
Code:
 
#!/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");
  #2  
Old 2004-02-18, 10:11 PM CST
Bana's Avatar
Bana Offline
Retired Community Manager
 
Join Date: Feb 2004
Location: Austin, Texas
Age: 22
Posts: 580
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 ?
__________________
http://coolhands.blogspot.com/
binarybana AT gmail.com
  #3  
Old 2004-02-19, 01:19 AM CST
mhelios Offline
Retired Community Manager
 
Join Date: Feb 2004
Location: Queensland, AU
Posts: 736
Quote:
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.
  #4  
Old 2004-02-19, 02:12 PM CST
ofeeley Offline
Registered User
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 16
Three styles of options

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"
  #5  
Old 2004-02-20, 01:57 AM CST
mhelios Offline
Retired Community Manager
 
Join Date: Feb 2004
Location: Queensland, AU
Posts: 736
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.
__________________
mhelios@fedoraforum.org
Registered Linux User # 348963
GnuPG KeyID: 0xCE9F8922
  #6  
Old 2007-03-30, 05:36 PM CDT
Jongi Online
Registered User
 
Join Date: Oct 2005
Posts: 1,913
I would like to add my experience of using tar to backup and restore. All the below is done as root.

Backup
Code:
# 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
Code:
# 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
Code:
# 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
Code:
# 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

Code:
# 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.
__________________
Desktop (64-bit) - F13 rawhide, Debian Sid, OpenSUSE 11.2, ArchLinux

Last edited by Jongi; 2007-03-30 at 05:53 PM CDT.
  #7  
Old 2007-04-04, 07:50 AM CDT
JaksLax Offline
Registered User
 
Join Date: Oct 2006
Location: Rochester, NY
Age: 27
Posts: 5
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?
Closed Thread

Tags
howto, tar, taring

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
TARing Recursion and excluding Question Quella Software 9 2005-10-22 07:43 AM CDT

Automatic Translations (Powered by Powered by Google):
Afrikaans Albanian Arabic Belarusian Bulgarian Catalan Chinese Croatian Czech Danish Dutch English Estonian Filipino Finnish French Galician German Greek Hebrew Hindi Hungarian Icelandic Indonesian Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Taiwanese Thai Turkish Ukrainian Vietnamese Yiddish

All times are GMT -7. The time now is 03:23 AM CST.

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
Hosting provided by ThePlanet



All trademarks, and forum posts in this site are property of their respective owner(s).

FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact | Founding Members
Designed By Ewdison Then | Powered by vBulletin ©2000-2009, Jelsoft Enterprises Ltd.
FedoraForum is Powered by Open Source Projects and Products
Translated to other languages thanks to NLP-er