How to create backup with tarball -tar- command

by

Please shareShare on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on RedditDigg thisShare on StumbleUponShare on VKBuffer this page

TAR is usually used to create archive of entires directory and after the tar process we usually compress it in a GZIP file, tar is considered as the easiest and simplest way to make a backup for files and folders in Linux.




1-How to create tar backup :

- Create a tar in zip format with the file named as todays date/time, as root (or use sudo before the command for ubuntu and debian):

tar czvf my_backup.$(date +%Y%m%d-%H%M%S).tgz /home/pirat9/Download

as you see i used czvf ,this mean :

“c” stands for create, “z” make tar archive in a GZIP compressed file,“v” verbose option , “f“filename (the name of our tarball)

- You can also create the backup using tar with a script like :

A simple shell script for the above:

#!/bin/bash
tar czvf $1.$(date +%Y%m%d-%H%M%S).tgz $1
exit $?

Save the script as ‘back’, use chmod to make it executable and then run it passing a single file or foldername:

chmod 700 backup

Now to create backup for the folder download type :

./back   Download

check if the backup has been created successefely.

[pirat9@localhost ~]$ ll
total 271212
drwxr-xr-x. 2 pirat9 pirat9 4096 2009-06-18 08:53 Desktop
drwxr-xr-x. 2 pirat9 pirat9 4096 2009-06-17 10:27 Documents
drwxr-xr-x. 23 pirat9 pirat9 12288 2009-06-18 08:57 Download
drwxrwxr-x. 19 pirat9 pirat9 12288 2009-06-12 08:28 hplip-3.9.4b
-rwxrwxr-x. 1 pirat9 pirat9 15721700 2009-06-12 07:19 hplip-3.9.4b.run
drwxr-xr-x. 2 pirat9 pirat9 4096 2009-06-11 01:23 Music
-rw-rw-r--. 1 pirat9 pirat9 261935395 2009-06-22 08:30 my_backup.20090622-083025.tgz
drwxr-xr-x. 2 pirat9 pirat9 4096 2009-06-11 01:23 Pictures
drwxr-xr-x. 2 pirat9 pirat9 4096 2009-06-11 01:23 Public
drwxr-xr-x. 2 pirat9 pirat9 4096 2009-06-11 01:23 Templates
drwxr-xr-x. 2 pirat9 pirat9 4096 2009-06-11 01:23 Videos

Important: Always keep your backup in another drive or in usb

2. How To Restore A Backup TAR file

To restore your backup, use the command :

  tar-zxvf   my_backup.20090622-083025.tgz

As you see above i used instead of c the “x” option means stands for extract.


(1) we will see later how to create a full system backup using the tarball command