master
Dave S. 2 years ago
commit 862195b01b
  1. 5
      README.md
  2. 64
      backup.rsc

@ -0,0 +1,5 @@
# Mikrotik backup script
## Installation
`/system script add dont-require-permissions=no name=backup policy=ftp,read,write,policy,test,sensitive source=...`

@ -0,0 +1,64 @@
:local rcpt "admin@example.org"
:local id [/system identity get name]
:local ts [/system clock get time]
:local ds [/system clock get date]
:set ts ([:pick $ts 0 2]."-".[:pick $ts 3 5]."-".[:pick $ts 6 8])
:set ds ([:pick $ds 4 6]."-".[:pick $ds 0 3]."-".[:pick $ds 7 11])
:local bn ("backup_".$id."_".$ds."_".$ts)
:local bn1 ($bn.".backup")
:local bn2 ($bn.".rsc")
:local bns {$bn1;$bn2}
:local bnl ([:len $bns])
:local subj ("MT Backup from ".$id." - ".$ds." ".$ts)
:local body ("This is a scheduled backup from MT device \"".$id."\". \r\nDate: ".$ds." \r\nTime: ".$ts." \r\n\r\n".$bnl." file(s) attached.")
:log info "Preparing backups..."
:if ([:len [/tool e-mail get address]] = 0 or [:len [/tool e-mail get from]] = 0) do={
:log error "Email configuration is missing"
:error "Email configuration is missing"
} else={
:local fail false
:do {
/system backup save dont-encrypt=yes name=$bn
/export compact file=$bn
:delay 4s
:log info "Sending backup email..."
:do {/tool e-mail send to=$rcpt subject=$subj body=$body file=$bns} on-error={
:log warning "Failed to send email message. Retrying in 30s..."
:delay 30s
:do {/tool e-mail send to=$rcpt subject=$subj body=$body file=$bns} on-error={
:log error "Failed to send email message (2nd attempt)"
:set fail true
}
}
:delay 20s
:log info "Cleaning up backups..."
/file remove $bns
} on-error={
:log warning "Cleaning up backups after a caught exception..."
/file remove $bns
:set fail true
}
:if ($fail = false) do={
:log info "Backup script completed"
} else={
:log error "Backup script completed with errors"
:error "Backup script completed with errors"
}
}
Loading…
Cancel
Save