Skip to content

Instantly share code, notes, and snippets.

@dlford
Last active March 10, 2024 18:23
Show Gist options
  • Save dlford/dbc83753f38c7334fac52d3cb85727ee to your computer and use it in GitHub Desktop.
Save dlford/dbc83753f38c7334fac52d3cb85727ee to your computer and use it in GitHub Desktop.
A bash script to backup multiple MikroTik devices all at once
#!/bin/bash
set -euo pipefail
# MikroTik Multi-Device Backup Script
#
# Copyright 2024 DL Ford
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# This script is designed to backup multiple MikroTik devices at once.
# It will create a backup file and optionally a GPG encrypted RSC file for each device.
# The backups are saved to a local directory.
#
# The script requires the following:
# - DNS resolution for each device by an internal domain (e.g. router.your.local.domain)
# - SSH access to each device using an SSH key
# - Optionally, GPG set up on your local machine for plaintext RSC backups
# ----- Config -----
# List all devices to backup, comma separated.
DEVICES_LIST="router,switch"
# Your internal domain, DNS should point to each device above.
# e.g. "router.your.local.domain", "switch.your.local.domain", ...
DOMAIN="your.local.domain"
# Path to save backups locally ("$HOME" = "/home/yourusername").
SAVEPATH="$HOME/Backups"
# Description makes up part of the filename, e.g. "router_[DESCRIPTION].backup".
# I prepend the description with todays date, but you can use any format you like.
DESCRIPTION="2024-03-10_breif-description-for-filename"
# If provided, script will create a GPG encrypted RSC plaintext backup
# in addition to the regular backup, leave blank to skip this step.
# Must have GPG set up on your local machine, put your email to encrypt for yourself.
GPG_EMAIL=""
# The encryption password for the MikroTik backup file.
PASSWORD=""
# ------------------
if [ -z "$PASSWORD" ]; then
echo "PASSWORD IS EMPTY!!!"
exit 1
fi
# Converts device list to an array
readarray -td, DEVICES <<<"$DEVICES_LIST,"; unset 'DEVICES[-1]'; declare -p DEVICES > /dev/null;
for DEVICE in ${DEVICES[@]}; do
FILENAME="${DEVICE}_${DESCRIPTION}"
if [ -n "$GPG_EMAIL" ]; then
echo "Creating $FILENAME.rsc.gpg ..."
ssh "$DEVICE.$DOMAIN" "/export show-sensitive" | gpg --output "$SAVEPATH/$FILENAME.rsc.gpg" --recipient "$GPG_EMAIL" --encrypt
fi
echo "Creating $FILENAME.backup on $DEVICE.$DOMAIN ..."
ssh "$DEVICE.$DOMAIN" "/system/backup/save name=$FILENAME password=$PASSWORD"
echo "Copying to localhost:$SAVEPATH/ ..."
scp "$DEVICE.$DOMAIN:/$FILENAME.backup" "$SAVEPATH/$FILENAME.backup"
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment