Walker News

Linux: How To Backup And Restore Directory With Tar Over SSH Connection?

As per internal disaster recovery code, we have to redo DR server setup exercise every year, as to get sysadmins familiar with processes that are not frequently done and take chance to confirm latest updates deployed to live server are also working well in DR server.

So, it’s unavoidable to backup some directories on live machine and restore them to DR box. Problem is that the live system has insufficient free disk space to create tarball (tar archive or .tar file), as some of the directories are bulky.
In this situation, flexibility of Linux system comes into picture. Instead of creating the tarball at live system, we opt to backup and restore over network!

In brief, it’s a method called tar over SSH, where we are going to create tarball of directories on live server, pipe it over SSH connection (secure shell connection), and untar the tarball on remote DR server at one go (i.e. enter a series of Linux commands on one line and press ENTER key to execute them).

How to backup and restore Linux file system with Tar command over SSH connection?

This is the commands template:
tar -zcvpf - dir1 dir2 | ssh root@walkernews.net "cd /usr/local/share/lib; tar -zxvpf -"

where
  • tar option switch
    • -z and -c meant to create (-c) a compressed tarball using gzip (-z)
    • -v meant to verbosely list files processed
    • -p meant to preserve file permission information
    • -f meant the tarball output location. In this example, the tarball is created to – (i.e. stdin or standard input).
       
  • dir1 and dir2 are the target directories to be backup
     
  • | is the pipeline, meant the Linux inter-process communication (IPC).
     
  • the SSH remote shell command execution group the series of commands in double-quotation mark, in which it login to remote DR server (e.g. walkernews.net) using root user account, changes working directory to /usr/local/share/lib follow by untar the tarball data stream from stdin input of first tar command (the tarball creation that initiated on live server).

Now, what about if you want to tar backup directories of local system onto remote DR server? I.e. to create tarball of local directories onto remote server over SSH connection (use either one of these two commands; where backup.tgz is a gzip-compressed tarball file name):
tar -zcvpf - dir1 dir2 | ssh root@walkernews.net "cd /backupdir; cat > backup.tgz"

or
tar -zcvpf - dir1 dir2 | ssh root@walkernews.net "cd /backupdir; dd of=backup.tgz"


Custom Search

  1. How To Force Linux To Fix File Transfer Rate? 27-12-09@22:26

    […] cf – . | valve –rate 256K | ssh user@domain.com "(cd /tmp; tar xf -)" The typical tar over ssh is as this without piping to valve program (i.e. dump the tar file content to standard output that […]

2017  •  Privacy Policy