Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /system script add comment="=SYS-ROS-BACKUP=" dont-require-permissions=no name=sys-ros-backup owner=admin policy=ftp,reboot,read,write,policy,test,sniff,sensitive source="# System Backup [ ver. 2.4.6 ]\
- \n# 1. Backup ROS configuration with multiple ways\
- \n# Require: ROS 6.44 or higher\
- \n# Links:\
- \n# https://wiki.mikrotik.com/wiki/Manual:Scripting\
- \n# https://wiki.mikrotik.com/wiki/Manual:Scripting_Tips_and_Tricks\
- \n# Shevchenko Dritry - 13.11.2019\
- \n\
- \n# 1. Global Variables\
- \n# 1.1 ROS update settings\
- \n# 1.1.1 Backup flag\
- \n:global m9eBackupROS;\
- \n:if ( [ :len \$m9eBackupROS ] = 0 ) do={ :set m9eBackupROS true }\
- \n# 1.1.2 Encryption password\
- \n:global m9eBackupPass;\
- \n# 1.1.3 Backup methods\
- \n:global m9eBackupAlso;\
- \n# 1.1.4 Email backup recipient\
- \n:global m9eBackupMail;\
- \n:local sendEmail true;\
- \n:local rcpntEmail;\
- \n:if ( [ :len \$m9eBackupMail ] = 0 ) do={\
- \n\t/system script environment remove m9eBackupMail ;\
- \n\t:if ( :len [ /snmp get contact ] != 0 ) do={ :set rcpntEmail [ /snmp get contact ] ; } else={ :set sendEmail false } ;\
- \n} else={\
- \n\t:set rcpntEmail [ /system script environment get m9eBackupMail value-name=value ];\
- \n};\
- \n# 1.1.5 FTP user and host\
- \n:global m9eBackupUser;\
- \n:if ( [ :len \$m9eBackupUser ] = 0 ) do={ :set m9eBackupUser anonymous }\
- \n:global m9eBackupFTP;\
- \n# 1.2 List of functions\
- \n# 1.2.1 Loading functions\
- \n:do { /system script run ros-functions } on-error={ and\
- \n\t:log error ( \"sys-ros-backup: can't load necessery functions\" );\
- \n\t:error \"[ERR]: can't load necessery functions - inconsistent list of scrips\" ;\
- \n}\
- \n# 1.2.2 Fuctions declaration\
- \n:global funcIsFlash;\
- \n:global funcCmd2Script;\
- \n:global funcROSVersionCompare;\
- \n# 2. Local Variables\
- \n# 2.1 ROS firmware versions\
- \n:local roscurrentfmw [ /system resource get version ];\
- \n:if ( \$roscurrentfmw ~\"^[0-9]+\\\\.[0-9]+\\\\.[0-9]+.*\\\\(.*\\\\)\\\$\" ) do={\
- \n\t:set roscurrentfmw [ :pick \$roscurrentfmw 0 [ :find \$roscurrentfmw \" \" ] ];\
- \n}\
- \n:local roscurmajver [ :pick \$roscurrentfmw 0 [ :find \$roscurrentfmw \".\" ] ];\
- \n# 2.2 ROS architecture\
- \n:local rbarch [ /system resource get architecture-name ];\
- \n# 2.3 ROS identity\
- \n:local rosid [ /system identity get name ];\
- \n# 2.4 Default filepaths\
- \n:local bckpBinFname ( \"pub/\" . \$rosid . \"-\" . \$rbarch . \"-\" . \$roscurrentfmw . \".backup\" );\
- \nif ( [ \$funcIsFlash ] ) do={ :set bckpBinFname ( \"flash/\" . \$bckpBinFname ); }\
- \n:local bckpRscFname ( \"pub/\" . \$rosid . \"-\" . \$rbarch . \"-\" . \$roscurrentfmw . \".rsc\" );\
- \n:if ( [ \$funcIsFlash ] ) do={ :set bckpRscFname ( \"flash/\" . \$bckpRscFname ); }\
- \n# 2.5 Cloud no-backup flag (if \$m9eBackupPass not set)\
- \n# Link: https://wiki.mikrotik.com/wiki/Manual:IP/Cloud#Backup\
- \n:local uploadCloud true;\
- \n# 2.4 Verbose output\
- \n:local VERBOSE false;\
- \n\
- \n# 3. Main\
- \n# 3.0 Version check\
- \n:if ( [ \$funcROSVersionCompare versionA=\$roscurrentfmw versionB=\"6.42\" ] < 0 ) do={\
- \n\t:log error (\"sys-ros-backup: ros version is less than minimal required\");\
- \n\t:if ( \$VERBOSE ) do={ :error \"[ERR]: ROS version is less than minimal required.\" }\
- \n}\
- \n# 3.1 Local backup\
- \n:if ( \$m9eBackupROS ) do={\
- \n\t:if ( \$VERBOSE ) do={ :put \"[MSG]: Backup router configuration\" };\
- \n\t:log info ( \"sys-ros-backup: binary backup current configuration\" );\
- \n\t:if ( [ :len \$m9eBackupPass ] = 0 ) do={\
- \n\t\t:log warn ( \"sys-ros-backup: encryption password is not set!\" );\
- \n\t\t:set uploadCloud false;\
- \n\t\t:do { /system backup save dont-encrypt=yes name=\$bckpBinFname } on-error={\
- \n\t\t\t:log error ( \"sys-ros-backup: binary backup is failed - storage problems\?\" );\
- \n\t\t\t:error \"[ERR]: Binary backup router configuration is failed\";\
- \n\t\t};\
- \n\t} else={\
- \n\t\t:do { /system backup save encryption=aes-sha256 password=\$m9eBackupPass name=\$bckpBinFname } on-error={\
- \n\t\t\t:log error ( \"sys-ros-backup: binary backup is failed - storage problems\?\" );\
- \n\t\t\t:error \"[ERR]: Binary backup router configuration is failed\";\
- \n\t\t};\
- \n\t};\
- \n\t:log info ( \"sys-ros-backup: rsc backup current configuration\" );\
- \n\t:do { export terse file=\$bckpRscFname; } on-error={\
- \n\t\t:log error ( \"sys-ros-backup: rsc backup is failed - storage problems\?\" );\
- \n\t\t:error \"[ERR]: Text backup router configuration is failed\";\
- \n\t\t\
- \n\t};\
- \n}\
- \n# 3.1 Other backup methods\
- \n# 3.1.1 No methods defined\
- \n:if ( [ :len \$m9eBackupAlso ] = 0 or [ :typeof \$m9eBackupAlso ] != \"str\" ) do={\
- \n\t:log info ( \"sys-ros-backup: additional backup methods wasn't defined or has incorrect type\" );\
- \n\t:do { /system script environment remove [ find name~\"func\" ] } on-error={\
- \n\t\t:log warning ( \"sys-ros-m9e: script environment cleanup is failed\" );\
- \n\t};\
- \n\t:error \"[WRN]: Additional backup methods wasn't defined or has incorrect type\"\
- \n}\
- \n# 3.1.2 Parsing methods\
- \nforeach method in=[ :toarray \$m9eBackupAlso ] do={\
- \n\t# 3.1.3 Cloud backup\
- \n\t# TODO: Should return secret-download-key to end-user.\
- \n\t:if ( method = \"cloud\" ) do={\
- \n\t\t:if ( \$uploadCloud ) do={\
- \n\t\t\t:if ( \$VERBOSE ) do={ :put \"[MSG]: Upload binary backup to cloud\" };\
- \n\t\t\t:log info ( \"sys-ros-backup: upload binary backup to cloud\" );\
- \n\t\t\t:do { \
- \n\t\t\t\t:if ( [ :len [ /system backup cloud find ] ] != 0 ) do={ /system backup cloud remove-file [ find ] ; } ; \
- \n\t\t\t\t/system backup cloud upload-file action=upload src-file=\$bckpBinFname } on-error={\
- \n\t\t\t\t:if ( \$VERBOSE ) do={ :put \"[ERR]: Cloud backup is failed - connectivity problems\?\" };\
- \n\t\t\t\t:log error ( \"sys-ros-backup: cloud backup is failed - connectivity problems\?\" );\
- \n\t\t\t};\
- \n\t\t} else={\
- \n\t\t\t# Encryption password was not set, so binary backup was created with dont-encrypt=yes.\
- \n\t\t\t# This kinds of backup files can't be uploaded to cloud.\
- \n\t\t\t# See official wiki-pages mentioned above.\
- \n\t\t\t:if ( \$VERBOSE ) do={ :put \"[ERR]: Cloud backup is failed - unencrypted file\" };\
- \n\t\t\t:log error ( \"sys-ros-backup: cloud backup is failed - unencrypted file\" );\
- \n\t\t};\
- \n\t};\
- \n\t# 3.1.4 Email backup (assume tool e-mail has valid settings)\
- \n\t:if ( method = \"email\" ) do={\
- \n\t\t:if ( \$sendEmail and \$rcpntEmail ~\"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\\\.[A-Za-z]{2,64}\" ) do={\
- \n\t\t\t:if ( \$VERBOSE ) do={ :put \"[MSG]: Send all backup files via e-mail\" };\
- \n\t\t\tlog info ( \"sys-ros-backup: send all backup files via e-mail\" );\
- \n\t\t\t:local sbjctEmail ( \"[Backup]: Automated backup (\" . [ :pick \$rosid 0 [ :find \$rosid \".\" ] ] . \")\" ) ;\
- \n\t\t\t:local rprtEmailHdr \"Hello \$rcpntEmail,\\nScheduled backup was executed successfully, see attached files.\\n\\n\" ;\
- \n\t\t\t:local rprtEmailId ( \"ID:\" . \$rosid . \"\\n\" )\
- \n\t\t\t:local rprtEmailBin ( \"BIN:\" . [ :pick \$bckpBinFname ( [ find \$bckpBinFname \"pub/\" ] + 4 ) [ :len \$bckpBinFname ]] . \" - \" . [ /file get value-name=creation-time \$bckpBinFname ] . \"\\n\" ) ;\
- \n\t\t\t:local rprtEmailRsc ( \"RSC:\" . [ :pick \$bckpRscFname ( [ find \$bckpRscFname \"pub/\" ] + 4 ) [ :len \$bckpRscFname ]] . \" - \" . [ /file get value-name=creation-time \$bckpRscFname ] . \"\\n\" ) ;\
- \n\t\t\t:local rprtEmailFtr \"\\nGoodbye and have a nice day.\"\
- \n\t\t\t:local bodyEmail ( \$rprtEmailHdr . \$rprtEmailId . \$rprtEmailBin . \$rprtEmailRsc . \$rprtEmailFtr ) ;\
- \n\t\t\t:local attchEmail [ :toarray ( \$bckpBinFname . \",\" . \$bckpRscFname ) ];\
- \n\t\t\t:do { /tool e-mail send subject=\"\$sbjctEmail\" body=\"\$bodyEmail\" to=\"\$rcpntEmail\" file=\$attchEmail } on-error={\
- \n\t\t\t\t:log error ( \"sys-ros-backup: email backup is failed - send errors\" );\
- \n\t\t\t} \
- \n\t\t} else={\
- \n\t\t\t:if ( \$VERBOSE ) do={ :put \"[ERR]: Email backup is failed - invalid recipient\" };\
- \n\t\t\t:log error ( \"sys-ros-backup: email backup is failed - invalid recipient\" );\
- \n\t\t};\
- \n\t};\
- \n\t# 3.1.5 FTP backup\
- \n\tif ( method = \"ftp\" ) do={\
- \n\t\t# FTP-variable may be a hostname or IP address\
- \n\t\t:if ( [ :len \$m9eBackupFTP ] != 0 ) do={ \
- \n\t\t\t:if ( \$VERBOSE ) do={ :put \"[MSG]: Copy all backup files to remote ftp-server\" };\
- \n\t\t\tlog info ( \"sys-ros-backup: copy all backup files to remote ftp-server\" );\
- \n\t\t\t:local bnameBinFname [ :pick \$bckpBinFname ( [ find \$bckpBinFname \"pub/\" ] + 4 ) [ :len \$bckpBinFname ]] ;\
- \n\t\t\t:local bnameRscFname [ :pick \$bckpRscFname ( [ find \$bckpRscFname \"pub/\" ] + 4 ) [ :len \$bckpRscFname ]] ;\
- \n\t\t\t:do {\
- \n\t\t\t\t/tool fetch mode=ftp upload=yes user=\$m9eBackupUser password=\$m9eBackupPass address=\$m9eBackupFTP src-path=\"\$bckpBinFname\" dst-path=\"\$rosid/\$bnameBinFname\" ;\
- \n\t\t\t\t/tool fetch mode=ftp upload=yes user=\$m9eBackupUser password=\$m9eBackupPass address=\$m9eBackupFTP src-path=\"\$bckpRscFname\" dst-path=\"\$rosid/\$bnameRscFname\" ;\
- \n\t\t\t} on-error={\
- \n\t\t\t\t:if ( \$VERBOSE ) do={ :put \"[ERR]: FTP backup is failed - fetch errors\" };\
- \n\t\t\t\t:log error ( \"sys-ros-backup: ftp backup is failed - fetch errors\" );\
- \n\t\t\t};\
- \n\t\t};\
- \n\t}; \
- \n};\
- \n# 3.2 Clean-up environment \
- \n:do { /system script environment remove [ find name~\"func\" ] } on-error={\
- \n\t:log warning ( \"sys-ros-backup: script environment cleanup is failed\" );\
- \n};\
- \n# 4.0 End"
- /system scheduler add comment="System backup" interval=1d name=sys-backup on-event=sys-ros-backup policy=ftp,reboot,read,write,policy,test,sniff,sensitive start-date=sep/10/2019 start-time=00:00:00
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement