OpenText product name changes coming to the community soon! Learn more.

Wikis - Page

SLES OES 2018 rsync backup script, for multiple NSS volumes

3 Likes

My experience is that some customers do not have a backup system for daily, weekly, or monthly backups. Rsync is an excellent tool that can be used for all your daily, weekly, or monthly backups without any human interference.

You will need to generate an SSH key. This is important: We will be generating a passwordless SSH key. Yes, this could be considered a security issue, but hardening (securing) your OES 2018 server and assume this is being used on a secure network. If your network (or your source and target server) isn't secure, you probably shouldn't be considering such an option as passwordless backups. Always use rsync over SSH because rsync does not provide any security while transferring data.

However, if you have a need for an automated rsync backup from one server to another, here's how to do it.

You need to achieve the steps mentioned below with the rsync backup script:


1. Generate an SSH key on the source server, and ssh-copy the key to the target server.

2.  Use the script that will rsync backup multiple NSS Volumes from the source OES server to the target server.

3.  Schedule the rsync script in remote manager (NRM) to run every hour, day, week or month to make backups of the data on the target server.

4. A mail will be sent to the recipient's email address if the NSS data was backed up successfully or if it failed.




Step 1: Generate an SSH key to access the target server

Rsync without prompting for password, you can generate an SSH public key and add it to backup server’s ssh authorized keys. Below are the steps.

Assuming the OES 2018 server is Server1 and backup server is Server2.

Generate the public key on the source server

#ssh-keygen or use ssh-keygen -t rsa
#Enter passphrase ( empty for no passphrase
#Enter same passphrase again :
The public key will be generated and stored in :
#~/.ssh/id_rsa.pub
#cd /root
#cd .ssh to see the public key id_rsa.pub that was generated.

Use ssh-copy to copy public key to the target server:
ssh-copy-id -i ~/.ssh/id_rsa.pub server_ip_address – to your target server IP or DNS name.

Type in the target server password and enter. The SSH key is now added on the target server.

Step 2: Create your rsync bash script

Use the bash script below as an example to copy multiple NSS Volumes.

Here is an example of the complete rsync script:

#!/bin/bash

rsync -avz -e ssh /media/nss/DATA1/ root@server_ip_address:/backup/
rsync -avz -e ssh /media/nss/DATA2/ root@server_ip_address:/backup/

if [ $? -eq 0 ]
then
mail -s "rsync backup completed successfully" **PERSONAL INFORMATION REMOVED** < /etc/HOSTNAME
# rsync backup completed successfully @ $(date) for $(hostname)
else
/usr/bin/mail -s "rsync backup failed" **PERSONAL INFORMATION REMOVED** < /etc/HOSTNAME
# rsync failed @ $(date) for $(hostname)
fi



Save the name you want e.g. rsyncbck.sh.

Sections of the script is explained below:

1. rsync -avz -e
rsync a – archive, v – verbose -z compress file data during the transfer. -e -specify the remote shell to use.

2. ssh /media/nss/DATA1/ root@server_ip_address:/backup/
SSH into target server and rsync copy data from source server to the target server.

3. if [ $? -eq 0 ]
A basic if statement effectively says, if a particular test is true, then perform a given set of actions. $? will contain the exit status of the last command executed.-eq 0 - string1 = string2 is true if the two strings are equal.

4. mail -s "rsync backup completed successfully" **PERSONAL INFORMATION REMOVED** < /etc/HOSTNAME - /usr/bin/mail -s "rsync backup failed" **PERSONAL INFORMATION REMOVED** < /etc/HOSTNAME
mail to recipient mail address if backup was successful or failed. You can add any detail in the title of the mail message.




You may also add other rsync switches as can be seen in the man pages for rsync.

Change the example script mentioned below, IP address and the NSS Volumes data names, what you want to copy, to the target server. Also change the recipients mail address.

Copy your rsyncbck.sh script to the /usr/bin directory. See Figure 1.

Figure 1



Give the script executable permissions with the command - chmod x rsyncbck.sh

Take note: For the first backup use the above rsync command as mentioned.

rsync -avz -e ssh /media/nss/DATA1/ root@server_ip_address:/backup/

Use your favourite editor or vi into your rsyncbck.sh script, and change the rsync command for the second backup as mentioned below, otherwise you will keep on creating another DATA1 folder on your target server.

rsync -avz -e ssh /media/nss/DATA1/ root@server_ip_address:/backup/DATA1

Step 3: Configure Postfix

For your rsync script to mail you the output, make sure postfix is correctly configured on your source server. Change directory to /etc/postfix, edit the main.cf file. Make sure that the myhostname = hostname of the server and the mail relayhost server IP address is added in the main.cf.If you make changes to your main.cf file restart postfix services.

rcpostfix restart or systmectl restart postfix

Test your script run ./rsyncbck.sh.Test and see if the recipient received the mail. If you do not receive the mail, make sure to whitelist the recipients mail address.

When the script complete successfully make a change in your script to test the failed message. Add the following line to test the failed mail message.

rsync -avz -e ssh /media/failed root@server_ip_address:/backup/DATA1

Change your script back if all tests are done successfully.

rsync -avz -e ssh /media/nss/DATA1/ root@server_ip_address:/backup/DATA1

Step 4: Schedule a time for the rsyncbck script to run

Schedule a time for the rsyncbck script to run in Novell Remote Manager (NRM). Login to your OES 2018 server using your eDirectory credentials or root username and password, to connect to Novell Remote Manager (NRM) using the URL https://server_ipaddress:8009 as in Figure 2.

Figure 2



Select the tab Manage Linux and then Schedule Tasks. Add the hour, minute, day of the week and day of month you want the script to run. See Figure 3.

Figure 3



See crontab or cronjobs explained below:

- Minutes specified as a number from 0 to 59.
- Hours specified as numbers from 0 to 23.
- Days of the month, specified as numbers from 1 to 31.
- Months specified as numbers from 1 to 12.
- Days of the week, specified as numbers from 0 to 7.

You will see your script rsyncbck.sh being added by NRM under the directory/etc/cron.d/.

The example mentioned below shows that the rsync script will run everyday at 17H00. See Figure 4.

Figure 4



Your rsync backup script is now complete. This is how great rsync is, so you can relax and rsync will complete the backup job for you.

Labels:

How To-Best Practice
Comment List
Related
Recommended