Skip to content

Instantly share code, notes, and snippets.

@sebastian13
Last active June 24, 2024 23:34
Show Gist options
  • Save sebastian13/3b05cbbbdd3326a402391e81e1a8f7af to your computer and use it in GitHub Desktop.
Save sebastian13/3b05cbbbdd3326a402391e81e1a8f7af to your computer and use it in GitHub Desktop.

Revisions

  1. sebastian13 revised this gist Jul 8, 2022. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions $mikrotik-backup-and-upgrade.md
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ set address=smtp.gmail.com [email protected] port=587 tls=starttls \

    ```
    /system scheduler
    add interval=1d name=upgrade-routeros on-event=upgrade-routeros start-time=04:00:00
    add interval=1d name=upgrade-firmware on-event=":delay 30s\nupgrade-firmware" start-time=startup
    add interval=4w name=backup2mail on-event=backup2mail start-time=06:00:00
    add name=upgrade-routeros on-event=upgrade-routeros start-time=04:00:00 interval=1d
    add name=upgrade-firmware on-event=":delay 30s\n/system script run upgrade-firmware" start-time=startup
    add name=backup2mail on-event=backup2mail start-time=06:00:00 interval=4w
    ```
  2. sebastian13 revised this gist Jul 8, 2022. 1 changed file with 11 additions and 9 deletions.
    20 changes: 11 additions & 9 deletions backup2mail.rsc
    Original file line number Diff line number Diff line change
    @@ -3,24 +3,26 @@
    :local BackupPassword "___";
    :global Identity [ / system identity get name ];

    :log info message="Sending Export Compact to $recipient"
    :log info message="Creating a cloud backup and sending export compact to $recipient"

    # Export Compact
    :put "Create export compact"
    /export compact file=export-compact
    :delay 10s;

    # Create Cloud-Backup
    # Cloud-Backup
    :do {
    # we are not interested in output, but print without count-only is
    # required to fetch information from cloud
    / system backup cloud print as-value;
    :if ([ / system backup cloud print count-only ] > 0) do={
    / system backup cloud upload-file action=create-and-upload \
    :if ([ /system backup cloud print count-only ] > 0) do={
    :put "Create, upload and replace cloud backup."
    /system backup cloud upload-file action=create-and-upload \
    password=$BackupPassword replace=[ get ([ find ]->0) name ];
    } else={
    / system backup cloud upload-file action=create-and-upload \
    :put "Create a new cloud backup."
    /system backup cloud upload-file action=create-and-upload \
    password=$BackupPassword;
    }
    } on-error={
    :log error ("Failed uploading backup for " . $Identity . " to cloud.");
    :error "Cloud Backup failed for $Identity failed."
    }

    # Get latest Cloud-Backup Details
  3. sebastian13 revised this gist Jul 8, 2022. 1 changed file with 3 additions and 8 deletions.
    11 changes: 3 additions & 8 deletions $mikrotik-backup-and-upgrade.md
    Original file line number Diff line number Diff line change
    @@ -14,12 +14,7 @@ set address=smtp.gmail.com [email protected] port=587 tls=starttls \

    ```
    /system scheduler
    add interval=1d name=upgrade-routeros on-event=upgrade-routeros policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-date=jan/01/2019 start-time=\
    04:00:00
    add interval=1d name=upgrade-firmware on-event=upgrade-firmware policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-time=startup
    add interval=4w2d name=backup2mail on-event=backup2mail policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-date=dec/10/2019 start-time=\
    08:00:00
    add interval=1d name=upgrade-routeros on-event=upgrade-routeros start-time=04:00:00
    add interval=1d name=upgrade-firmware on-event=":delay 30s\nupgrade-firmware" start-time=startup
    add interval=4w name=backup2mail on-event=backup2mail start-time=06:00:00
    ```
  4. sebastian13 renamed this gist Jul 8, 2022. 1 changed file with 0 additions and 0 deletions.
  5. sebastian13 revised this gist Jul 8, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions $backup-upgrade_routeros.md
    Original file line number Diff line number Diff line change
    @@ -4,8 +4,8 @@

    ```
    /tool e-mail
    set address=smtp.gmail.com [email protected] port=587 tls=starttls user=\
    [email protected]
    set address=smtp.gmail.com [email protected] port=587 tls=starttls \
    user=[email protected] password=___
    ```

    2. Create the 3 scripts, named **backup2mail**, **upgrade-firmware**, **upgrade-routeros**
  6. sebastian13 revised this gist Jul 8, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion $backup-upgrade_routeros.md
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ set address=smtp.gmail.com [email protected] port=587 tls=starttls user=\
    [email protected]
    ```

    2. Create the 3 scripts, named backup2mail, upgrade-firmware, upgrade-routeros
    2. Create the 3 scripts, named **backup2mail**, **upgrade-firmware**, **upgrade-routeros**

    3. Create schedulers

  7. sebastian13 revised this gist Jul 8, 2022. 1 changed file with 25 additions and 1 deletion.
    26 changes: 25 additions & 1 deletion $backup-upgrade_routeros.md
    Original file line number Diff line number Diff line change
    @@ -1 +1,25 @@
    # Backup & Auto Upgrade Mikrotik Routers
    ## Backup & Auto Upgrade Mikrotik Routers

    1. Set SMTP

    ```
    /tool e-mail
    set address=smtp.gmail.com [email protected] port=587 tls=starttls user=\
    [email protected]
    ```

    2. Create the 3 scripts, named backup2mail, upgrade-firmware, upgrade-routeros

    3. Create schedulers

    ```
    /system scheduler
    add interval=1d name=upgrade-routeros on-event=upgrade-routeros policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-date=jan/01/2019 start-time=\
    04:00:00
    add interval=1d name=upgrade-firmware on-event=upgrade-firmware policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-time=startup
    add interval=4w2d name=backup2mail on-event=backup2mail policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-date=dec/10/2019 start-time=\
    08:00:00
    ```
  8. sebastian13 renamed this gist Jul 8, 2022. 1 changed file with 0 additions and 0 deletions.
  9. sebastian13 revised this gist Jul 8, 2022. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions backup_upgrade_routeros.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    # Backup & Auto Upgrade Mikrotik Routers
  10. sebastian13 revised this gist Jul 8, 2022. No changes.
  11. sebastian13 created this gist Jul 8, 2022.
    39 changes: 39 additions & 0 deletions backup2mail.rsc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    :local recipient
    :set recipient "__@__.com"
    :local BackupPassword "___";
    :global Identity [ / system identity get name ];

    :log info message="Sending Export Compact to $recipient"
    /export compact file=export-compact
    :delay 10s;

    # Create Cloud-Backup
    :do {
    # we are not interested in output, but print without count-only is
    # required to fetch information from cloud
    / system backup cloud print as-value;
    :if ([ / system backup cloud print count-only ] > 0) do={
    / system backup cloud upload-file action=create-and-upload \
    password=$BackupPassword replace=[ get ([ find ]->0) name ];
    } else={
    / system backup cloud upload-file action=create-and-upload \
    password=$BackupPassword;
    }
    } on-error={
    :log error ("Failed uploading backup for " . $Identity . " to cloud.");
    }

    # Get latest Cloud-Backup Details
    :local backupn [/system backup cloud get 0 name ];
    :local backupk [/system backup cloud get 0 secret-download-key ];
    :local backupd [/system backup cloud get 0 date ];
    :local backupr [/system backup cloud get 0 ros-version ];
    :local backups [/system backup cloud get 0 size ];

    # Send E-Mail
    /tool e-mail send to=$recipient \
    subject="Export-Compact | $Identity" \
    body="A Cloud-Backup was created for $Identity. A compact export is attached.\n\nName: $backupn \nDate: $backupd \nRouterOS: $backupr \nSize: $backups \nDownload key: $backupk \n" \
    file=export-compact.rsc
    :delay 10s;
    /file remove export-compact
    11 changes: 11 additions & 0 deletions upgrade-firmware.rsc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    :log info "Checking firmware...";
    /system routerboard
    :if ([get current-firmware] != [get upgrade-firmware]) do={
    :log info "Updating firmware";
    upgrade;
    # Automatic restart
    :delay 10s
    /system reboot
    } else={
    :log info "No firmware update needed."
    }
    10 changes: 10 additions & 0 deletions upgrade-routeros.rsc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    :log info message="Checking for RouterOS Updates"

    /system package update
    check-for-updates once
    :delay 5s;

    :if ( [get status] = "New version is available") do={
    /system script run backup2mail
    /system package update install
    }