Search Windows and Linux Networking

Friday, December 20, 2013

Step by Step : How to backup data using tar



Backup your data using tar

To backup or restore your data using tar always start backup/ restore from root (/) directory. To go to root (/) type the command

cd /

if you don't start backup from root (/) directory  then it will not restore data its original location. because when creating archive with tar. tar alway remove leading "/" from the member and that may be problem when restoring data at original location.

See the following example. I have started backup from home directory not from root (/) directory.

root@sandeepk:~# tar -cvpf /backup/exampleone.tar /home/sandeep/test
tar: Removing leading `/' from member names
/home/sandeep/test/
/home/sandeep/test/test3
/home/sandeep/test/test7
/home/sandeep/test/test2
/home/sandeep/test/test8
/home/sandeep/test/testmd/
/home/sandeep/test/testmd/test3
/home/sandeep/test/testmd/test7
/home/sandeep/test/testmd/test2
/home/sandeep/test/testmd/test8
/home/sandeep/test/testmd/test5
/home/sandeep/test/testmd/test6
/home/sandeep/test/testmd/test4
/home/sandeep/test/testmd/test9
/home/sandeep/test/testmd/test1
/home/sandeep/test/test5
/home/sandeep/test/test6
/home/sandeep/test/test4
/home/sandeep/test/test9
/home/sandeep/test/test1


Now test the data using following command

 root@sandeepk:~# tar -dvf /backup/exampleone.tar
home/sandeep/test/
tar: home/sandeep/test: Warning: Cannot stat: No such file or directory
home/sandeep/test/test3
tar: home/sandeep/test/test3: Warning: Cannot stat: No such file or directory


It will be give warning massage that No such file or directory. tar is checking data home/sandeep/test/ and not from /home/saneep/test

To resole the issue you should always start your backup from root directory (/)
 and now i will take another backup same data but from root directory

root@sandeepk:~# cd /
root@sandeepk:/#

root@sandeepk:/# tar -cvpf /backup/exampletwo.tar home/sandeep/test
home/sandeep/test/
home/sandeep/test/test3
home/sandeep/test/test7
home/sandeep/test/test2
home/sandeep/test/test8
home/sandeep/test/testmd/
home/sandeep/test/testmd/test3
home/sandeep/test/testmd/test7
home/sandeep/test/testmd/test2
home/sandeep/test/testmd/test8
home/sandeep/test/testmd/test5
home/sandeep/test/testmd/test6
home/sandeep/test/testmd/test4
home/sandeep/test/testmd/test9
home/sandeep/test/testmd/test1
home/sandeep/test/test5
home/sandeep/test/test6
home/sandeep/test/test4
home/sandeep/test/test9
home/sandeep/test/test1


Now verify data using following command

root@sandeepk:/# tar -dvf /backup/exampletwo.tar
home/sandeep/test/
home/sandeep/test/test3
home/sandeep/test/test7
home/sandeep/test/test2
home/sandeep/test/test8
home/sandeep/test/testmd/
home/sandeep/test/testmd/test3
home/sandeep/test/testmd/test7
home/sandeep/test/testmd/test2
home/sandeep/test/testmd/test8
home/sandeep/test/testmd/test5
home/sandeep/test/testmd/test6
home/sandeep/test/testmd/test4
home/sandeep/test/testmd/test9
home/sandeep/test/testmd/test1
home/sandeep/test/test5
home/sandeep/test/test6
home/sandeep/test/test4
home/sandeep/test/test9
home/sandeep/test/test1



Now delete the data from your system and try to restore it

root@sandeepk:/# rm -rf /home/sandeep/test
root@sandeepk:/# ls -l /home/sandeep/test
ls: cannot access /home/sandeep/test: No such file or directory
root@sandeepk:/# cd
root@sandeepk:~# tar -xvpf /backup/exampleone.tar
home/sandeep/test/
home/sandeep/test/test3
home/sandeep/test/test7
home/sandeep/test/test2
home/sandeep/test/test8
home/sandeep/test/testmd/
home/sandeep/test/testmd/test3
home/sandeep/test/testmd/test7
home/sandeep/test/testmd/test2
home/sandeep/test/testmd/test8
home/sandeep/test/testmd/test5
home/sandeep/test/testmd/test6
home/sandeep/test/testmd/test4
home/sandeep/test/testmd/test9
home/sandeep/test/testmd/test1
home/sandeep/test/test5
home/sandeep/test/test6
home/sandeep/test/test4
home/sandeep/test/test9
home/sandeep/test/test1


Now check if your data restored its original location or not  by using ls command.


root@sandeepk:~# ls -l /home/sandeep/test
ls: cannot access /home/sandeep/test: No such file or directory
root@sandeepk:~#


It not restore at it's original location if you run ls command you will see new home directory created and restore all data in that directory.

root@sandeepk:~# ls -l
total 12824
-rw-r--r-- 1 root root 13127680 Dec 18 15:07 archive
drwxr-xr-x 3 root root     4096 Dec 20 12:19 home



now extract from root (/) directory.


root@sandeepk:~# cd /
root@sandeepk:/# tar -xvpf /backup/exampletwo.tar
home/sandeep/test/
home/sandeep/test/test3
home/sandeep/test/test7
home/sandeep/test/test2
home/sandeep/test/test8
home/sandeep/test/testmd/
home/sandeep/test/testmd/test3
home/sandeep/test/testmd/test7
home/sandeep/test/testmd/test2
home/sandeep/test/testmd/test8
home/sandeep/test/testmd/test5
home/sandeep/test/testmd/test6
home/sandeep/test/testmd/test4
home/sandeep/test/testmd/test9
home/sandeep/test/testmd/test1
home/sandeep/test/test5
home/sandeep/test/test6
home/sandeep/test/test4
home/sandeep/test/test9
home/sandeep/test/test1

root@sandeepk:/#


root@sandeepk:/# ls -l /home/sandeep/test
total 4
-rw-r--r-- 1 root root    0 Dec 20 10:29 test1
-rw-r--r-- 1 root root    0 Dec 20 10:29 test2
-rw-r--r-- 1 root root    0 Dec 20 10:29 test3
-rw-r--r-- 1 root root    0 Dec 20 10:29 test4
-rw-r--r-- 1 root root    0 Dec 20 10:29 test5
-rw-r--r-- 1 root root    0 Dec 20 10:29 test6
-rw-r--r-- 1 root root    0 Dec 20 10:29 test7
-rw-r--r-- 1 root root    0 Dec 20 10:29 test8
-rw-r--r-- 1 root root    0 Dec 20 10:29 test9
drwxr-xr-x 2 root root 4096 Dec 20 10:30 testmd







No comments:

Post a Comment