tar command is known for backup, and most commonly to tapes. It is also known to the open-source community as a way to package files and distribute as one single file for easier transfer method. In this small section we will try to show you the most out of tar command. We will not be covering the details usage or options available in tar command as the information is already available in the “man tar” output.
Lets begin with the most basic usage of it, backing up files into tapes.
User# tar cvf /dev/rmt/1 /data
In above example, the tar was used with option “cvf” to backup everything in “/data” directories into the tape device “/dev/rmt/1”. In the simple words the option “cvf” can be describe as “create” backup, “verbose” the output and “file” it into device (in this case “/dev/rmt/1”).
Now that you have backed-up the “/data”, lets view the contents of the tape.
User# tar tvf /dev/rmt/1
This time we use the option “tvf”, option “t” is to list the contents of the archive. Option “v” for verbose and “f” is used with output device name, in this case “/dev/rmt/1”.
It is a good practise to do a restore test of your backup tape. This can be done into another machine or to the same machine. Lets see the example below.
User# tar xvf /dev/rmt/1
User# tar xvf /dev/rmt/1 /data/file1
Option “x” is for extract or restore. We can just do as above to restore the whole tape or we can specify files to be restored at the end of the command lines. In order to restore specific files, we need to give the exact path and filename which we can get by using the option “tvf” to view it first.
The way we specify the arguments of the files to be backed-up in backup command will affects the way its going to be restored later on. In the previous example we specify “/data” in the backup command line, Nothing wrong with this example, except that when we restore the backup, it will only goes to the “/data” which is absolute path and if its on the live machine, this will need a serious consideration, just in case you might override some important files. The work around is you can just restore it into other machine first and then transfer it into your live machine, or you can fix this situation from the beginning.
When you backup, you can just change directory to the /data and run the backup command like below
User# cd /data User# tar cvf /dev/rmt/1 .
You can see that we just specify “.” (dot) instead of “/data” as the argument. The dot means everything in this directory including subdirectories (if any). This way, we can restore the backup into any directory, this is due to the fact that the restore point of each files inside the backup starts with “./” meaning “current directory” doesn’t matter where your current directory is. This is a contrast of the earlier example where you have no choice except to restore it into “/data”.
Expand the usage of tar
So far we have used tar as a tool to backup into devices like tape etc. beside that, we can specify a normal filename as an output. So the backup will be in file format instead of tapes. This is useful when we plan to share some files or small application that we created, with friends of just put it on the website for viewers downloads.
Tar has been widely used in the open-source community. Although it originate in unix environment, thats doesn’t mean windows users cannot use it, there are windows version of tar in the net, and the application Winzip (www.winzip.com) can actually open and extract the tar files.
One very good advantage of tar is that it keeps the original ownership and permission of the files that it backed-up. This will helps a lot when you really concern on the ownership and permission of the tar contents especially when that tar file is for distribution.
Consider this situation. You have to transfer one folder belongs to the important application into another filesystem as an online backup. Lets say the application folder is “/apps” and you need to make a duplicate into a “/backup” filesystem, purpose may be to act as an online backup for fast recovery option. You can use the “cp” command but “cp” command will change the ownership and permission of the end results, and this will be disaster as some application is very particular in ownership and permission of its files. In this case tar will be the best option to use.
User# mkdir /backup/apps User# cd /apps User# tar cvf - . | (cd /backup/apps; tar xvf -)
In the above example, first we create the apps folder in the /backup, then we change directory to /apps, where the application resides. The last one is the most interesting part. There are actually 3 portions combined into one long command line.
- backup the file and transfer the output as an input for the next statements after the pipe
- change directory to /backup/apps
- restore the files received from step 1 into current directory
steps 2 and 3 was combined into one command by using the round brackets, the reason is we want to execute the “cd /backup/apps” without losing the output that has been transferred by the first command in the left. If the didn’t combined the step 2 and 3, the output from step 1 will be useless to the “cd /backup/apps” command and the step 3 will be executed without receiving anything because now step 3 is a separate command due to the semi-colon (;) action, and the whole thing will be useless.