HostOnNet Blog

Backup linux server with rsync

Looking for Linux Server Admin or WordPress Expert? We can help.

To backup linux server to remote server using rsync, create file

mkdir /usr/hostonnet/
vi /usr/hostonnet/rsync_backup.sh

Add

#/bin/bash

# Set following cronjob to run it daily
# @weekly /usr/hostonnet/rsync_backup.sh >/dev/null 2>&1
# https://blog.hostonnet.com/rsync-backup

REMOTE_BACKUP_LOCATION="root@BACKUP-SERVER-IP-HERE:/backup/`hostname`/"

date +"%d-%b-%Y %T" >> /home/backup_remote_time.txt

/usr/bin/rsync -avz "-e ssh -p 22" --exclude-from=/usr/hostonnet/rsync_backup_ignore.txt / $REMOTE_BACKUP_LOCATION

echo "Backup taken to $REMOTE_BACKUP_LOCATION" | /bin/mail -s "Backup finished on `hostname`" [email protected]

In the above code, replace BACKUP-SERVER-IP-HERE with your backup server IP. [email protected] with your email address, so you get email when backup task is run.

make it executable

chmod 755 /usr/hostonnet/rsync_backup.sh

We don’t need all files, so create a exclude file

vi /usr/hostonnet/rsync_backup_ignore.txt

Add following folders, that normally don’t need in most cases. You should remove any folder that you need to backup

/tmp
/srv
/lib
/bin
/lib64
/lost+found
/sbin
/media
/backup_drive
/backup
/mnt
/opt
/proc
/selinux
/sys
/dev
/hd2
/vz
/etc/webmin
/etc/selinux
/usr/bin
/usr/lib
/usr/lib64
/usr/local
/usr/share
/usr/libexec
/usr/sbin
/var/cache
/var/log

Now lets do a test backup by running command

/usr/hostonnet/backup_remote.sh

When you run first time, it ask you to save the ssh key fingerprint of backup server. Press Y.

Now if it ask you for password, press CTRL+C and enable ssh key authentication on your server, so that your server can login to backup server with out password.

Once password less authentication is setup, we can set cronjob to run backup weekly

crontab -e

Add

@weekly /usr/hostonnet/backup_remote.sh >/dev/null 2>&1

Posted in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.