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
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics via:

Category List of Unix and Linux commands
Disk space analyzers df duf ncdu pydf
File Management cat cp mkdir tree
Firewall Alpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilities bat exa
Network Utilities NetHogs dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searching ag grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN Alpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
2 comments… add one
  • Braykov Apr 29, 2017 @ 10:33

    Nice!
    And you can drop the test condition for _dest. The -p switch of mkdir covers the case.

  • Epy Apr 30, 2017 @ 10:14

    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 :)

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum