Auto Backup MikroTik

Всім привіт !

Бакап то святе ! А автобакап то маст хев! Автобакап для мікротіків дуже корисна штука, а тому викладу два скрипти автобакапу, котрі використовую у себе дома:

Ідемо на WinBox в System – Scripts і додаємо скрипти

  1. Перший скрипт чесно злямзений на одному форумі:

#Your email address to receive the backups

:local toemail “user@gmail.com”

 

#The From address (you can use your own address if you want)

:local fromemail “user@gmail.com”

 

#A mail server your machines can send through

:local emailserver “smtp.gmail.com

 

#E-mail password

:local passEmail “password

 

#E-mail login

:local userEmail “user@gmail.com”

 

############## Don’t edit below this line ##############

 

:local sysname [/system identity get name]

:local textfilename

:local backupfilename

:local time [/system clock get time]

:local date [/system clock get date]

:local uptime [/system resource get uptime]

:local logMessages

:local newdate “”;

:for i from=0 to=([:len $date]-1) do={ :local tmp [:pick $date $i];

:if ($tmp !=”/”) do={ :set newdate “$newdate$tmp” }

:if ($tmp =”/”) do={}

}

#check for 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 logMessages “”

:foreach i in=[/log find ] do={

:set logMessages ($logMessages. [/log get $i time ]. ” “);

:set logMessages ($logMessages. [/log get $i message ]);

:set logMessages ($logMessages. “\n”)

}

:set textfilename ($”newdate” . “-” . $”sysname” . “.rsc”)

:set backupfilename ($”newdate” . “-” . $”sysname” . “.backup”)

:execute [/export file=$”textfilename”]

:execute [/system backup save name=$”backupfilename”]

#Allow time for export to complete

:delay 2s

 

#email copies

:log info “Emailing backups”

/tool e-mail send server=[:resolve $emailserver] port=587 user=$”userEmail” password=$”passEmail” start-tls=yes to=$”toemail” from=$”fromemail” subject=”[Config Backup *.rsc] $sysname $time $date” file=$”textfilename” body=”[Config Backup LOG] $logMessages”

#Send as different subjects to force GMail to treat as new message thread.

:local time [/system clock get time]

/tool e-mail send server=[:resolve $emailserver] port=587 user=$”userEmail” password=$”passEmail” start-tls=yes to=$”toemail” from=$”fromemail” subject=”[Config Backup *.backup] $sysname $time $date” file=$”backupfilename” body=”[Config Backup Uptime:] $uptime”

 

#Allow time to send

:delay 10s

 

#delete copies

/file remove $textfilename

/file remove $backupfilename

 

Скрипт робить бакап і скидує на пошту два листи.

  1. Але на пошту то звичайно добре, а хотілось би на якийсь сторадж, чи фтп, тож для фтп наступний скрипт:

# automated backup export ftp

# ftp configuration

:local ftphost “ftp.server”

:local ftpuser “ftp_user”

:local ftppassword “password”

:local ftppath “/”

# months array

:local months (“jan”,”feb”,”mar”,”apr”,”may”,”jun”,”jul”,”aug”,”sep”,”oct”,”nov”,”dec”);

# get time

:local ts [/system clock get time]

:set ts ([:pick $ts 0 2].[:pick $ts 3 5].[:pick $ts 6 8])

# get Date

:local ds [/system clock get date]

# convert name of month to number

:local month [ :pick $ds 0 3 ];

:local mm ([ :find $months $month -1 ] + 1);

:if ($mm < 10) do={ :set mm (“0” . $mm); }

# set $ds to format YYYY-MM-DD

:set ds ([:pick $ds 7 11] . $mm . [:pick $ds 4 6])

# file name for system backup – file name will be Mikrotik-servername-date-time.backup

:local fname1 (“/Mikrotik-“.[/system identity get name].”-“.$ds.”-“.$ts.”.backup”)

# file name for config export – file name will be Mikrotik-servername-date-time.rsc

:local fname2 (“/Mikrotik-“.[/system identity get name].”-“.$ds.”-“.$ts.”.rsc”)

# backup the data

/system backup save name=$fname1

:log info message=”System backup finished (1/2).”;

/export compact file=$fname2

:log info message=”Config export finished (2/2).”

# upload the user manager backup

:log info message=”Uploading system backup (1/2).”

/tool fetch address=”$ftphost” src-path=$fname1 user=”$ftpuser” mode=ftp password=”$ftppassword” dst-path=”$ftppath/$fname1″ upload=yes

# upload the config export

:log info message=”Uploading config export (2/2).”

/tool fetch address=”$ftphost” src-path=$fname2 user=”$ftpuser” mode=ftp password=”$ftppassword” dst-path=”$ftppath/$fname2″ upload=yes

# delay time to finish the upload – increase it if your backup file is big

:delay 30s;

# find file name start with Mikrotik- then remove

:foreach i in=[/file find] do={ :if ([:typeof [:find [/file get $i name] “Mikrotik-“]]!=”nil”) do={/file remove $i}; }

:log info message=”Configuration backup finished.”;

А щоб вони були дійсно автоматичні, потрібно піти в System – Scheduler і створити дві задачі, де задаємо інтервал запуску скриптів, а в On Event вказуємо який саме скрипт і де він лежить. Наприклад: /system script run Auto_Backup

backup