Community discussions

MikroTik App
 
swits1109
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 81
Joined: Sat Sep 10, 2016 6:03 pm

Automatic backup and email script

Fri Dec 09, 2022 12:04 am

I have this script which was working in the past, but seems to no longer work. It is supposed to create a backup and then email it, but it doesn't seem to send the emails. Can anyone see the problem? (passwords removed)
/tool e-mail
set address=email-smtp.us-west-2.amazonaws.com from=mikrotik@xxx.com password=xxx \
    port=587 start-tls=tls-only user=xxxx

/system scheduler
add interval=1w name="Upgrade Router" on-event="/system package update set chann\
    el=stable; /system package update check-for-updates; /system package update\
    \_download; /system reboot;" policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \
    start-date=jan/01/2018 start-time=04:00:00
add interval=24h name=Backup on-event=":local emailAddress \"mikrotik.o8507@xxxx.c\
    om\"\r\
    \n:log info \"BACKUP STARTING NOW\"\r\
    \n:global backupfile ([/system identity get name] . \"-\" . [/system clock g\
    et time])\r\
    \n/system backup save name=\$backupfile\r\
    \n:log info \"BACKUP FILE SAVED, SENDING EMAIL IN 2 SECONDS\"\r\
    \n:delay 2s\r\
    \n/tool e-mail send to=\"\$emailAddress\" subject=([/system identity get nam\
    e] . \" Backup \" . [/system clock get date] . \" \" . [/system clock get ti\
    me]) file=\$backupfile\r\
    \n:delay 15s\r\
    \n:log info \"BACKUP EMAIL SENT, REMOVING BACKUP FILE\"\r\
    \n/file remove \$backupfile\r\
    \n:log info \"BACKUP FINISHED\"" policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \
    start-date=apr/05/2019 start-time=15:25:22
Last edited by BartoszP on Fri Dec 09, 2022 12:20 am, edited 1 time in total.
Reason: use proper tags for quotting, code etc.
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Automatic backup and email script

Fri Dec 09, 2022 12:25 am

1. Post the the script as copy past from the gui. Its hard to understand and debug script like this.
2. Never ever auto upgrade your router. Only upgrade if:
..a. you need to upgrade. (new function, serious bug fix etc)
..b. read the forum over and over
..c. wait at least some week after a new release before upgrade.
..d. the read forum again to see if there are any show stoppers
..e. upgrade on an equal router with same software as prod ruter. test if all works fine
..f. make a backup
..g. then you may upgrade if you need.

Example on script posting.
:local emailAddress "mikrotik.o8507@xxxx.com"
:log info "BACKUP STARTING NOW"
:global backupfile ([/system identity get name] . "-" . [/system clock get time])
/system backup save name=$backupfile
:log info "BACKUP FILE SAVED, SENDING EMAIL IN 2 SECONDS"
:delay 2s
/tool e-mail send to="$emailAddress" subject=([/system identity get name] . " Backup " . [/system clock get date] . " " . [/system clock get time]) file=$backupfile
:delay 15s
:log info "BACKUP EMAIL SENT, REMOVING BACKUP FILE"
/file remove $backupfile
:log info "BACKUP FINISHED"
Here is a working gmail backup scrript.
viewtopic.php?t=183631
Last edited by Jotne on Fri Dec 09, 2022 12:27 am, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Automatic backup and email script

Fri Dec 09, 2022 12:27 am

If nothing is touched and routeros is the same, probably mail server change requirements.
Check if you can send mail directly with the tool.
 
swits1109
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 81
Joined: Sat Sep 10, 2016 6:03 pm

Re: Automatic backup and email script

Fri Dec 09, 2022 12:49 am

This is a new router, an old script that I haven't used in some time. Yes, the manual email send works.
Last edited by BartoszP on Fri Dec 09, 2022 12:55 am, edited 1 time in total.
Reason: removed excessive quotting of preceding post; be wise, quote smart. lines of quote, 1 line of post.
 
