Backing up in Linux using tar, gzip an rsync

By peter, September 12, 2009

About TAR and GZIP


Linux has combined the following programs:
TAR
GZIP
The “tar” utility archives but doesn’t compress very well,so we use the “gzip” along with “tar” to compress.

Zipping

The command to zip and compress is “tar -vzcf”
The command to zip only is “tar -vcf”
Exercise:
Back up the /etc directory as follows;
“tar -vcf etc.tar /etc”
Have a look at the size.Now back up using GZIP with TAR as follows;
“tar -vzcf etc.tar.gz /etc” (v=verbose; z=gzip; c=create; f=file) notice the difference in size

Extracting
The command to restore a zipped and compressed file is “tar -vzxf zipfile.tar.gz” The command to restore a zipped only file is
“tar -vxf zipfile.tar.gz”
To extract to a different location you would include the “-C” switch followed by the location, as follows:

tar -vzxf home.tar.gz -C /home/extracted/

To extract selected files use the “tar -vzxf zipfile.tar.gz location/filetoextract.txt” command.


Viewing the content of a tar.gz file
You can view the content of a zipped file with the following command (In this example I have piped the output to “more”)

tar -tzvf /backup/etc.tar.zg | more

How to do an incremental backup:
For an incremental backup, tar needs to generate a file which tells tar which files have been backed up and when. To generate this file you need to use the “-g” switch as follows:
First you need to do a full backup which will generate the “snapshot_file” if it doesn”t exist

tar -cz -g /backup/snapshot_file -f /backup/backup_monday.tar.gz /home

Next you need to do the incremental backup, making use of the “snapshot_file” file

tar -cz -g /backup/snapshot_file -f /backup/backup_tuesday.tar.gz /home

Mirroring directories with the “rsync” command:
The rsync command makes a mirror image of the directory you select. You can mirror to a local folder, or a folder on a remote computer.

Using rsync to mirror locally:

rsync -a /home /mirror

if you wanted to mirror the contents of the home directory, including it”s file structure, but not the home directory its self, type the following:

rsync -a /home/. /mirror

When you run the command again, only files that have changed will be updated

Here are some of the switches you can use:
-a Archive mode

-x Does not follow links etc to other locations
-v Verbose mode

-z Compresses during transfer (suited for remote transfer)

–delete Deletes files on the mirror that no longer exist in the original directory

–exclude-from Does not backup files included in the exclude file

Use the “exclude” option as follows:

rsync -a -exclude-from /home/exclude /home/. /mirror

Using rsync to mirror to a remote location:

You will need “rsync” installed on both the local and remote computers
Example 1

rsync -ave ssh root@remotepc:/data/. /databackup/

This example copies the files from a remote computer to the local computer into a folder called “databackup”

Example 2

rsync -ave ssh /databackup/ root@remotepc:/data/

This example restores the data from the “databackup” folder on the local folder to the remote computer

2 Responses to “Backing up in Linux using tar, gzip an rsync”

  1. JOSE says:


    Pillspot.org. Canadian Health&Care.No prescription online pharmacy.Best quality drugs.Special Internet Prices. High quality drugs. Order drugs online

    Buy:Synthroid.Accutane.Arimidex.Valtrex.Human Growth Hormone.Petcam (Metacam) Oral Suspension.Actos.Prednisolone.Lumigan.Retin-A.100% Pure Okinawan Coral Calcium.Nexium.Zyban.Mega Hoodia.Zovirax.Prevacid….

Leave a Reply

OfficeFolders theme by Themocracy