{"id":4945,"date":"2017-04-19T13:14:26","date_gmt":"2017-04-19T17:14:26","guid":{"rendered":"http:\/\/www.linux-databook.info\/?page_id=4945"},"modified":"2017-04-19T13:14:26","modified_gmt":"2017-04-19T17:14:26","slug":"using-tar-and-ssh-for-backups","status":"publish","type":"page","link":"http:\/\/www.linux-databook.info\/?page_id=4945","title":{"rendered":"Using tar and ssh for backups"},"content":{"rendered":"<p style=\"text-align: left;\" align=\"center\">Couples complement each other and each member of the couple contributes unique and irreplaceable parts to the whole. But some couples are very odd. Such is the case with this\u00a0couple of commands \u2013 the <b>tar<\/b> and <b>ssh<\/b> commands.<\/p>\n<p>Wait \u2013 what?!<\/p>\n<p>Yup, that&#8217;s right, the tar and ssh commands work together in some very interesting ways \u2013 especially when used with full consideration of the capabilities of Standard I\/O (STDIO), which is also known as <a href=\"https:\/\/en.wikipedia.org\/wiki\/Standard_streams\" target=\"_blank\">standard streams<\/a>.<\/p>\n<h1 class=\"western\">ssh<\/h1>\n<p>Most everyone knows that the ssh command is a secure and sophisticated form of terminal emulator allows one to login to a remote computer to access a shell session and run commands. So I could login to a remote computer and run the <b>ls<\/b> command on the remote computer. The results are displayed in the ssh terminal emulator window on my local host. The Standard Output (STDOUT) of the command is displayed on my terminal window but it remains on the remote host and cannot be used by the local host.<\/p>\n<p>That is trivial and everyone does that. But the next step is a bit more interesting. Rather than maintain a terminal session on the remote computer and issuing multiple commands, I can simply use a command like the following to run a single command on the remote computer with the results being displayed on the local host. This assumes that <a href=\"https:\/\/www.howtoforge.com\/linux-basics-how-to-install-ssh-keys-on-the-shell\" target=\"_parent\">SSH public\/<\/a><a href=\"https:\/\/www.howtoforge.com\/linux-basics-how-to-install-ssh-keys-on-the-shell\" target=\"_parent\">private<\/a><a href=\"https:\/\/www.howtoforge.com\/linux-basics-how-to-install-ssh-keys-on-the-shell\" target=\"_parent\"> keypairs<\/a> (PPKP) are in use and I do not have to enter a password each time I issue a command to the remote host.<\/p>\n<pre class=\"western\" style=\"padding-left: 30px;\"><strong>ssh remotehost ls<\/strong><\/pre>\n<p>So now I can use the results of that command on my local host because the standard output data stream is sent through the SSH tunnel to the local host. OK, that is good, but what does it mean?<\/p>\n<p>Let&#8217;s look at the tar command before we answer that question.<\/p>\n<h1 class=\"western\">tar<\/h1>\n<p>The tar command is used to make backups. The name \u201ctar\u201d stands for Tape ARchive, but it can be used with any type of recording media such as tape, hard drives, thumb drives and more. A command like the following can be used to create a backup of a home directory on the local host.<\/p>\n<pre class=\"western\" style=\"padding-left: 30px;\"><strong>tar -cvf \/tmp\/home.tar \/home<\/strong><\/pre>\n<p>This command created a tar file \u2013 also called a tarball \u2013 named home.tar in the \/tmp directory. That file is a backup of everything in the home directory. Well, that&#8217;s nice, but also not very interesting because it is very common. What can be very interesting about this is that, although many people do not realize it, if the target output file is not specified using the -f option, the output of the tar command is sent directly to STDOUT as in the sample below; try it.<\/p>\n<pre class=\"western\" style=\"padding-left: 30px;\"><strong>tar -cv \/home<\/strong><\/pre>\n<p>That means that the complete output of the tar command \u2013 the files being backed up \u2013 is sent to the terminal, which opens up some very interesting possibilities such as redirecting the STDOUT data stream to a backup file. That looks like the following command.<\/p>\n<pre class=\"western\" style=\"padding-left: 30px;\"><strong>tar -cv \/home &gt; \/tmp\/home.tar<\/strong><\/pre>\n<p>This command performs the same function as the first tar command in this section, just in a somewhat different and more interesting manner.<\/p>\n<h1 class=\"western\">The Odd Couple<\/h1>\n<p>We can use a command similar to the following to back up the home directory of the remote host to the \/tmp directory of that remote host.<\/p>\n<pre class=\"western\" style=\"padding-left: 30px;\"><strong>ssh remotehost \u201ctar -cvf \/tmp\/home.tar \/home\u201d<\/strong><\/pre>\n<p>Note that the command to be executed on the remote host is enclosed in quotes to ensure that the correct command is executed remotely; this is a bit of clarification for both the shell and for us humans. A slight change to this command gives us one in which we simply redirect the output of the tar command to the \/tmp directory on the remote host.<\/p>\n<pre class=\"western\" style=\"padding-left: 30px;\"><strong>ssh remotehost \u201ctar -cv \/home &gt; \/tmp\/home.tar\u201d<\/strong><\/pre>\n<p>This command produces exactly the same result as the previous one. In this case the STDOUT data stream of the tar command is maintained entirely on the remote host and is redirected to the backup file.<\/p>\n<p>However the next command is the one that opens up many new possibilities. Can you see what it does?<\/p>\n<pre class=\"western\" style=\"padding-left: 30px;\"><strong>ssh remotehost \u201ctar -cv \/home\u201d &gt; \/tmp\/home.tar<\/strong><\/pre>\n<p>In this case the STDOUT data stream from the tar command is sent through the SSH connection to the local host. This stream of data is then redirected to the backup file \/tmp\/home.tar on the local host. Simply by moving the trailing quote to the left, the command is changed so that we now have a command that can do backups of remote hosts to a local host.<\/p>\n<p>I used <strong>tar<\/strong> and <strong>ssh<\/strong>\u00a0every day to perform backups. I used\u00a0a script that uses ssh and tar, along with SSH public key encryption, to perform backups of several remote hosts to an external USB hard drive on a local host. These two commands make very simple a necessary task and the best part is that they are Free Open Source Software; free as in beer as well as free as in speech.<\/p>\n<p>I now use <strong>rsync<\/strong> for backups, but that is another article.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Couples complement each other and each member of the couple contributes unique and irreplaceable parts to the whole. But some couples are very odd. Such is the case with this\u00a0couple of commands \u2013 the tar and ssh commands. Wait \u2013&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/www.linux-databook.info\/?page_id=4945\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":1803,"menu_order":7,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4945","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/pages\/4945","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4945"}],"version-history":[{"count":3,"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/pages\/4945\/revisions"}],"predecessor-version":[{"id":4948,"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/pages\/4945\/revisions\/4948"}],"up":[{"embeddable":true,"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=\/wp\/v2\/pages\/1803"}],"wp:attachment":[{"href":"http:\/\/www.linux-databook.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}