You are on page 1of 5

Automated Server Backup with Rsnapshot,Rsync and SSH

# Reference: http://www.rsnapshot.org/howto/1.2/rsnapshot-HOWTO.en.html

# Client Side Setup

# Local backups on client using Rsnapshot

# Download rsnapshot from http://www.rsnapshot.org/

cd /tmp

wget http://www.rsnapshot.org/downloads/rsnapshot-1.3.0-1.noarch.rpm

rpm -Uvh rsnapshot-1.3.0-1.noarch.rpm

rm -rf rsnapshot-1.3.0-1.noarch.rpm

cp -a /etc/rsnapshot.conf /etc/rsnapshot.conf.orig

vi /etc/rsnapshot.conf

# The directory /.snapshots will be where the local backups are taken.You can also
specify a different backup directory

snapshot_root /.snapshots/

#########
#Edit the line as shown and uncomment all other interval lines
interval daily 1

# Specify all directories to be backedup as shown below eg.if you want to backup
/etc and /var/www then make the following entries
backup /etc/ .
backup /var/www/ .
# Specify rysnc parameters as follows to enable softlink targets to be copied.
rsync_short_args -a -L
rsync_long_args --delete --numeric-ids --relative --delete-excluded --copy-links

# Please ensure that all fields are separated by tabs( and not spaces) otherwise
an error will be encountered
#########

# Backing up Mysql Databases using Rsnapshot and without using a password

# To enable password-less backup do the following

cp -a /etc/my.cnf /etc/my.cnf.orig

vi /etc/my.cnf

####
# Add the following lines
[mysqldump]

user = root

password = <mysql_root_password>
host = localhost
####

Create a backup_mysql.sh shell script as follows

mkdir -p /root/backup_scripts

vi /root/backup_scripts/backup_mysql.sh
#!/bin/sh
# To backup all databases
/usr/bin/mysqldump --all-databases > mysqldump_all_databases.sql
# To backup a selected database
/usr/bin/mysqldump db_name > db_name.sql
# To backup a selected table in a database
/usr/bin/mysqldump db_name table_name > table_name.sql
# Make the backup readable only by root
/bin/chmod 600 mysqldump_all_databases.sql
########

chown root.root /root/backup_scripts/backup_mysql.sh


chmod 700 /root/backup_scripts/backup_mysql.sh

vi /etc/rsnapshot.conf

# Add the following line

backup_script /root/backup_scripts/backup_mysql.sh mysql_backups/


# Please ensure that the above three fields are separated by tabs( and not spaced)
otherwise an error will be encountered
# Mysqld should be running when the Mysql backups are taken by rsnapshot else an
error will be encountered and Mysql db backup will not happen.Other backups will
howvever happen without any issues.

# Setup Rsync to backup this data to a remote backup server

# Generate DSA keys

#On your server which you want to backup do this as root.


cd /root

ssh-keygen -t dsa -b 1024 -f root_dsa -C 'Root dsa key'

mv root_dsa /root/.ssh/

scp -P222 root_dsa.pub username@backup_server:/tmp

# On your backup server where you want to store your backups,do this as root

su

cd /tmp

/usr/sbin/useradd <backup_account>

/usr/bin/passwd <backup_account>

# Assign a strong password (minimum 8 characters with characters( both uppercase


and lowercase) numbers and special characters)

mkdir -p /home/<backup_account>/.ssh

cat /tmp/root_dsa.pub >> /home/<backup_account>/.ssh/authorized_keys

chown -R <backup_account>.<backup_account> /home/<backup_account>/.ssh


chmod 600 /home/<backup_account>/.ssh/authorized_keys

rm -rf /tmp/root_dsa.pub

# Please ensure that the following entries are present in your backup servers
/etc/ssh/sshd_config file or else your public key authentication wont work

cp -a /etc/ssh/sshd_config /etc/ssh/sshd_config_backup_<todays_date>

vi /etc/ssh/sshd_config

#####
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
#####

mkdir -p /remote_backups/<backed_up_server_hostname>

chown -R <backup_account>.<backup_account> /remote_backups/


chmod -R 700 /remote_backups/

exit

# On the server which you want to backup

# Check if you can connect to your backup server using the newly generated keys
ssh -p22 -i /root/.ssh/root_dsa <backup_account>@<backup_server>

# Create an rsync script which will rysnc the backups to your remote backup server

vi /root/backup_scripts/remote_rsync.sh

#!/bin/sh
rsync -auz --delete -e "ssh -p22 -i /root/.ssh/root_dsa" /.snapshots/daily.0/
<backup_account>@<remote_backup_server>:/remote_backups/<backed_up_server_hostname
>/

####

chown root.root /root/backup_scripts/remote_rsync.sh


chmod 700 /root/backup_scripts/remote_rsync.sh

# Comment out the existing cmd_postexec line and add this line to the
/etc/rsnapshot.conf file to enable rsync to run after rysync finishes the backup.

vi /etc/rsnapshot.conf

######
cmd_postexec /root/backup_scripts/remote_rsync.sh
######
# Run the following command to test your configuration.Fix issues till you get a
"Syntax OK" message.
/usr/bin/rsnapshot configtest

# This will print out a verbose list of the things it will do, without actually
doing them. To do a test run, run this command:
/usr/bin/rsnapshot -t daily

# Scheduled Job to backup daily


crontab -e
0 0 * * * /usr/bin/rsnapshot daily

# Backup Server Configuration


# Download rsnapshot from http://www.rsnapshot.org/

cd /tmp

wget http://www.rsnapshot.org/downloads/rsnapshot-1.3.0-1.noarch.rpm

rpm -Uvh rsnapshot-1.3.0-1.noarch.rpm

rm -rf rsnapshot-1.3.0-1.noarch.rpm

cp -a /etc/rsnapshot.conf /etc/rsnapshot.conf.orig

vi /etc/rsnapshot.conf

# Ensure that the directory /.snapshots is created where the backups are taken
locally.You can also specify a different backup directory

snapshot_root /.snapshots/

vi /etc/rsnapshot.conf

#########
#Edit the line as shown and uncomment all other interval lines.This defines the
number of backups to be retained
interval daily 7
interval weekly 4

# Specify the remote backup directory to be backedup


backup /remote_backups/ .

# Run the following command to test your configuration.Fix issues till you get a
"Syntax OK" message.
/usr/bin/rsnapshot configtest

# This will print out a verbose list of the things it will do, without actually
doing them. To do a test run, run this command:
/usr/bin/rsnapshot -t daily

# Scheduled Job to backup daily


crontab -e
0 4 * * * /usr/bin/rsnapshot daily
0 5 * * 0 /usr/bin/rsnapshot weekly

# Setup of log rotation of rsnapshot logs on the server to be backed up and the
remote backup server

cd /etc/logrotate.d/

vi rsnapshot

###
/var/log/rsnapshot {
missingok
notifempty
copytruncate
}
###
chown root.root rsnapshot
chmod 644 rsnapshot

You might also like