Shell script to mirror backups to another server in pull mode using rsync

in Categories Automation & Management, Backup last updated April 28, 2017

A shell script mirror remote server named server1.cyberciti.biz and /backups directory to another server. The script use rsync command and also make sure a special directory mounted in local server. You must setup ssh keys (see “How To Set Up SSH Keys” for more info).

#!/bin/bash
# Purpose: Mirror remote server backup using rsync command
# Author: Vivek Gite {https://www.cyberciti.biz/}
# License: GPL ver 2.x+
# Note: Tested on Debian/Ubuntu and CentOS only.
# ------------------------------------------------------------
 
## change me ##
_mnt="/dev/mapper/offsite-backups"
_user="vivek"
_host="server1.cyberciti.biz" 
_dir="/backups"
_dest="/offsite-backups/mirror.${_host}"
 
# Make sure offsite backup directory mounted
mount | grep -q ${_mnt}
if [ $? -eq 0 ]
then
  # Make dir if not found
  [ ! -d "$_dest" ] && mkdir -p "${_dest}"
  # alright mirror it. make sure you setup ssh-keys
  rsync --bwlimit 10000 --delete  -P -az -H --numeric-ids ${_user}@{_host}:${_dir} ${_dest}
else
  echo "$0: Error '${_dest}' not mounted." 
  exit 999
fi

Share this on:

2 comment

  1. Hello
    Thanks for this script, I’ll need it soon :)
    I suggest using ‘mountpoint -q’ to check if the destination directory is mounted, it’s its goal :)

    Have a question? Post it on our forum!