Tuesday, May 20, 2014

RSYNC script for automated backup on CentOS


For the
 purpose of this excercise, both servers are on the same network.
Main Centos Server's IP: 192.168.0.1
Backup CentOS Server's IP: 192.168.0.2
As this uses SSH you could use port forwarding and external IP's to perform this remotely.

Modify Firewall Rules

Modify firewall rules to allow ssh connections IF REQUIRED:
[root@192.168.0.1 ~]# vi /etc/sysconfig/iptables
Press enter.

Append the following line BEFORE the REJECT line:
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT

Save and close and reload the firewall rules:
[root@192.168.0.1 ~]# service iptables restart
Press enter.

Be aware that the first time this script is run it may take a long time depending on directory sizes and link speed.
The script will only copy modified data after this time (with the exception of the databse.sql which is usually very small).
NB: These scripts must be saved as a executable file and run from terminal.

Generating a private/public key

N.B: Modify directory names and IP address as required.
Step 1: Create public and private keys using ssh-key-gen on the backup server
[root@192.168.0.2 ~]# ssh-keygen

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):[press enter]

Enter passphrase (empty for no passphrase): [Press enter]

Enter same passphrase again: [Pess enter]

Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
33:b3:fe:af:95:95:18:11:31:d5:de:96:2f:f2:35:f9 root@localhost

Step 2: Copy the public key to remote-host using ssh-copy-id
[root@localhost ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.0.1
root@192.168.0.1's password:[enter password here]
Now try logging into the machine, with "ssh 'remote-host'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.

Creating the Backup Script

Now for the backup script.
Option 1:
#!/bin/sh

## N.B: Modify the VARIABLES and database name as required.
#Name the script 'rsynccentos.sh' and save in /home/sshuser/cron/
#cd /home/sshuser/cron/
#chmod +x rsynccentos.sh
#./rsynccentos.sh


#-----------------------------------------------
#VARIABLES
RSYNC=/usr/bin/rsync
SSH=/usr/bin/ssh
KEY=~/.ssh/id_rsa
RUSER=root
RHOST=192.168.0.1
RPATH1=/var/www/html
RPATH2=/home/sshuser/sqldump
RPATH3=/etc/httpd/conf
LPATH1=/var/www
LPATH2=/home/sshuser
LPATH3=/etc/httpd
DT=$(date +%d%m%y)
#END OF VARIABLES
#-----------------------------------------------
#
echo START OF CRON JOB
#
echo Removing old sql files
rm -f /home/sshuser/sqldump/*sql
echo files removed
#
echo dumping database from remote host and transferring to localhost
$SSH -i $KEY $RUSER@$RHOST mysqldump --user=mysqluser --password=mysqlpassword databasename --databases > /home/sshuser/sqldump/databasename.sql
echo mysqldump and transfer complete
#
echo Start copy of html folder....
$RSYNC -tav -e "$SSH -i $KEY" $RUSER@$RHOST:$RPATH1 $LPATH1
echo Copying Completed
#
echo changing directory
cd /home/sshuser/sqldump/
#
echo running databasename.sql sql script on localhost server.....
mysql --user=mysqluser --password=mysqlpassword databasename < databasename.sql
echo database restore complete!
#
echo Archiving todays sqldump folder....
tar -cjf /home/sshuser/sqlbackup/sqldump-$DT.tar /home/sshuser/sqldump
echo archive completed.
#
echo Restarting Services
service mysqld restart
service httpd restart
echo Job Complete.
Option 2
I needed to modify the script for copying from CentOS 6.x to CentOS 5.x.....
#!/bin/sh

## N.B: Modify the VARIABLES and database name as required.
#cd /home/sshuser/cron/
#chmod +x rsynccentos.sh
#./rsynccentos.sh

#-----------------------------------------------
#VARIABLES
SSH=/usr/bin/ssh
RSYNC=/usr/bin/rsync
RUSER=root
RHOST=192.168.0.1
RPATH=/var/www/html
LPATH1=/root/sqldump
LPATH2=/root/sqlbackup
LPATH3=/var/www/
DT=$(date +%d-%m-%Y)
#END OF VARIABLES
#-----------------------------------------------
echo START OF CRON JOB
#
#Remove existing *.sql dump files from CentOS.
rm -f $LPATH1/*sql
#
echo Dumping mediawikidb from the remote-host and transferring to localhost
$SSH $RUSER@$RHOST mysqldump databasename --databases > $LPATH1/databasename.sql
echo mediawikidb.sql dump has been created and can be found in /root/sqldump/.
#
echo running mediawikidb.sql mysql script on localhost.....
mysql -u root -e"\. $LPATH1/databasename.sql"
service mysqld restart
echo databasename database restore complete!
#
echo Archiving todays sqldump folder....
tar -cjf $LPATH2/databasename-$DT.tar $LPATH1/
echo Archive completed and can be found in /root/sqlbackup/.
#
echo Copying html directory from the remote-host to localhost CentOS Server....
$RSYNC -tav -e $SSH $RUSER@$RHOST:$RPATH $LPATH3
service httpd restart
echo Copying Completed!
#
echo JOB WELL DONE!

Creating the Cronjob

Now create a cronjob on the Backup CentOS Server.
Create cronjob to automate running of the script.
Modify directory and times as required.
Use 'crontab -e' in terminal to insert a line for this new cron job:
[root@192.168.0.2 ~]# crontab -e
i
0 8,18 * * * /home/sshuser/cron/rsynccentos.sh
:wq
This will run the script automatically at 8am and 6pm every day.

Testing

Test the script.
[root@localhost ~]# cd /home/sshuser/cron/

[root@localhost cron]# ./rsynccentos.sh

0 comments: