core-restore-from-backup.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env bash
  2. clear
  3. YW=`echo "\033[33m"`
  4. BL=`echo "\033[36m"`
  5. RD=`echo "\033[01;31m"`
  6. BGN=`echo "\033[4;92m"`
  7. GN=`echo "\033[1;92m"`
  8. DGN=`echo "\033[32m"`
  9. CL=`echo "\033[m"`
  10. BFR="\\r\\033[K"
  11. HOLD="-"
  12. CM="${GN}✓${CL}"
  13. APP="Home Assistant Core"
  14. while true; do
  15. read -p "This will restore ${APP} from a backup. Proceed(y/n)?" yn
  16. case $yn in
  17. [Yy]* ) break;;
  18. [Nn]* ) exit;;
  19. * ) echo "Please answer yes or no.";;
  20. esac
  21. done
  22. clear
  23. function header_info {
  24. cat << "EOF"
  25. __ __ ___ _ __ __ ______
  26. / / / /___ ____ ___ ___ / | __________(_)____/ /_____ _____ / /_ / ____/___ ________
  27. / /_/ / __ \/ __ `__ \/ _ \ / /| | / ___/ ___/ / ___/ __/ __ `/ __ \/ __/ / / / __ \/ ___/ _ \
  28. / __ / /_/ / / / / / / __/ / ___ |(__ |__ ) (__ ) /_/ /_/ / / / / /_ / /___/ /_/ / / / __/
  29. /_/ /_/\____/_/ /_/ /_/\___/ /_/ |_/____/____/_/____/\__/\__,_/_/ /_/\__/ \____/\____/_/ \___/
  30. RESTORE FROM BACKUP
  31. EOF
  32. }
  33. header_info
  34. function msg_info() {
  35. local msg="$1"
  36. echo -ne " ${HOLD} ${YW}${msg}..."
  37. }
  38. function msg_ok() {
  39. local msg="$1"
  40. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  41. }
  42. DIR=/root/.homeassistant/restore
  43. if [ -d "$DIR" ];
  44. then
  45. msg_ok "Restore Directory Exists."
  46. else
  47. mkdir -p /root/.homeassistant/restore
  48. msg_ok "Created Restore Directory."
  49. fi
  50. if [ -z "$(ls -A /root/.homeassistant/backups/)" ]; then echo -e "${RD}No backups found!${CL} \n"; exit 1; fi
  51. cd /root/.homeassistant/backups/
  52. PS3="Please enter your choice: "
  53. files="$(ls -A .)"
  54. select filename in ${files}; do msg_ok "You selected ${BL}${filename}${CL}"; break; done
  55. msg_info "Stopping Home Assistant"
  56. sudo service homeassistant stop
  57. msg_ok "Stopped Home Assistant"
  58. msg_info "Restoring Home Assistant using ${filename}"
  59. tar xvf ${filename} -C /root/.homeassistant/restore &>/dev/null
  60. cd /root/.homeassistant/restore
  61. tar -xvf homeassistant.tar.gz &>/dev/null
  62. if ! command -v rsync >/dev/null 2>&1; then apt-get install -y rsync &>/dev/null; fi
  63. rsync -a /root/.homeassistant/restore/data/ /root/.homeassistant
  64. rm -rf /root/.homeassistant/restore/*
  65. msg_ok "Restore Complete"
  66. msg_ok "Starting Home Assistant \n"
  67. sudo service homeassistant start