| Subcribe via RSS

Automated Backup via Rsync, Tar and SCP

January 22nd, 2009 Posted in Backup & Restore, Linux

In the previous article, I have discussed about synchronizing data with rync. In this article I will continue the topic to a new discussion: how to use rsync for automated backup. Here are the scenarios that I use in this article:
There are 3 servers (Server1, Server2, Server3) that have data in it and need to be backed up daily to a backup server (Backupsvr) in directory /backups. I will use a user account named backupusr on backupsvr. The backupusr have full control access to directory /backups. I will prepare the public/private key pair for backupusr and use it for key-based authentication. For the backup process, I will use these three steps:
1) Use rsync to back up data in each server in to a separate directory in the each local server. By doing this, each server will have backup copy of its data in separate directory that can be restored quickly and easily when needed.
2) Create a compressed archive file of backed-up directory for offsite transfer. This process will create a compact file that can be easily transfer offsite.
3) Transfer the compressed archive file to the backupsvr using SCP. In this step, I will use key-based authentication for SCP.

Here are the detail steps :

Preparing the public/private dsa key pair and place the private key in backupsvr and distribute the public key the server-server.

In Server1
1. Create user account backupusr. I will use this user account to generate public/private dsa key pair.
[root@server1 ~]# useradd backupusr
[root@server1 ~]# passwd backupusr
Changing password for user backupusr.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
[root@server1 ~]#

2. Generate public/private dsa key pair for user backupusr. I will create key pair called identity (identity and identity.pub) in backupusr home directory with null-passphrase.
[root@server1 ~]# su - backupusr
[backupusr@server1 ~]$ ssh-keygen -f identity-backupusr -t dsa -N ""
Generating public/private dsa key pair.
Your identification has been saved in identity-backupusr.
Your public key has been saved in identity-backupusr.pub.
The key fingerprint is:
26:e3:55:7a:fd:0f:e8:a6:5c:52:bf:a4:eb:bc:69:cd [email protected]
[backupusr@server1 ~]$

3. Copy the identity file to root home directory. Copying this private key file is to make it easier for root to access the file.
[backupusr@server1 ~]$ exit
[root@server1 ~]# cp /home/backupusr/identity-backupusr .

In Backupsvr
1. Create user account backupusr.
[root@backupsvr ~]# useradd backupusr
[root@backupsvr ~]# passwd backupusr
Changing password for user backupusr.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.

2. Create directory /backups and give permissions only to the backupusr
[root@backupsvr ~]# mkdir /backups
[root@backupsvr ~]# chown backupusr.backupusr /backups
[root@backupsvr ~]# chmod 700 /backups

3. Log on as backupusr in Backupsvr, create .ssh directory and copy the identity-backupusr.pub (public key) from Server1, copy the identity-backupuser.pub file to .ssh/authorized_keys file and change the permissions.
[backupusr@backupsvr ~]$ scp [email protected]:identity-backupusr.pub .
The authenticity of host 'server1.mydomain.com (1.2.3.2)' can't be established.
RSA key fingerprint is 8d:35:1e:e4:1a:87:26:9a:b0:96:c3:5d:cd:1b:c1:ed.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'server1.mydomain.com' (RSA) to the list of known hosts.
backupusr@backupsvr's password:
identity-backupusr.pub 100% 615 0.6KB/s 00:00
[backupusr@backupsvr ~]$ cat identity-backupusr.pub >> .ssh/authorized_keys
[backupusr@backupsvr ~]$ chmod 700 .ssh
[backupusr@backupsvr ~]$ chmod 700 .ssh/authorized_keys

In Server2
1. Copy the identity file (private key file) from Server1 to root home directory in Server2.
[root@server2 ~]# scp [email protected]:identity-backupusr .
The authenticity of host 'server1.mydomain.com (1.2.3.2)' can't be established.
RSA key fingerprint is 8d:35:1e:e4:1a:87:26:9a:b0:96:c3:5d:cd:1b:c1:ed.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'server1.mydomain.com' (RSA) to the list of known hosts.
backupusr@server1's password:
identity-backupusr.pub 100% 615 0.6KB/s 00:00
[root@server2 ~]#

