Mikrotik SFTP backup script

# Variables
:local sftpServer "<SFTP Server IP>"
:local sftpUser "<SFTP User>"
:local sftpPass "<SFTP User Password>"

# Set subdirectory on SFTP server
:local sftpDirectory "/data"

#Define what port the SFTP server is listening on
:local sftpPort 22


# Define binary backup and RSC export file names
:local backupFileName ("config_backup_" . [/system identity get name] . "_" . [:put [/system clock get date]]. "_" . [:put [/system clock get time]])

:local exportFileName ("config_export_" . [/system identity get name] . "_" . [:put [/system clock get date]] . "_" . [:put [/system clock get time]])


# Generate binary backup
/system backup save name=$backupFileName
  

# Generate export
/export file=$exportFileName

# Wait for backup files to be created
:delay 10s
 
# SFTP URL format
:local sftpUrl ("sftp://" . $sftpServer . ":" . $sftpPort . $sftpDirectory . "/")

# Upload binary backup to SFTP server
/tool fetch url=($sftpUrl . $backupFileName . ".backup") src-path=($backupFileName . ".backup") user=$sftpUser password=$sftpPass upload=yes

# Upload export to SFTP server
/tool fetch url=($sftpUrl . $exportFileName . ".rsc") src-path=($exportFileName . ".rsc") user=$sftpUser password=$sftpPass upload=yes

# Remove local binary backup file
/file remove ($backupFileName . ".backup")

# Remove local backup file
/file remove ($exportFileName . ".rsc")


References