swits1109
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 81
Joined: Sat Sep 10, 2016 6:03 pm

Re: Automatic backup and email script

Fri Dec 09, 2022 12:52 am

1. Post the the script as copy past from the gui. Its hard to understand and debug script like this.
2. Never ever auto upgrade your router. Only upgrade if:
..a. you need to upgrade. (new function, serious bug fix etc)
..b. read the forum over and over
..c. wait at least some week after a new release before upgrade.
..d. the read forum again to see if there are any show stoppers
..e. upgrade on an equal router with same software as prod ruter. test if all works fine
..f. make a backup
..g. then you may upgrade if you need.

Example on script posting.
:local emailAddress "mikrotik.o8507@xxxx.com"
:log info "BACKUP STARTING NOW"
:global backupfile ([/system identity get name] . "-" . [/system clock get time])
/system backup save name=$backupfile
:log info "BACKUP FILE SAVED, SENDING EMAIL IN 2 SECONDS"
:delay 2s
/tool e-mail send to="$emailAddress" subject=([/system identity get name] . " Backup " . [/system clock get date] . " " . [/system clock get time]) file=$backupfile
:delay 15s
:log info "BACKUP EMAIL SENT, REMOVING BACKUP FILE"
/file remove $backupfile
:log info "BACKUP FINISHED"
Here is a working gmail backup scrript.
viewtopic.php?t=183631
The gmail script seems to work well, just what I need. Do you have a version that can be pasted in the terminal that automatically creates the scheduler entry?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Automatic backup and email script

Fri Dec 09, 2022 1:04 am

@swits1109 do not fully quote post on useless way....

simply create the scheduler on winbox, paste the script, and export the scheduler on terminal...
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Automatic backup and email script

Fri Dec 09, 2022 1:41 am

This backup works very well for me in all versions of RouterOS.
:local sysname [/system identity get name]
:local textfilename
:local backupfilename
:local Version [/system resource get version]

/system clock
:local vtime [get time]
:local vdate [get date]
:local M ([:find "xxanebarprayunulugepctovecANEBARPRAYUNULUGEPCTOVEC" [:pick $vdate 1 3] -1] / 2); :if ($M>12) do={:set M ($M - 12)}
:local MM [:pick (100 + $M) 1 3]
# format DDMMYYYY-HHMMSS
:local mydatetime ( [:pick $vdate 4 6].$MM.[:pick $vdate 7 11]."-".[:pick $vtime 0 2].[:pick $vtime 3 5].[:pick $vtime 6 8] )

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

# backup action
:if ($Version~"^7") do={
:execute [/export file=$"textfilename" show-sensitive]
} else={
:execute [/export file=$"textfilename"]
}
:execute [/system backup save name=$"backupfilename"]

# START Send Email .RSC
/tool e-mail send to="<your-email>" subject="[Backup] $sysname $mydatetime" body="Backup *.rsc OK" file="$textfilename";
# END Send Email .RSC

:delay 2s

# START Send Email .BACKUP
/tool e-mail send to="<your-email>" subject="[Backup] $sysname $mydatetime" body="Backup *.backup OK" file="$backupfilename";
# END Send Email .BACKUP

# Remove local backups
:delay 10s
/file remove $textfilename
/file remove $backupfilename

:log info "Backups OK"
BR.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Automatic backup and email script

Fri Dec 09, 2022 11:20 am

This backup works very well for me in all versions of RouterOS.
[...]
I suggest you to change the format from
DDMMYYYY-HHMMSS
to
YYYYMMDD-HHMMSS
On this way the files are sortable for names... ;)
(yes, I know also are sortable for dates....)
 
User avatar
diamuxin
Member
Member
Posts: 319
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Automatic backup and email script

Fri Dec 09, 2022 11:54 am

OK, got it.

BR.

Who is online

Users browsing this forum: No registered users and 17 guests