Remote backup using ssh, tar and cron
For the purpose of this tutorial, the local machine will be called “localmachine” (running slackware) and the remote server will be called “remoteserver” (slackware as well). The user will be joe (me). You will have to substitute those 3 with your own machines names and user.
Generating your private/public key pair
To be able to logon to another server without being prompted for your password, you need to generate a key that will be trusted by the remote server, where your backups will be sent to. To accomplish this, follow the following steps as the user you will use (joe here).- ssh-keygen -t rsa
- Generating public/private rsa key pair.
Enter file in which to save the key (/home/joe/.ssh/id_rsa):
- Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/joe/.ssh/id_rsa.
Your public key has been saved in /home/joe/.ssh/id_rsa.pub.
The key fingerprint is:
a6:84:5d:a6:cd:ff:31:48:21:85:ca:46:93:88:7a:50 joe@localmachine
The id_rsa.pub is your public key, which you share with the remote host. The id_rsa is your private key, and this is only for you. Do not lose it or share it with anyone, as this is your passkey! Make sure the file is not readable by anyone but you (chmod 600 ~/.ssh/id_rsa). Anyone having a copy of this key could usurpate your identity and login to this server as you. It is not any more dangerous to use this method as to use a traditional password, but I will not enter into a debate here.
Now that you have your keyring, it is time to send your public key to the remote machine, so that it can trust you.
Sharing your localmachine public key
First things first, let's make sure that the remote folder into which you will put this key exists (~/.ssh), and will only be readable by you.- ssh remotemachine “mkdir .ssh; chmod 600 .ssh”
Next step is to actually copy your public key in the remote directory, like this:
- scp ~/.ssh/id_rsa.pub remotemachine:.ssh/authorized_keys2
- joe@localmachine:~$ scp ~/.ssh/id_rsa.pub remotemachine:.ssh/authorized_keys2
joe@remotemachine's password:
id_rsa.pub 100% 225 0.0KB/s 00:00
joe@localmachine:~$
Creating the job to be run by cron
To make it easy, we will create the backup tarball first, then ssh it over to the remote host. Beforehand, let's make sure you have a remote directory where you will put the tarball, accessible to the user running the script. In this case, we will use 'backup' directory under joe's homedir, that you can create like this:- ssh remotemachine “mkdir backup”
- #!/bin/bash
# this tars up joe's home directory into myhome.tar.gz tarball.
/bin/tar -zcpf /home/joe/myhome.tar.gz /home/joe
# This sends the tarball to the remote directory
cat /home/joe/myhome.tar.gz |ssh remotemachine "cd backup; cat > backup.tar.gz"
Cron
All that is left is to create a cron job for this user.
- crontab -e
- 0 2 * * * /home/joe/home_backup.sh
Conclusion
I hope this will help some people to better protect their personal data.If I can be of any help, you can email me at chapeaurouge_AT_madpenguin_DOT_org. Thanks.
Fred Blaise
Disclaimer: I cannot be held responsible for any data loss due to this HOWTO.













