• VMware

    Learn about VMware virtualization for its products like vsphere ESX and ESXi, vCenter Server, VMware View, VMware P2V and many more

  • Linux

    Step by step configuration tutorials for many of the Linux services like DNS, DHCP, FTP, Samba4 etc including many tips and tricks in Red Hat Linux.

  • Database

    Learn installation and configuration of databases like Oracle, My SQL, Postgresql, etc including many other related tutorials in Linux.

  • Life always offers you a second chance ... Its called tomorrow !!!

    Sunday, December 29, 2013

    How to preserve Symbolic links with tar command in Unix/Linux

    Well there are times when you have to take backup of your Linux machine and copy it some other location but at the same time you don't want to loose all the symlinks which are their in your machine. If you do a normal compression/decompression, then you will loose all your symbolic links which is not a good idea for a production environment specially.

    If you go through the man page of tar
    # man tar
     -h, --dereference
     follow symlinks; archive and dump the files they point to

    So, as of now we need to use -h argument along with tar command but how and when?

    Suppose I want to copy my home directory to some other location and my home directory consists of few symlinks
    # cd /home/test
    # ls -l
    -rw-r--r--. 1 root root 0 Sep 28 12:45 1
    lrwxrwxrwx. 1 root root 1 Sep 28 12:46
    10 -> 5
    -rw-r--r--. 1 root root 0 Sep 28 12:45 2
    -rw-r--r--. 1 root root 0 Sep 28 12:45 3
    -rw-r--r--. 1 root root 0 Sep 28 12:45 4
    -rw-r--r--. 1 root root 0 Sep 28 12:45 5
    lrwxrwxrwx. 1 root root 1 Sep 28 12:46 6 -> 1
    lrwxrwxrwx. 1 root root 1 Sep 28 12:46 7 -> 2
    lrwxrwxrwx. 1 root root 1 Sep 28 12:46 8 -> 3
    lrwxrwxrwx. 1 root root 1 Sep 28 12:46 9 -> 4

    As you can see there are many symlinks which exists in the test home directory. Now let us compress and copy to some other location

    NOTE: You need to compress the directory without "-h" argument as you can see below
    [root@test home]# tar -czvf test.tar.gz test/
    test/
    test/2
    test/4
    test/.gnome2/
    test/1
    test/.bash_history
    test/8
    test/.emacs
    test/5
    test/.bash_logout
    test/9
    test/10
    test/.bashrc
    test/.bash_profile
    test/.mozilla/
    test/.mozilla/extensions/
    test/.mozilla/plugins/
    test/6
    test/7
    test/3

    Move the zipped file to another location
    [root@test home]# mv test.tar.gz /tmp/
    [root@test home]# cd /tmp/

    While extracting the directory make sure you use "-h" argument to save the symlinks as they were while compressing
    [root@test tmp]# tar -xhzvf test.tar.gz
    test/
    test/2
    test/4
    test/.gnome2/
    test/1
    test/.bash_history
    test/8
    test/.emacs
    test/5
    test/.bash_logout
    test/9
    test/10
    test/.bashrc
    test/.bash_profile
    test/.mozilla/
    test/.mozilla/extensions/
    test/.mozilla/plugins/
    test/6
    test/7
    test/3
    [root@test tmp]# cd test

    Now let us verify if it worked
    [root@test test]# ls -l
    total 0
    -rw-r--r--. 1 root root 0 Sep 28 12:45 1
    lrwxrwxrwx. 1 root root 1 Sep 28 12:49
    10 -> 5
    -rw-r--r--. 1 root root 0 Sep 28 12:45 2
    -rw-r--r--. 1 root root 0 Sep 28 12:45 3
    -rw-r--r--. 1 root root 0 Sep 28 12:45 4
    -rw-r--r--. 1 root root 0 Sep 28 12:45 5
    lrwxrwxrwx. 1 root root 1 Sep 28 12:49 6 -> 1
    lrwxrwxrwx. 1 root root 1 Sep 28 12:49 7 -> 2
    lrwxrwxrwx. 1 root root 1 Sep 28 12:49 8 -> 3
    lrwxrwxrwx. 1 root root 1 Sep 28 12:49 9 -> 4

    So. the magic did happened. Let me know your success and failures

    Follow the below links for more tutorials

    Configure Red Hat Cluster using VMware, Quorum Disk, GFS2, Openfiler
    Tutorial for Monitoring Tools SAR and KSAR with examples in Linux
    How to configure Samba 4 Secondary Domain Controller
    How to secure Apache web server in Linux using password (.htaccess)
    How to register Red Hat Linux with RHN (Red Hat Network )
    Red hat Enterprise Linux 5.5 Installation Guide (Screenshots)
    15 tips to enhance security of your Linux machine
    Why is Linux more secure than windows and any other OS
    What is the difference between "su" and "su -" in Linux?
    What is kernel-PAE in Linux?
    What is swappiness and how do we change its value?
    How to log iptables messages in different log file
    What are the s and k scripts in the etc rcx.d directories
    How to check all the currently running services in Linux
    How to auto start service after reboot in Linux
    What is virtual memory, paging and swap space?

    5 comments:

    1. I don't believe the -h has any impact on the tar -x.....
      It is applicable when creating the tar and basically replaces the sym link with the actual file the link was pointing to.

      ReplyDelete
      Replies
      1. Hello,

        Thanks for marking the mistake. That has been corrected

        Thanks
        Deepak

        Delete
    2. This works only with root or privileged user but not as normal user, how to make tar to do same as normal user

      ReplyDelete
      Replies
      1. I verified the same as normal user and I don't see any difference in the output.

        Please make sure that you use "h" argument while uncompressing the tar.

        Delete