Saturday, 31 January 2015

File backup(tar/gzip)

 Linux is an "almost perfect" operating system, but things do happen and data is sometimes lost. The best way to avoid problems is to backup your files. Linux provides two key programs to do this: 'tar' and 'gzip'

Tar:
This program assembles various files into one package, commonly called a "tarball". Let's say you have some files - notes say:
notes1.txt
notes2.txt
notes3.txt

we placed these files under a directory named linux_notes and We  want to back them up and keep them on a floppy, let's say. You would type the following command to package them in a tarball.

tar -cvf linux_notes.tar notes*.txt

First, We have tar, the name of the program. Then we have the options, c (--create) v (--verbose-show what files they are) (f--file -make a file - should always be the last option) Then we have the name of the file we want to create ( linux_notes.tar) and the files we  want to backup (notes*.txt).

This presupposes that you may have other files in the directory that you don't want to include. If you want to include ALL files in a directory, just substitute notes*.txt for *.*.

tar -cvf linux_notes.tar *

With one asterisk, you will include directories and files without extensions (my_file as opposed to my_file.txt). Be prepared to get a fairly voluminous tarball.

This is the first step in the backup process. Now let's look at the second step; the compression of these files.


Using 'gzip'

As we mentioned, 'tar' just assembles the files together into only one file. There is no reduction in the size of these files (the tarball might even be bigger!) Now we would have to do one more thing in order to reduce this file into a more manageable size: use 'gzip'.

gzip is the preferred compression tool for Linux. To reduce the size of your tar file, you would issue the following command:

gzip linux_notes.tar
and the tar file would be compressed. You can also compress a regular file using the same command, but gzip is used primarily with tarballs.

The result would be a file like this: your_tar_file.tar.gz

The two file extensions show us that the file is a tarball and it is compressed with the 'gzip' format.

We can reduce these two steps into one means we can  create the .tar.gz file at the same time when we make the tarball.
If you add z to the options, and change the name of the file to create to a .gz extension, you have the whole shebang in one step.

tar  -czvf linux_notes.tat.gz  notes.*txt

Remember f should always be the last option.

Untar:

tar -zxvpf  linux_notes.tar.gz

I've used my preferred options. I'll explain them:

-z - unzip the file first
-x - extract the files from the tarball
-v - \"verbose\" (i.e tar tells you what files it's extracting)
-p - preserves dates, permissions of the original files
-f - use the file in question (if you don't specify this, tar just sort of sits around doing nothing)

The files are extracted and your original tarball is preserved (my_tar_file.tar.gz).

We can ommit the option z
tar -xvpf linux_notes.tar.gz


To unzip the .gz file
gzip -d linux_notes.tar.gz

This will result a tarball  named linux_notes.tar

Now  we can extract the files from the tarball
tar -xvpf linux_notes.tar


Folder Zip:

Sometimes we need to zip the  files of directory and subdirectory within the directory and files & subdirectory of subdirectory and so on. So to Zip use:

zip -r filename.zip  directory_to_zip_recursively

and to unzip

unzip filename.zip








No comments:

Post a Comment