Google Plus

How to create backup with tarball -tar- command

Written by Mel Kham on . Posted in Linux tutorials

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

For questions please refer to our Q/A forum at : http://ask.unixmen.com

Mel Kham

Founder of Unixmen, Living in Amsterdam. Am working in my free time to help people to understand the Opensource and to explain them in easy way how to make the fist steps to the the light. Working day and night with my Co-founder Zinovsky to keep this website live even with less resources.
  • http://voku-online.de voku

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

    # ein Backup einer Datei anlegen (mit Datum)

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

    # Ein komprimiertes Backup + Datum erstellen

    packen()
    {
    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
    http://www.unixmen.com/linux-tutorials/287-backup-with-tar-command

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

Like us on Facebook

This week Top Posts

Write for us

Recent Comments

Nova

|

I wonder if there is a way to create your own themes.

Red Adaya

|

Thank you! This worked for me!!!

SK

|

Andrew look into your httpd.conf file at line no 350. There might be a syntax error.

andrew

|

hello I have this error

[root@cloud html]# /etc/init.d/httpd restart

then this appears

Starting httpd: httpd: Syntax error on line 350 of /etc/httpd/conf/httpd.conf: without matching section.

plz tell how to fix this.

 
IDG Tech Network
Copyright © 2008-2013 Unixmen.com .
Maintained by Anblik .