From ba16b700137e5bd7ba661ecccaab5352b3818ab6 Mon Sep 17 00:00:00 2001 From: Pothi Kalimuthu <1254302+pothi@users.noreply.github.com> Date: Tue, 8 Nov 2022 20:58:08 +0530 Subject: [PATCH] Improve backup scripts! --- scripts/backup-cron.rsc | 38 ++++++++++++++++++++++++++---------- scripts/backup-scripts.rsc | 40 ++++++++++++++++++++++++++++---------- scripts/cloud-backup.rsc | 22 ++++++++++----------- 3 files changed, 69 insertions(+), 31 deletions(-) diff --git a/scripts/backup-cron.rsc b/scripts/backup-cron.rsc index 2526b92..b03f533 100644 --- a/scripts/backup-cron.rsc +++ b/scripts/backup-cron.rsc @@ -3,18 +3,36 @@ # filename: backup-cron # policy: ftp, read, write, policy, test -:local adminEmailAddress pothi@duck.com -:local deviceIdentity [/system identity get name] +:global adminEmail +:if ([:typeof $adminEmail] = "nothing" || $adminEmail = "") do={ + :log error "adminEmail is not defined or nil."; :error "Error: Check the log"; } -:log info "Executing the script \"backup-cron\"..." +:local backupName "cron" -/system scheduler export file=cron; :delay 3s +:local fileName "$backupName.rsc" +:local emailStatus -/tool e-mail send to="$adminEmailAddress" \ - subject="[Mikrotik $deviceIdentity] Backup of Cron Entries" \ - body="See the subject and the attachment." \ - file=cron.rsc; :delay 10s +:log info "\nCreating a $backupName backup..." -:log info "An email is probably sent to $adminEmailAddress." +# remove existing file, if exists for unknown reason +/file remove [find name=$fileName]; :delay 1s -/file remove cron.rsc +# create a backup of cron entries +/system scheduler export file=$fileName +:delay 3s + +/tool e-mail + +:do { send to="$adminEmail" subject="Backup of $backupName" \ + body="See the subject and the attachment." file=$fileName + } on-error={ :log error "Error sending email." } + +:do { :delay 5s; :set emailStatus [get last-status] } while=( $emailStatus = "in-progress" ) + +:if ( $emailStatus = "failed" ) do={ + :log error "Backup failed!" +} else={ + :log info "Backup is taken and is sent to $adminEmail." +} + +/file remove $fileName diff --git a/scripts/backup-scripts.rsc b/scripts/backup-scripts.rsc index e963d9e..8c8f1ad 100644 --- a/scripts/backup-scripts.rsc +++ b/scripts/backup-scripts.rsc @@ -3,18 +3,38 @@ # filename: backup-scripts # policy: ftp, read, write, policy, test -:local adminEmailAddress "admin@example.com" -:local deviceIdentity [/system identity get name] +:global adminEmail +:if ([:typeof $adminEmail] = "nothing" || $adminEmail = "") do={ + :log error "adminEmail is not defined or nil."; :error "Error: Check the log"; } -:log info "Executing the script \"backup-scripts\"..." +:local backupName "scripts" +:local fileName "$backupName.rsc" -/system script export file=scripts; :delay 3s +:local emailStatus -/tool e-mail send to="$adminEmailAddress" \ - subject="[Mikrotik $deviceIdentity] Backup of Scripts" \ - body="See the subject and the attachment." \ - file=scripts.rsc; :delay 10s +:log info "\nCreating a $backupName backup..." -:log info "An email is probably sent to $adminEmailAddress." +# remove existing file, if exists for unknown reason +/file remove [find name=$fileName]; :delay 3s -/file remove scripts.rsc +# take a backup +/system script + export file=$fileName +:delay 3s + +/tool e-mail + +:do { send to="$adminEmail" subject="Backup of $backupName" \ + body="See the subject and the attachment." file=$fileName + } on-error={ :log error "Error sending email." } + +:do { :delay 5s; :set emailStatus [get last-status] } while=( $emailStatus = "in-progress" ) + +:if ( $emailStatus = "failed" ) do={ + :log error "Backup failed!" +} else={ + :log info "Backup is taken and is sent to $adminEmail." +} + +# Optional +:delay 5s; /file remove $fileName diff --git a/scripts/cloud-backup.rsc b/scripts/cloud-backup.rsc index a4c2a1b..9fc56e9 100644 --- a/scripts/cloud-backup.rsc +++ b/scripts/cloud-backup.rsc @@ -1,13 +1,13 @@ # Cloud Backup # requirement/s: -:global cloudBackupPass +:global cloudPass +:if ([:typeof $cloudPass] = "nothing" || $cloudPass = "") do={ + :log error "cloudPass is not defined or nil."; :error "Error: Check the log"; } # permissions required: ftp, read, write, policy, test -:local backupName "Cloud Backup" - -:log info "\nRunning the script \"cloud-backup\"..." +:log info "\nCreating a new cloud backup..." /system backup cloud @@ -16,15 +16,15 @@ remove-file number=0 :delay 3s - :log info "Existing $backupName is removed to create-and-upload a new backup." + :log info " Existing cloud backup is removed to create space for a new backup." -} else={ :log info "No existing $backupName found."; } +} else={ :log info " No existing cloud backup found."; } -:log info "Creating a new $backupName..." +:log info " A new cloud backup is being created..." - upload-file action=create-and-upload password=$cloudBackupPass - :delay 10s + upload-file action=create-and-upload password=$cloudPass + :delay 30s :if ( ([:pick [print as-value] 0]->"status") = "ok" ) do={ - :log info "$backupName is successful." -} else={ :log error "$backupName failed!" } + :log info "Cloud backup is successful." +} else={ :log error "Cloud backup failed!" }