Community discussions

MikroTik App
 
MatiasSemino
just joined
Topic Author
Posts: 1
Joined: Sat Jul 22, 2023 9:34 pm

BACKUP Automatic

Thu Feb 15, 2024 6:55 pm

Hello everyone. Create a script for backup, with some variables that can be modified to customize some parameters. The idea of the script is to take backup with the name of the identity and the current date automatically, so just pasting it on a computer and scheduling a schedule would be guaranteed the copy.
I hope you like it and find it useful. I also accept comments to make modifications and see if we can improve it or add more things!

Hola a todos. cree un script para backup, con algunas variables que se pueden modificar para personalizar algunos parametros. la idea del script en tomar backup con el nombre del identity y la fecha actual automaticamente, asi solopegnadolo en un equipo y programando un schedule tendrian garantizada la copia.
Espero que les guste y les sirva. Tambien acepto comentarios para hacerle modificaciones y ver de mejorarlo o agregarle mas cosas!
You do not have the required permissions to view the files attached to this post.
 
User avatar
BartoszP
Forum Guru
Forum Guru
Posts: 2880
Joined: Mon Jun 16, 2014 1:13 pm
Location: Poland

Re: BACKUP Automatic

Thu Feb 15, 2024 9:01 pm

It would be better to use English descriptions/comments. However thank you for your effort.
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: BACKUP Automatic

Thu Feb 15, 2024 11:32 pm

The idea of the script is to take backup with the name of the identity and the current date automatically, so just pasting it on a computer and scheduling a schedule would be guaranteed the copy.
I hope you like it and find it useful. I also accept comments to make modifications and see if we can improve it or add more things!
Too many global variables, also the date format of your script is already obsolete, from version 7.10+ the date format changes from:
apr/22 or apr/22/2023 to 04-22 or 2023-04-22.
 
drpioneer
just joined
Posts: 10
Joined: Mon Nov 01, 2021 8:33 am

Re: BACKUP Automatic

Sat Feb 17, 2024 9:55 am

Hi!
An alternative version of the script:
https://github.com/drpioneer/MikrotikBa ... backup.rsc
- automatically determines the data storage location,
- allows you to set the archive depth in days,
- looks after free space on the drive,
- saves a backup file, a settings file and a log file,
- file names is based on the device ID and date,
- works on RouterOS 6.xx and 7.xx
Last edited by drpioneer on Sat Feb 17, 2024 7:07 pm, edited 3 times in total.
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: BACKUP Automatic

Sat Feb 17, 2024 11:24 am

Another simpler method, for RouterOS 7.10+ versions for backup, sending files to an ftp server, local deletion of files and telegram notification.

# ------------------
# Function: Telegram
# ------------------
:global tgFunc do={
    :do {
        :local BotToken "XXXXXXXXXX:XXXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXX"
        :local ChatID "XXXXXXXXX"
        :local parseMode "HTML"
        :local DisableWebPagePreview true
        :local SendText $1

        /tool fetch url="https://api.telegram.org/bot$BotToken/sendMessage\?chat_id=\
            $ChatID&parse_mode=$parseMode&text=$SendText&disable_web_page_preview=\
            $DisableWebPagePreview" keep-result=no
        
        :log info "=> Telegram message sent successfully"
    } on-error={
        :log info "=> Telegram message sent failed"
    }
}

# ---------------------------------
# Function: Change Date/Time format
# ---------------------------------
:global date2ymd do={
    # Only Current Date: v7.10+ "2023-04-22 16:33:41" or "04-22 16:33:41"
    :local dtime [:tostr $1]
    :local yyyy; :local MM; :local dd
    :local hh; :local mm; :local ss

    /system clock
    :set yyyy [:pick $dtime 0 4]; :set dd [:pick $dtime 8 10]; :set MM [:pick $dtime 5 7]
    :set hh [:pick $dtime 11 13]; :set mm [:pick $dtime 14 16]; :set ss [:pick $dtime 17 19]
    :return "$yyyy$MM$dd-$hh$mm$ss"
}

# ------------
# Main script
# ------------
:global tgFunc
:global date2ymd
:local sysname [/system identity get name]
:local mydatetime "$[/system clock get date] $[/system clock get time]"
:local textfilename
:local backupfilename

# FTP Config
:local ftpserver "192.168.88.200"
:local username "mikrotik"
:local password "MyStR0ngPaSsWoRd!"
:local port 21

# Check for white spaces in system identity to replace with underscores
:if ([:find $sysname " "] !=0) do={
    :local name $sysname
    :local newname ""
    :for i from=0 to=([:len $name]-1) do={
        :local tmp [:pick $name $i]
        :if ($tmp !=" ") do={:set newname "$newname$tmp"}
        :if ($tmp =" ")  do={:set newname "$newname_"}
    }
    :set sysname $newname;
}

:set mydatetime [$date2ymd $mydatetime]
:set textfilename ($"mydatetime" . "-" . $"sysname" . ".rsc")
:set backupfilename ($"mydatetime" . "-" . $"sysname" . ".backup")

:log info ">>> START FTP Backup <<<"

:execute [/export file=$"textfilename" show-sensitive]; :delay 3s
:execute [/system backup save name=$"backupfilename"]

# Sent backup files to ftp server
:delay 3s
/tool fetch address=$ftpserver mode=ftp user=$username password=$password \
    src-path=$textfilename dst-path=$textfilename upload=yes port=$port;

:delay 3s
tool fetch address=$ftpserver mode=ftp user=$username password=$password \
    src-path=$backupfilename dst-path=$backupfilename upload=yes port=$port;

# Delete files
:delay 5s
/file remove $textfilename
/file remove $backupfilename

$tgFunc ("\F0\9F\92\BE <b>$sysname:</b> Backup OK!")

:log info ">>> END FTP Backup <<<"

Who is online

Users browsing this forum: No registered users and 6 guests