Backup linux server with rsync

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

mkdir /usr/hostonnet/
vi /usr/hostonnet/



# Set following cronjob to run it daily
# @weekly /usr/hostonnet/ >/dev/null 2>&1


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`"

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

make it executable

chmod 755 /usr/hostonnet/

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


Now lets do a test backup by running command


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


@weekly /usr/hostonnet/ >/dev/null 2>&1

Posted in Linux