Example_code 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/bin/bash
  2. #
  3. #
  4. # Please note that for the correct operation you
  5. # will need the utilities found here.
  6. #
  7. #
  8. # Static Variables
  9. DIR=/home/user/backup/ # Directory for backups
  10. # # Do not use system directories, create a new!
  11. #
  12. PRT=22 # Port, but if you have different ports for each device:
  13. # # - adjust Supplementer.py
  14. #
  15. #
  16. DATE=$(echo `date +%Y.%m.%d`) # Date
  17. exec 2>> $DIR$DATE".log" # Need for log (errors redirection)
  18. #
  19. #
  20. # Individual variables
  21. IP='192.168.1.1' # Mikrotik IP
  22. LGIN='admin' # Mikrotik login (user)
  23. PASS='example password' # Password (SSH)
  24. NAME='TestRouter' # Name router
  25. #status=$(ping -c 1 $IP &> /dev/null && echo success || echo fail) # Verification. I do not recommend
  26. status=$(netcat -w3 -z $IP $PRT && echo success || echo fail) # ICMP, but you can comment on
  27. # # the unnecessary, use #
  28. #
  29. #NEWVARIABLE
  30. #
  31. #
  32. # Other Variables
  33. SHELF=13 # Shelf life, needed to remove old backups (in days)
  34. gg=success # Variable to check (netcat or ICMP check)
  35. #
  36. #
  37. #
  38. # Echo is necessary for visualizing and recording errors.
  39. # If verification passes, a backup will be created on the router,
  40. # then saved locally,
  41. # after which the backup will be deleted from the router.
  42. # If not, an error will be displayed, excluding utility errors.
  43. #
  44. function router0 {
  45. echo "
  46. Start $IP ($NAME)...
  47. "
  48. if [ $status = $gg ]
  49. then
  50. echo "_______________________________
  51. Status $IP - OK, create backup:"
  52. sshpass -p $PASS ssh -T -p $PRT $LGIN@$IP << EOF
  53. export file=$IP$NAME
  54. system backup save name=$IP$NAME
  55. quit
  56. EOF
  57. echo "
  58. Lockal save backup's...
  59. "
  60. sshpass -p $PASS scp -P $PRT $LGIN@$IP":/$IP$NAME.rsc" $DIR
  61. sshpass -p $PASS scp -P $PRT $LGIN@$IP":/$IP$NAME.backup" $DIR
  62. echo "
  63. Cleaning router directory...
  64. "
  65. sshpass -p $PASS ssh -T -p $PRT $LGIN@$IP << EOF
  66. file remove "$IP$NAME.rsc"
  67. file remove "$IP$NAME.backup"
  68. quit
  69. EOF
  70. echo "
  71. $IP END
  72. _______________________________
  73. "
  74. else
  75. echo " ...............................
  76. ERROR $IP,
  77. backup can't create and save.
  78. Check the specified port and ip
  79. ...............................
  80. _______________________________
  81. "
  82. fi
  83. }
  84. #
  85. #
  86. #NEWFUNK
  87. #
  88. #
  89. function startsaver{
  90. router0
  91. # NEWNAME
  92. }
  93. #
  94. #
  95. #
  96. startsaver >> $DIR$DATE.log # Start save backup's
  97. #
  98. #
  99. #
  100. # Logging, archiving and version control
  101. #
  102. # If an error occurred while creating or copying, including when
  103. # utilities were working, the archive will be marked as broken,
  104. # and you will see an error in the log
  105. statsave=$(cat $DIR$DATE.log | grep "ERROR \| failed \| denied \| error \| Permission" && echo ERROR || echo success )
  106. #
  107. function archandel {
  108. echo "
  109. _______________________________
  110. Archiving of collected backup's started:"
  111. if [ "$statsave" = "$gg" ]
  112. then
  113. zip -9 -j $DIR$DATE.zip $DIR*.backup $DIR*rsc
  114. rm $DIR*.backup $DIR*.rsc
  115. echo "
  116. search and delete old versions"
  117. find "$DIR"2* -mtime +$SHELF -delete
  118. echo "
  119. Archiving done
  120. _______________________________"
  121. else
  122. echo " ...............................
  123. ERROR
  124. not all backups are received
  125. сheck the correctness
  126. of the entered data
  127. and read the log!
  128. ..............................."
  129. zip -9 -j $DIR$DATE"_brocken".zip $DIR*.backup $DIR*rsc
  130. rm $DIR*.backup $DIR*.rsc
  131. echo " _______________________________"
  132. fi
  133. }
  134. #
  135. archandel >> $DIR$DATE.log # Archiving of collected backup's started
  136. # Search and delete old versions opt.
  137. #
  138. #
  139. #
  140. # I suggest using the Telegram bot for alerts.
  141. # It is convenient and quite simple. You can learn about how to find
  142. # out the chat ID and bot token from the many tutorials on the network,
  143. # I will not describe it.
  144. # Attention!
  145. # If this messenger is blocked in your country, you will have to use VPN or PROXY.
  146. #
  147. #
  148. # Variables for alerts
  149. TOKEN="000000000:AAAaAAaAAAA-0aa0-AaaAaAAAaaaaaaA0aa"
  150. IDCHAT="-0000000000000" # ID dialogue (only bot and you) will be unsigned('-')
  151. URL="https://api.telegram.org/bot$TOKEN/sendMessage"
  152. #
  153. #
  154. # If you want to use proxies, just use these variables instead of the ones presented below:
  155. #PROXYSOCKS= #example: PROXYSOCKS="192.168.1.2:1080"
  156. #TGMESS="curl --silent --show-error --fail -k -G -o /dev/null -x socks5://${PROXYSOCKS} ${URL} -d chat_id=${IDCHAT} "
  157. # If the proxy type in the variable above is not suitable for you needs,
  158. # refer to the documentation utility 'curl' and correct the variable above.
  159. #
  160. #
  161. TGMESS="curl --silent --show-error --fail -k -G -o /dev/null $URL -d chat_id=$IDCHAT "
  162. # Function alerts
  163. function alert {
  164. echo " _______________________________
  165. Alerts are generated"
  166. if [ "$statsave" = "$gg" ]
  167. then
  168. $TGMESS --data-urlencode "text=Creation of backups $DATE was successful"
  169. echo "END
  170. _______________________________"
  171. else
  172. $TGMESS --data-urlencode "text=An error occurred while creating backups, check the $DATE log"
  173. echo "END
  174. _______________________________"
  175. fi
  176. }
  177. #
  178. alert >> $DIR$DATE.log