2. Test the connection from Server2 to Backupsvr using key-based SSH authentication. If login no longer requires a password, then the key-based ssh authentication is working.
[root@server2 ~]# ssh –i identity-backupusr backupsvr.mydomain.com
[backupusr@backupsvr ~]$

In Server3
1. Copy the identity file (private key file) from Server1 to root home directory in Server3.
[root@server3 ~]# scp [email protected]:identity-backupusr .
The authenticity of host 'server1.mydomain.com (1.2.3.2)' can't be established.
RSA key fingerprint is 8d:35:1e:e4:1a:87:26:9a:b0:96:c3:5d:cd:1b:c1:ed.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'server1.mydomain.com' (RSA) to the list of known hosts.
backupusr@server1's password:
identity-backupusr.pub 100% 615 0.6KB/s 00:00
[root@server3 ~]#

2. Test the connection from Server3 to Backupsvr using key-based SSH authentication. If login no longer requires a password, then the key-based ssh authentication is working.
[root@server3 ~]# ssh –i identity-backupusr backupsvr.mydomain.com
[backupusr@backupsvr ~]$

In Server1
1. Test the connection from Server1 to Backupsvr using key-based SSH authentication. If login no longer requires a password, then the key-based ssh authentication is working.
[root@server1 ~]# ssh –i identity-backupusr backupsvr.mydomain.com
[backupusr@backupsvr ~]$

The next step is to create the backup script file and schedule it.

In Server1
Prepare the backup script

[root@server1 ~]# vi /backups/backup.sh

#!/bin/bash

EXT_BACKUP_PATH=”[email protected]:/backups”
INT_BACKUP_PATH=”/backups”
DIRS=”/data /home”

weekday=`date +%a`
shorthostname=`hostname -s`

# Backup to local directory
rsync -avHK –delete $DIRS $INT_BACKUP_PATH/$weekday

# Create archive of backed-up directory for offsite transfer
tar -zcvf $INT_BACKUP_PATH/$shorthostname.$weekday.backup.gz $INT_BACKUP_PATH/$weekday

#Transfer archives file to the backup server
scp -i /root/identity-backupusr $INT_BACKUP_PATH/$shorthostname.$weekday.backup.gz $EXT_BACKUP_PATH

Then make the file executable and scheduled the script. In this scenario, I scheduled the script to run every night using cron.

[root@server1 ~]# chmod +x /backups/backup.sh
[root@server1 ~]# crontab –e
@daily sh /backups/backup.sh

Then create the same script for server2 and server3 then scheduled the script.

Note: you can refer to an article about crontab if you want it to run at other times.

In Server2 and Server3
Create the backup script (or copy it from server1) then schedule it

When the scheduled time comes, the server will backup the data via rsync, archive the backed up copy then transfer the archive file to the backup server.

Tags: , ,

