freedombone-utils-backup 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Backup functions
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2016 Bob Mottram <bob@robotics.uk.to>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. function configure_backup_key {
  31. if grep -Fxq "configure_backup_key" $COMPLETION_FILE; then
  32. return
  33. fi
  34. apt-get -y install gnupg
  35. BACKUP_KEY_EXISTS=$(gpg_key_exists "root" "$MY_NAME (backup key)")
  36. if [[ $BACKUP_KEY_EXISTS == "yes" ]]; then
  37. return
  38. fi
  39. # Generate a GPG key for backups
  40. BACKUP_KEY_EXISTS=$(gpg_key_exists "$MY_USERNAME" "$MY_NAME (backup key)")
  41. if [[ $BACKUP_KEY_EXISTS == "no" ]]; then
  42. echo 'Key-Type: 1' > /home/$MY_USERNAME/gpg-genkey.conf
  43. echo 'Key-Length: 4096' >> /home/$MY_USERNAME/gpg-genkey.conf
  44. echo 'Subkey-Type: 1' >> /home/$MY_USERNAME/gpg-genkey.conf
  45. echo 'Subkey-Length: 4096' >> /home/$MY_USERNAME/gpg-genkey.conf
  46. echo "Name-Real: $MY_NAME" >> /home/$MY_USERNAME/gpg-genkey.conf
  47. echo "Name-Email: $MY_EMAIL_ADDRESS" >> /home/$MY_USERNAME/gpg-genkey.conf
  48. echo "Name-Comment: backup key" >> /home/$MY_USERNAME/gpg-genkey.conf
  49. echo 'Expire-Date: 0' >> /home/$MY_USERNAME/gpg-genkey.conf
  50. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/gpg-genkey.conf
  51. echo $'Backup key does not exist. Creating it.'
  52. su -c "gpg --batch --gen-key /home/$MY_USERNAME/gpg-genkey.conf" - $MY_USERNAME
  53. shred -zu /home/$MY_USERNAME/gpg-genkey.conf
  54. echo $'Checking that the Backup key was created'
  55. BACKUP_KEY_EXISTS=$(gpg_key_exists "$MY_USERNAME" "$MY_NAME (backup key)")
  56. if [[ $BACKUP_KEY_EXISTS == "no" ]]; then
  57. echo $'Backup key could not be created'
  58. exit 43382
  59. fi
  60. fi
  61. MY_BACKUP_KEY_ID=$(su -c "gpg --list-keys \"$MY_NAME (backup key)\" | grep 'pub '" - $MY_USERNAME | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}')
  62. echo "Backup key: $MY_BACKUP_KEY_ID"
  63. MY_BACKUP_KEY=/home/$MY_USERNAME/backup_key
  64. su -c "gpg --output ${MY_BACKUP_KEY}_public.asc --armor --export $MY_BACKUP_KEY_ID" - $MY_USERNAME
  65. su -c "gpg --output ${MY_BACKUP_KEY}_private.asc --armor --export-secret-key $MY_BACKUP_KEY_ID" - $MY_USERNAME
  66. if [ ! -f ${MY_BACKUP_KEY}_public.asc ]; then
  67. echo 'Public backup key could not be exported'
  68. exit 36829
  69. fi
  70. if [ ! -f ${MY_BACKUP_KEY}_private.asc ]; then
  71. echo 'Private backup key could not be exported'
  72. exit 29235
  73. fi
  74. # import backup key to root user
  75. gpg --import --import ${MY_BACKUP_KEY}_public.asc
  76. gpg --allow-secret-key-import --import ${MY_BACKUP_KEY}_private.asc
  77. shred -zu ${MY_BACKUP_KEY}_public.asc
  78. shred -zu ${MY_BACKUP_KEY}_private.asc
  79. echo 'configure_backup_key' >> $COMPLETION_FILE
  80. }
  81. function backup_to_friends_servers {
  82. # update crontab
  83. echo '#!/bin/bash' > /etc/cron.daily/backuptofriends
  84. echo "if [ -f /usr/local/bin/${PROJECT_NAME}-backup-remote ]; then" >> /etc/cron.daily/backuptofriends
  85. echo " /usr/local/bin/${PROJECT_NAME}-backup-remote" >> /etc/cron.daily/backuptofriends
  86. echo 'else' >> /etc/cron.daily/backuptofriends
  87. echo " /usr/bin/${PROJECT_NAME}-backup-remote" >> /etc/cron.daily/backuptofriends
  88. echo 'fi' >> /etc/cron.daily/backuptofriends
  89. chmod +x /etc/cron.daily/backuptofriends
  90. }
  91. # NOTE: deliberately no exit 0