Advertisement
sa_drug

[MT]: Scripts - sys-ros-backup

Nov 21st, 2019
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.17 KB | None | 0 0
  1. /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 ]\
  2.    \n# 1. Backup ROS configuration with multiple ways\
  3.    \n# Require: ROS 6.44 or higher\
  4.    \n# Links:\
  5.    \n# https://wiki.mikrotik.com/wiki/Manual:Scripting\
  6.    \n# https://wiki.mikrotik.com/wiki/Manual:Scripting_Tips_and_Tricks\
  7.    \n# Shevchenko Dritry - 13.11.2019\
  8.    \n\
  9.    \n# 1. Global Variables\
  10.    \n# 1.1 ROS update settings\
  11.    \n# 1.1.1 Backup flag\
  12.    \n:global m9eBackupROS;\
  13.    \n:if ( [ :len \$m9eBackupROS ] = 0 ) do={ :set m9eBackupROS true }\
  14.    \n# 1.1.2 Encryption password\
  15.    \n:global m9eBackupPass;\
  16.    \n# 1.1.3 Backup methods\
  17.    \n:global m9eBackupAlso;\
  18.    \n# 1.1.4 Email backup recipient\
  19.    \n:global m9eBackupMail;\
  20.    \n:local sendEmail true;\
  21.    \n:local rcpntEmail;\
  22.    \n:if ( [ :len \$m9eBackupMail ] = 0 ) do={\
  23.    \n\t/system script environment remove m9eBackupMail ;\
  24.    \n\t:if ( :len [ /snmp get contact ] != 0 ) do={ :set rcpntEmail [ /snmp get contact ] ; } else={ :set sendEmail false } ;\
  25.    \n} else={\
  26.    \n\t:set rcpntEmail [ /system script environment get m9eBackupMail value-name=value ];\
  27.    \n};\
  28.    \n# 1.1.5 FTP user and host\
  29.    \n:global m9eBackupUser;\
  30.    \n:if ( [ :len \$m9eBackupUser ] = 0 ) do={ :set m9eBackupUser anonymous }\
  31.    \n:global m9eBackupFTP;\
  32.    \n# 1.2 List of functions\
  33.    \n# 1.2.1 Loading functions\
  34.    \n:do { /system script run ros-functions } on-error={ and\
  35.    \n\t:log error ( \"sys-ros-backup: can't load necessery functions\" );\
  36.    \n\t:error \"[ERR]: can't load necessery functions - inconsistent list of scrips\" ;\
  37.    \n}\
  38.    \n# 1.2.2 Fuctions declaration\
  39.    \n:global funcIsFlash;\
  40.    \n:global funcCmd2Script;\
  41.    \n:global funcROSVersionCompare;\
  42.    \n# 2. Local Variables\
  43.    \n# 2.1 ROS firmware versions\
  44.    \n:local roscurrentfmw [ /system resource get version ];\
  45.    \n:if ( \$roscurrentfmw ~\"^[0-9]+\\\\.[0-9]+\\\\.[0-9]+.*\\\\(.*\\\\)\\\$\" ) do={\
  46.    \n\t:set roscurrentfmw [ :pick \$roscurrentfmw 0 [ :find \$roscurrentfmw \" \" ] ];\
  47.    \n}\
  48.    \n:local roscurmajver [ :pick \$roscurrentfmw 0 [ :find \$roscurrentfmw \".\" ] ];\
  49.    \n# 2.2 ROS architecture\
  50.    \n:local rbarch [ /system resource get architecture-name ];\
  51.    \n# 2.3 ROS identity\
  52.    \n:local rosid [ /system identity get name ];\
  53.    \n# 2.4 Default filepaths\
  54.    \n:local bckpBinFname ( \"pub/\" . \$rosid . \"-\" . \$rbarch . \"-\" . \$roscurrentfmw . \".backup\" );\
  55.    \nif ( [ \$funcIsFlash ] ) do={ :set bckpBinFname ( \"flash/\" . \$bckpBinFname ); }\
  56.    \n:local bckpRscFname ( \"pub/\" . \$rosid . \"-\" . \$rbarch . \"-\" . \$roscurrentfmw . \".rsc\" );\
  57.    \n:if ( [ \$funcIsFlash ] ) do={ :set bckpRscFname ( \"flash/\" . \$bckpRscFname ); }\
  58.    \n# 2.5 Cloud no-backup flag (if \$m9eBackupPass not set)\
  59.    \n# Link: https://wiki.mikrotik.com/wiki/Manual:IP/Cloud#Backup\
  60.    \n:local uploadCloud true;\
  61.    \n# 2.4 Verbose output\
  62.    \n:local VERBOSE false;\
  63.    \n\
  64.    \n# 3. Main\
  65.    \n# 3.0 Version check\
  66.    \n:if ( [ \$funcROSVersionCompare versionA=\$roscurrentfmw versionB=\"6.42\" ] < 0 ) do={\
  67.    \n\t:log error (\"sys-ros-backup: ros version is less than minimal required\");\
  68.    \n\t:if ( \$VERBOSE ) do={ :error \"[ERR]: ROS version is less than minimal required.\" }\
  69.    \n}\
  70.    \n# 3.1 Local backup\
  71.    \n:if ( \$m9eBackupROS ) do={\
  72.    \n\t:if ( \$VERBOSE ) do={ :put \"[MSG]: Backup router configuration\" };\
  73.    \n\t:log info ( \"sys-ros-backup: binary backup current configuration\" );\
  74.    \n\t:if ( [ :len \$m9eBackupPass ] = 0 ) do={\
  75.    \n\t\t:log warn ( \"sys-ros-backup: encryption password is not set!\" );\
  76.    \n\t\t:set uploadCloud false;\
  77.    \n\t\t:do { /system backup save dont-encrypt=yes name=\$bckpBinFname } on-error={\
  78.    \n\t\t\t:log error ( \"sys-ros-backup: binary backup is failed - storage problems\?\" );\
  79.    \n\t\t\t:error \"[ERR]: Binary backup router configuration is failed\";\
  80.    \n\t\t};\
  81.    \n\t} else={\
  82.    \n\t\t:do { /system backup save encryption=aes-sha256 password=\$m9eBackupPass name=\$bckpBinFname } on-error={\
  83.    \n\t\t\t:log error ( \"sys-ros-backup: binary backup is failed - storage problems\?\" );\
  84.    \n\t\t\t:error \"[ERR]: Binary backup router configuration is failed\";\
  85.    \n\t\t};\
  86.    \n\t};\
  87.    \n\t:log info ( \"sys-ros-backup: rsc backup current configuration\" );\
  88.    \n\t:do { export terse file=\$bckpRscFname; } on-error={\
  89.    \n\t\t:log error ( \"sys-ros-backup: rsc backup is failed - storage problems\?\" );\
  90.    \n\t\t:error \"[ERR]: Text backup router configuration is failed\";\
  91.    \n\t\t\
  92.    \n\t};\
  93.    \n}\
  94.    \n# 3.1 Other backup methods\
  95.    \n# 3.1.1 No methods defined\
  96.    \n:if ( [ :len \$m9eBackupAlso ] = 0 or [ :typeof \$m9eBackupAlso ] != \"str\" ) do={\
  97.    \n\t:log info ( \"sys-ros-backup: additional backup methods wasn't defined or has incorrect type\" );\
  98.    \n\t:do { /system script environment remove [ find name~\"func\" ] } on-error={\
  99.    \n\t\t:log warning ( \"sys-ros-m9e: script environment cleanup is failed\" );\
  100.    \n\t};\
  101.    \n\t:error \"[WRN]: Additional backup methods wasn't defined or has incorrect type\"\
  102.    \n}\
  103.    \n# 3.1.2 Parsing methods\
  104.    \nforeach method in=[ :toarray \$m9eBackupAlso ] do={\
  105.    \n\t# 3.1.3 Cloud backup\
  106.    \n\t# TODO: Should return secret-download-key to end-user.\
  107.    \n\t:if ( method = \"cloud\" ) do={\
  108.    \n\t\t:if ( \$uploadCloud ) do={\
  109.    \n\t\t\t:if ( \$VERBOSE ) do={ :put \"[MSG]: Upload binary backup to cloud\" };\
  110.    \n\t\t\t:log info ( \"sys-ros-backup: upload binary backup to cloud\" );\
  111.    \n\t\t\t:do { \
  112.    \n\t\t\t\t:if ( [ :len [ /system backup cloud find ] ] != 0 ) do={ /system backup cloud remove-file [ find ] ; } ; \
  113.    \n\t\t\t\t/system backup cloud upload-file action=upload src-file=\$bckpBinFname } on-error={\
  114.    \n\t\t\t\t:if ( \$VERBOSE ) do={ :put \"[ERR]: Cloud backup is failed - connectivity problems\?\" };\
  115.    \n\t\t\t\t:log error ( \"sys-ros-backup: cloud backup is failed - connectivity problems\?\" );\
  116.    \n\t\t\t};\
  117.    \n\t\t} else={\
  118.    \n\t\t\t# Encryption password was not set, so binary backup was created with dont-encrypt=yes.\
  119.    \n\t\t\t# This kinds of backup files can't be uploaded to cloud.\
  120.    \n\t\t\t# See official wiki-pages mentioned above.\
  121.    \n\t\t\t:if ( \$VERBOSE ) do={ :put \"[ERR]: Cloud backup is failed - unencrypted file\" };\
  122.    \n\t\t\t:log error ( \"sys-ros-backup: cloud backup is failed - unencrypted file\" );\
  123.    \n\t\t};\
  124.    \n\t};\
  125.    \n\t# 3.1.4 Email backup (assume tool e-mail has valid settings)\
  126.    \n\t:if ( method = \"email\" ) do={\
  127.    \n\t\t:if ( \$sendEmail and \$rcpntEmail ~\"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\\\.[A-Za-z]{2,64}\" ) do={\
  128.    \n\t\t\t:if ( \$VERBOSE ) do={ :put \"[MSG]: Send all backup files via e-mail\" };\
  129.    \n\t\t\tlog info ( \"sys-ros-backup: send all backup files via e-mail\" );\
  130.    \n\t\t\t:local sbjctEmail ( \"[Backup]: Automated backup (\" . [ :pick \$rosid 0 [ :find \$rosid \".\" ] ] . \")\" ) ;\
  131.    \n\t\t\t:local rprtEmailHdr \"Hello \$rcpntEmail,\\nScheduled backup was executed successfully, see attached files.\\n\\n\" ;\
  132.    \n\t\t\t:local rprtEmailId ( \"ID:\" . \$rosid . \"\\n\" )\
  133.    \n\t\t\t:local rprtEmailBin ( \"BIN:\" . [ :pick \$bckpBinFname ( [ find \$bckpBinFname \"pub/\" ] + 4 ) [ :len \$bckpBinFname ]] . \" - \" . [ /file get value-name=creation-time \$bckpBinFname ] . \"\\n\" ) ;\
  134.    \n\t\t\t:local rprtEmailRsc ( \"RSC:\" . [ :pick \$bckpRscFname ( [ find \$bckpRscFname \"pub/\" ] + 4 ) [ :len \$bckpRscFname ]] . \" - \" . [ /file get value-name=creation-time \$bckpRscFname ] . \"\\n\" ) ;\
  135.    \n\t\t\t:local rprtEmailFtr \"\\nGoodbye and have a nice day.\"\
  136.    \n\t\t\t:local bodyEmail ( \$rprtEmailHdr . \$rprtEmailId . \$rprtEmailBin . \$rprtEmailRsc . \$rprtEmailFtr ) ;\
  137.    \n\t\t\t:local attchEmail [ :toarray ( \$bckpBinFname . \",\" . \$bckpRscFname ) ];\
  138.    \n\t\t\t:do { /tool e-mail send subject=\"\$sbjctEmail\" body=\"\$bodyEmail\" to=\"\$rcpntEmail\" file=\$attchEmail } on-error={\
  139.    \n\t\t\t\t:log error ( \"sys-ros-backup: email backup is failed - send errors\" );\
  140.    \n\t\t\t} \
  141.    \n\t\t} else={\
  142.    \n\t\t\t:if ( \$VERBOSE ) do={ :put \"[ERR]: Email backup is failed - invalid recipient\" };\
  143.    \n\t\t\t:log error ( \"sys-ros-backup: email backup is failed - invalid recipient\" );\
  144.    \n\t\t};\
  145.    \n\t};\
  146.    \n\t# 3.1.5 FTP backup\
  147.    \n\tif ( method = \"ftp\" ) do={\
  148.    \n\t\t# FTP-variable may be a hostname or IP address\
  149.    \n\t\t:if ( [ :len \$m9eBackupFTP ] != 0 ) do={ \
  150.    \n\t\t\t:if ( \$VERBOSE ) do={ :put \"[MSG]: Copy all backup files to remote ftp-server\" };\
  151.    \n\t\t\tlog info ( \"sys-ros-backup: copy all backup files to remote ftp-server\" );\
  152.    \n\t\t\t:local bnameBinFname [ :pick \$bckpBinFname ( [ find \$bckpBinFname \"pub/\" ] + 4 ) [ :len \$bckpBinFname ]] ;\
  153.    \n\t\t\t:local bnameRscFname [ :pick \$bckpRscFname ( [ find \$bckpRscFname \"pub/\" ] + 4 ) [ :len \$bckpRscFname ]] ;\
  154.    \n\t\t\t:do {\
  155.    \n\t\t\t\t/tool fetch mode=ftp upload=yes user=\$m9eBackupUser password=\$m9eBackupPass address=\$m9eBackupFTP src-path=\"\$bckpBinFname\" dst-path=\"\$rosid/\$bnameBinFname\"  ;\
  156.    \n\t\t\t\t/tool fetch mode=ftp upload=yes user=\$m9eBackupUser password=\$m9eBackupPass address=\$m9eBackupFTP src-path=\"\$bckpRscFname\" dst-path=\"\$rosid/\$bnameRscFname\" ;\
  157.    \n\t\t\t} on-error={\
  158.    \n\t\t\t\t:if ( \$VERBOSE ) do={ :put \"[ERR]: FTP backup is failed - fetch errors\" };\
  159.    \n\t\t\t\t:log error ( \"sys-ros-backup: ftp backup is failed - fetch errors\" );\
  160.    \n\t\t\t};\
  161.    \n\t\t};\
  162.    \n\t}; \
  163.    \n};\
  164.    \n# 3.2 Clean-up environment \
  165.    \n:do { /system script environment remove [ find name~\"func\" ] } on-error={\
  166.    \n\t:log warning ( \"sys-ros-backup: script environment cleanup is failed\" );\
  167.    \n};\
  168.    \n# 4.0 End"
  169.  
  170. /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