How to create backup with tarball -tar- command

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:

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

  • voku

    man könnte sich auch z.B. folgende Funktionen in der .bashrc eintragen :)

    # ein Backup einer Datei anlegen (mit Datum)

    for FILE ; do
    [[ -e “$1” ]] && cp “$1” “${1}_$(date +%Y-%m-%d_%H-%M-%S)” || echo “\”$1\” not found.” >&2
    alias fback=file_backup

    # Ein komprimiertes Backup + Datum erstellen

    tar czvf “$1-$(date +%y%m%d-%H%M%S).tar.gz” “$1” ;

  • jeff

    the ad for IDrive covers up the first dozen lines or so for the page

    frustrating; can’t get rid of it …

  • Zinovsky

    Sorry to hear this, i will try to work on this the weekend to change the ad position, but now if you are using firefox or google chrome use the ad bloker extension :)

  • Lucia

    Thank you!

  • Iliya popov

    Sorry for maybe fool question, but how to designate the drive name on which we will make our tarball ( external USB – “toshiba ext”)?