Tar Backup – Watchout

April 11, 2012

Be careful while taking the TAR Backup.While restoring the file to different location from TAR backup, you may end up overwrite the existing file.So be-careful with method you follow to backup as below .============================================================File 1)=========cat file1.txtcontents of file1.txtFile 2)=========cat file2.txtcontents of file2.txtmaldevsrv:/opt/oracle: ls -ltr *.txt-rw-r–r– 1 oracle dba 24 Feb 19 06:45 p.txt-rw-r–r– 1 oracle dba 22 Apr 10 09:44 file1.txt-rw-r–r– 1 oracle dba 22 Apr 10 09:45 file2.txtWe will take the Tar backup in two ways:=========================================1) Without providing the full path of file(s)/dir of which we need to backup. (This backup is Safe, and can be restored which ever directory you need.)cd /opt/oraclemaldevsrv:/opt/oracle: tar -cvf demo1.tar *.txta file1.txt 1Ka file2.txt 1Ka p.txt 1K2) Providing the full path of file(s)/dir of which we need to backup. (This backup is Not Safe, and cannot be restored which ever directory you need, Not safe because while restoring it overwrites to original files/location.)maldevsrv:/opt/oracle: tar -cvf demo2.tar /opt/oracle/*.txta /opt/oracle/file1.txt 1Ka /opt/oracle/file2.txt 1Ka /opt/oracle/p.txt 1KAfter backup I have added more contents to file2.txt as below==============================================================maldevsrv:/opt/oracle: cat file2.txtcontents of file2.txtHello I am new Content===============================================================Now we will try Restoring the above tar’s in /tmp/SR and see what happens.===============================================================1) We will use first tar file (demo1.tar)that was backed up without giving full path, and see if its restoring to required directory (/tmp/SR).maldevsrv:/opt/oracle: cd /tmp/SRmaldevsrv:/tmp/SR: tar -xvf /opt/oracle/demo1.tartar: blocksize = 8x file1.txt, 22 bytes, 1 tape blocksx file2.txt, 22 bytes, 1 tape blocksx p.txt, 24 bytes, 1 tape blocksmaldevsrv:/tmp/SR: pwd/tmp/SRmaldevsrv:/tmp/SR: ls -ltrtotal 48-rw-r–r– 1 oracle dba 24 Feb 19 06:45 p.txt-rw-r–r– 1 oracle dba 22 Apr 10 09:44 file1.txt-rw-r–r– 1 oracle dba 22 Apr 10 09:45 file2.txtmaldevsrv:/tmp/SR: cat file2.txtcontents of file2.txtAs you see file2.txt is restored to required directory /tmp/SR and it worked well as expected.maldevsrv:/tmp/SR: cat /opt/oracle/file2.txt contents of file2.txtHello I am new ContentThe original file file2.txt with latest contents, i.e. while restoring it did not overwrite the original files.2) Now we will use second tar file (demo2.tar ) that was backed up giving full path, and see if its restoring to required directory (/tmp/SR) or Overwriting the original files.Just cleaning up files that were restored from demo1.tar=========================================================maldevsrv:/tmp/SR: pwd/tmp/SRmaldevsrv:/tmp/SR: rm *.txtmaldevsrv:/tmp/SR: ls -ltrtotal 0Now restoring the files from demo2.tar========================================maldevsrv:/tmp/SR: pwd/tmp/SRmaldevsrv:/tmp/SR: tar -xvf /opt/oracle/demo2.tartar: blocksize = 8x /opt/oracle/file1.txt, 22 bytes, 1 tape blocksx /opt/oracle/file2.txt, 22 bytes, 1 tape blocksx /opt/oracle/p.txt, 24 bytes, 1 tape blocksmaldevsrv:/tmp/SR: ls -ltr total 0So it did not restore to expected directory (/tmp/SR), instead it restored to original directory from where the files got backed up.maldevsrv:/tmp/SR: cat /opt/oracle/file2.txt contents of file2.txtThis show’s the latest file (file2.txt) in /opt/oracle, was overwritten with restored file.Conclusion: Always take Tar backup without providing the full path, This will avoid accidentalrestore from overwriting the original files.E.g 1) To Backup Oracle Home==============================export BACKUP_DIR=/u01/orasoft/orabin_bkpmaldevsrv:/opt/oracle: echo $ORACLE_HOME
/u01/app/oracle/product/11.2.0maldevsrv:/opt/oracle: cd $ORACLE_HOME/..