/mikrotik-backup-to-git-script

A solution for backing up MikroTik configurations using Git

Primary LanguageRouterOS Script

MikroTik Configuration Backup Using Git

MikroTik does not natively support Git, but you can push configuration backups to a remote server and where they are automatically committed and pushed to a Git repository.

This guide will help you set up a system for backing up MikroTik configurations using Git. This lets you track and revert changes easily.

Prerequisites

  1. A remote server with PKI Authentication Enabled and a Git repository.
  2. Scheduler on your MikroTik to automate backups.

Set Up a Scheduler

Create a scheduler to run the backup daily:

/system scheduler add interval=1d name=config-daily-backup on-event="config-daily-backup-script"

Create the Config Backup Script

Configure Git on your remote server then Replace SFTP_SERVER_ADDRESS, SFTP_USERNAME, and REMOTE_PATH with your details:

:local config ("config_" . [/system identity get name])
:do {/export file=$config show-sensitive} on-error={/log error "Configuration export failed"}
:local sftpAddr "SFTP_SERVER_ADDRESS"
:local sftpUser "SFTP_USERNAME"
:local sftpDirectory "REMOTE_PATH"
:local sftpUrl ("sftp://" . $sftpAddr . ":" . $sftpDirectory . "/")
:do {
/tool fetch url=($sftpUrl . $config . ".rsc") src-path=($config . ".rsc") user=$sftpUser upload=yes;
:delay 5s;
/file remove ($config . ".rsc")
} on-error={/log error "File upload failed"}
:do {/system ssh-exec address=$sftpAddr user=$sftpUser command=("cd " . $sftpDirectory . "; git add " . $config . ".rsc; git commit -m \"config backup for " . [/system identity get name] . "\"; git push")} on-error={/log error "Git operations failed"}

Git Configuration

On your remote server, install and configure Git if not already done. Create and init Git repository in the desired directory

mkdir REMOTE_PATH
cd REMOTE_PATH
git init