rcmpayne22

Untitled

Jul 20th, 2020
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ## Created by Roger P
  4. ## Updated: 7.20.2020
  5. ## Created to backup my UDM backup files to Unraid NAS daily
  6.  
  7.  
  8.  
  9. ########################
  10. ##### Prerequisite #####
  11. ########################
  12.  
  13. # Unraid Steps:
  14. ## Install Nerd Pack
  15. ### Install expect package
  16.  
  17. # Unraid Steps:
  18. ## SSH Unraid and run the following commands
  19. ## # ssh-keygen (enter to all options)
  20.  
  21. # Unraid Steps:
  22. ## SSH the UDM Pro Once (Needed to save the host as known address on Unraid)
  23. ## # ssh root@<UDMIP> (successfully connect once)
  24.  
  25. # Unraid Steps:
  26. ## Backup the Keys so when Unraid is rebooted we ratain a copy
  27. ## # cp /root/.ssh/id_rsa /boot/config/ssh
  28. ## # cp /root/.ssh/id_rsa.pub /boot/config/ssh
  29. ## # cp /root/.ssh/known_hosts /boot/config/ssh
  30.  
  31. # Unraid Steps:
  32. ## Create a script with the UDM password
  33. ## # touch /mnt/user/appdata/login.expect
  34. ## # chmod +x /mnt/user/appdata/login.expect
  35. ## edit the file and add the following (Change <UDM SSH Password>) (Don't add COMMENT1)
  36. <<COMMENT1
  37. #!/usr/bin/expect -f
  38. spawn ssh-copy-id $argv
  39. expect "password:"
  40. send "<UDM_SSH_Password>\n"
  41. expect eof
  42. COMMENT1
  43.  
  44.  
  45.  
  46. #####################################
  47. ##### User adjustable variables #####
  48. #####################################
  49.  
  50. userName=root
  51. UDMServer="10.0.0.2"
  52. toEmailAddress="[email protected]"
  53. fromEmailAddress="[email protected]"
  54. emailSubject="UDMPro Backup Error"
  55. emailBody="Error backing up UDMPro configuration file for $UDMServer."
  56. backUpPath="/mnt/user/appdata/UDMPro_backup"
  57. backUpsToKeep=365
  58.  
  59.  
  60.  
  61. ##################
  62. ##### Script #####
  63. ##################
  64.  
  65. ## Move ssh files back to .ssh. This is needed after reboots
  66. cp /boot/config/ssh/id_rsa /root/.ssh/
  67. cp /boot/config/ssh/known_hosts /root/.ssh/
  68. cp /boot/config/ssh/id_rsa.pub /root/.ssh/
  69.  
  70. ## Run This to ensure ssh keys are re-added to UDM upon reboot
  71. cd /mnt/user/appdata/
  72. ./login.expect $userName@$UDMServer
  73.  
  74. ## Check if backup directory exists, if it doesn't then create it
  75. if [ ! -d $backUpPath ]
  76. then
  77. mkdir -p $backUpPath
  78. fi
  79.  
  80. ## Get latest backup
  81. scp $userName@$UDMServer:/mnt/data/unifi-os/unifi/data/backup/autobackup/*.unf $backUpPath/ \
  82. || {
  83. echo To: $toEmailAddress
  84. echo From: $fromEmailAddress
  85. echo Subject: $emailSubject
  86. echo "$emailBody"
  87. } | ssmtp $toEmailAddress
  88.  
  89. ## Clean up old backups
  90. if [ $(ls -t |tail -n +$(( $backUpsToKeep + 1 )) |wc -l) -gt 0 ]
  91. then
  92. rm $(ls -t $backUpPath |tail -n +$(( $backUpsToKeep + 1 )))
  93. fi
Add Comment
Please, Sign In to add comment