44 Responses to “Automated Backup via Rsync, Tar and SCP”

  1. Wphxpy Says:

    order tricor 160mg generic buy tricor 160mg pill buy tricor pills for sale


  2. Ompesw Says:

    purchase ketotifen without prescription buy doxepin pills for sale buy tofranil 75mg online


  3. Hgjdpq Says:

    buy generic cialis 10mg sildenafil 50mg order sildenafil 100mg pill


  4. Vgpzkm Says:

    acarbose 25mg without prescription griseofulvin 250 mg ca fulvicin sale


  5. Mitryp Says:

    buy mintop generic minoxytop price best ed medications


  6. Czqptc Says:

    order aspirin 75 mg online cheap buy generic zovirax buy zovirax sale


  7. Ykamxa Says:

    order dipyridamole online dipyridamole online pravachol 20mg ca


  8. Iigfty Says:

    buy melatonin online brand danazol order danazol 100 mg for sale


  9. Qujxcb Says:

    purchase florinef pills fludrocortisone 100mcg pill imodium 2mg sale


  10. Xwmrgg Says:

    brand duphaston order empagliflozin 10mg pills buy generic jardiance online


  11. Rgayfa Says:

    buy prasugrel pills how to buy tolterodine order tolterodine sale


  12. Bosdyp Says:

    etodolac 600mg drug etodolac 600 mg drug cost pletal


  13. Ciqeiy Says:

    ferrous 100 mg pill generic actonel 35 mg order betapace 40mg pills


  14. Fzwgni Says:

    order mestinon 60 mg maxalt online order rizatriptan 10mg over the counter


  15. Ayhcmy Says:

    enalapril 5mg pill lactulose tubes order generic lactulose


  16. Pdceap Says:

    betahistine 16mg oral betahistine cost probalan over the counter


  17. Bkxdme Says:

    generic zovirax order exelon 6mg without prescription order rivastigmine


  18. Tgmeru Says:

    prilosec 20mg ca omeprazole 20mg over the counter buy generic metoprolol 50mg


  19. Zerqab Says:

    brand premarin cost viagra sildenafil without a doctor’s prescription


  20. Mhejuy Says:

    buy generic telmisartan for sale brand molnupiravir 200mg molnunat order online


  21. Hzgjvq Says:

    oral cenforce 100mg buy cenforce no prescription chloroquine 250mg usa


  22. Eenvxb Says:

    buy tadalafil online cheap order cialis pills sildenafil for sale


  23. Wqtpfo Says:

    oral cefdinir 300mg cefdinir pill cheap lansoprazole


  24. Ayiwms Says:

    provigil 200mg generic prednisone generic prednisone 40mg pills


  25. Yepbuc Says:

    order isotretinoin 20mg generic zithromax 500mg pills purchase azithromycin sale


  26. Fpgguj Says:

    order lipitor 20mg for sale order amlodipine pills norvasc pills


  27. Ffaomc Says:

    order azithromycin 250mg cost azipro 500mg buy gabapentin medication


  28. Adojrk Says:

    pantoprazole 40mg for sale pyridium order online phenazopyridine 200 mg tablet


  29. Jnvbub Says:

    casino slot free buy furosemide 40mg without prescription order lasix 40mg generic


  30. Fjliqh Says:

    play online blackjack doxycycline 100mg over the counter ventolin 4mg sale


  31. Bnpazc Says:

    symmetrel 100 mg price buy generic amantadine 100 mg dapsone 100 mg pills


  32. Evbumc Says:

    online poker real money stromectol oral ivermectin canada


  33. 6388 Says:

    Heya! I’m aat work browsing our blog froom mmy new apple iphone!
    Jusst wanted tto saay I loe reading our bloog and liok forward tto alll yoiur posts!
    Carry on tthe great work!


  34. Klbahg Says:

    online money poker synthroid drug buy levothyroxine pills for sale


  35. Ovxqqp Says:

    buy medrol pills for sale nifedipine tablet triamcinolone 10mg cheap


  36. Miwzxb Says:

    purchase clomid generic buy imdur medication buy azathioprine medication


  37. Pvwapu Says:

    buy vardenafil pills for sale generic vardenafil 20mg tizanidine online


  38. Azwazn Says:

    aceon 4mg without prescription clarinex 5mg over the counter buy fexofenadine pills for sale


  39. Brudxj Says:

    dilantin canada order generic flexeril 15mg purchase ditropan


  40. Scvdby Says:

    order loratadine 10mg online cheap purchase dapoxetine without prescription dapoxetine where to buy


  41. Hxjsnm Says:

    order ozobax for sale endep 10mg uk how to buy toradol


  42. Bnrltu Says:

    baclofen 10mg canada buy ketorolac pills for sale purchase toradol sale


  43. Ppjzzl Says:

    amaryl brand glimepiride sale purchase etoricoxib


  44. Updahspar Says:

    60mg priligy Afterwards, the Duke of Bavaria led his troops to pursue it, expanded the land for hundreds of miles, and plundered three cities, Roger smiled, Said There are no shortcuts to magic training, But it shouldn t take four dollar blood pressure medicine at walmart long, I should be a tenth level mage, I will let you experience the harm of decomposition


Leave a Reply