Gmail + Google Drive + MikroTik scripting = Automated Backups Folder

I was recently looking for a simpler alternative to something like RANCID to periodically backup all our MikroTik configs.

RANCID is great and all, but I didn’t really need the diff copies of each file, and I was plenty happy just knowing that I have a .backup and .rsc file for each router I manage.

Given I use Gmail it might have been ok just to have each router send them there and leave it at that, but I’m not really the sort of person to half-ass my afterhours projects.. I learnt from Greg that you have to just whole-ass them ๐Ÿ™‚

So, there’s plenty of ways to have a MikroTik router backup and send a copy of it’s config to an email account, but what I really wanted to do was have them automagically move into a Google Drive folder of my choosing. Why Google Drive? Well I already use GMail so I have 30GB of hosted space I can keep everything together in, so why wouldn’t I?! This also sync between my Desktop and Laptop so I’ll always have copies of the backups with me.

I searched around and finally stumbled across this post about ย a script called “Send to Google Drive” that can run in the background on your GMail account all the time.

Perfect!

So to start with I created a script to export, send and then remove config files from the local router (saved as backup-and-email):


#### Modify these values to match your requirements ####

#Your email address to receive the backups
:local toemail "you@example.com"

#The From address (you can use your own address if you want)
:local fromemail "mikrotik@example.com"

#A mail server your machines can send through
:local emailserver "smtp.example.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 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 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 to=$"toemail" from=$"fromemail" server=[:resolve $emailserver] port=25 subject="[Config Backup] $sysname $time" file=$"textfilename"
#Send as different subjects to force GMail to treat as new message thread.
:local time [/system clock get time]
/tool e-mail send to=$"toemail" from=$"fromemail" server=[:resolve $emailserver] port=25 subject="[Config Backup] $sysname $time" file=$"backupfilename"

#Allow time to send
:delay 10s

#delete copies
/file remove $textfilename
/file remove $backupfilename

Next I logged into my gmail account and created a label called “RBackup” and another called “RBackup Saved” for the files to flow into, then logged into Google Drive and created a new folder in the root directory called “Router Backups”

Then as instructed I copied the spreadsheet provided and set the values as follows:

stgd

Then followed the 2 step procedure to authorise and run the app.

Next I created the filter in GMail for all emails from secretrouteremail@example.com destined to myemailaddress@example.com where subject contained “[Config Backup]” and move it into the newly created “RBackup” folder/label.

Any emails that get moved here will be processed the by script, have the “RBackupSaved” label appended to them and attachments moved into the predetermined “Router Backups” folder I created in Google Drive.

To test I manually ran the script on my router and waited; it takes about a minute sometimes for the script to see the files, but the end result is that each night a new copy of all my router backups come flying into my Google Drive folder ready for any emergencies.

Next step is to ensure there’s a scheduler entry to have the script run each night, week, month or however often you’d like your backups.

/system scheduler
add interval=1d name=daily-backup on-event="/system script run backup-and-email" policy=ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api start-date=jan/01/2014 start-time=04:00:00

Big thanks to the fellow at Labnol.org who made his script freely available, hopefully mine is of use to some of you as well!

The finished product:

router-backups

12 thoughts on “Gmail + Google Drive + MikroTik scripting = Automated Backups Folder

  1. Hi, i followed all your guidelines.
    But it doesn’t work.
    It seems the device doesn’t send the email.
    thank you

  2. This is the working configuration with Google mail accounts.

    Here is the script

    #### Modify these values to match your requirements ####

    #Your email address to receive the backups
    :local toemail “info@gmailaccount”

    #The From address (you can use your own address if you want)
    :local fromemail “info@gmailaccount”

    #A mail server your machines can send through
    :local emailserver “173.194.65.16”

    #A mail Username
    :local pass “accountpassword”

    ############## 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 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 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 “Invio per Email i backups”
    /tool e-mail send to=$”toemail” from=$”fromemail” server=[:resolve $emailserver] port=25 user=$”fromemail” pass=$”pass” subject=”[Backup Configurazione] $sysname $time” file=$”textfilename”
    #Send as different subjects to force GMail to treat as new message thread.
    :local time [/system clock get time]
    /tool e-mail send to=$”toemail” from=$”fromemail” server=[:resolve $emailserver] port=25 user=$”fromemail” pass=$”pass” subject=”[Backup Configurazione] $sysname $time” file=$”backupfilename”

    #Allow time to send
    :delay 10s

    #delete copies
    /file remove $textfilename
    /file remove $backupfilename

Leave a Reply