Remote Backup of Linux (Unix) Server to Local Extenral Hard Drive Using SSH, tar, SCP, Cron and cygwin

No GoodNeed ImprovementOKGoodExcellent (2 votes, average: 5 out of 5)
Loading ... Loading ...

If you want to save money or have a copy of your company’s data in different location, you can use cheap USB disks for backing up data on remote location (e.g., data center).

1, Buy a Extenral Hard Drive, install Cygwin (http://www.cygwin.com/) on your local computer

2, Generating your private/public key pair.

Open a Cygwin window, type:

ssh-keygen -t rsa

Do not enter password (press enter to skip) in the following steps:

Generating public/private rsa key pair.
Enter file in which to save the key (/home/Alex/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/Alex/.ssh/id_rsa.
Your public key has been saved in /home/Alex/.ssh/id_rsa.pub.
The key fingerprint is:
16:d7:e0:71:74:a9:4a:d7:e0:72:42:f9:79:01:86:76 Alex@MyDellLaptop

3. Now you have your private key in ~/.ssh/id_rsa and public key in ~/.ssh/id_rsa.pub. Keep your private key as top secret and upload public key to your remote server:

ssh remoteserver "mkdir .ssh; chmod 600 .ssh"
scp ~/.ssh/id_rsa.pub remoteserver:.ssh/authorized_keys2
alex@MyDellLaptop:~$ scp ~/.ssh/id_rsa.pub remoteserver:.ssh/authorized_keys2
alex@MyDellLaptop’s password:
id_rsa.pub 100% 225 0.0KB/s 00:00
alex@MyDellLaptop:~$

4. Write a simple shell script backup.sh for backup:

#!/bin/bash
ssh alex@remoteserver “cd /home; tar cvf labsupplymall.tar labsupplymall”
ssh alex@remoteserver “cd /home; gzip labsupplymall.tar”
scp alex@remoteserver:/home/labsupplymall.tar.gz /cygdrive/f/labsupplymall_backup/.
ssh alex@remoteserver “cd /home; rm -rf labsupplymall.tar.gz”

You may want to use dos2unix to remove ^M

$ dos2unix backup.sh

5. Create a cron job:

crontab -e

This will start backup job every Sunday at 1 AM.

0 1 * * 0 /home/Alex/backup.sh

Save the file, and sit back and relax.

Technorati : , , , , , , , ,

Popularity: 1%

Leave a Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word