freedombone-remote 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Creates an inventory of remote backup locations
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2015 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 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 General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. NO_OF_ARGS=$#
  31. # User to create the list for
  32. MY_USERNAME=
  33. # Filename of the remote backups list
  34. FRIENDS_SERVERS_LIST=
  35. # Minimum password length in characters
  36. MINIMUM_PASSWORD_LENGTH=10
  37. # How many remote locations were specified
  38. entering_remote_backups_ctr=0
  39. function show_help {
  40. echo ''
  41. echo 'freedombone-remote -u [username] -l [backup list filename] -m [min password length]'
  42. echo ''
  43. echo 'Creates an inventory of remote backup locations'
  44. echo ''
  45. echo ''
  46. echo ' -h --help Show help'
  47. echo ' -l --list Remote backup list (usually /home/$USER/backup.list)'
  48. echo ' -m --min Minimum password length (characters)'
  49. echo ''
  50. exit 0
  51. }
  52. # If there are no options specified
  53. if [[ $NO_OF_ARGS == 0 ]]; then
  54. show_help
  55. exit 0
  56. fi
  57. # Get the commandline options
  58. while [[ $# > 1 ]]
  59. do
  60. key="$1"
  61. case $key in
  62. -h|--help)
  63. show_help
  64. ;;
  65. # backup list filename
  66. # typically /home/$USER/backup.list
  67. -l|--list)
  68. shift
  69. FRIENDS_SERVERS_LIST="$1"
  70. ;;
  71. # username within /home
  72. -u|--user)
  73. shift
  74. MY_USERNAME="$1"
  75. ;;
  76. # Minimum password length
  77. -m|--min)
  78. shift
  79. MINIMUM_PASSWORD_LENGTH="$1"
  80. ;;
  81. *)
  82. # unknown option
  83. ;;
  84. esac
  85. shift
  86. done
  87. function interactive_configuration_remote_backups {
  88. if [ ! $MY_USERNAME ]; then
  89. echo 'Please specify a username with the -u option'
  90. exit 7356
  91. fi
  92. if [ ! /home/$MY_USERNAME ]; then
  93. echo "The user /home/$MY_USERNAME does not exist on the system"
  94. exit 3689
  95. fi
  96. if [ ! $FRIENDS_SERVERS_LIST ]; then
  97. FRIENDS_SERVERS_LIST=/home/$MY_USERNAME/backup.list
  98. fi
  99. # clear any existing list
  100. if [ -f $FRIENDS_SERVERS_LIST ]; then
  101. rm -f $FRIENDS_SERVERS_LIST
  102. touch $FRIENDS_SERVERS_LIST
  103. fi
  104. # number of entries made
  105. entering_remote_backups_ctr=1
  106. entering_remote_backups_done="no"
  107. while [[ $entering_remote_backups_done == "no" ]]
  108. do
  109. data=$(tempfile 2>/dev/null)
  110. trap "rm -f $data" 0 1 2 5 15
  111. dialog --backtitle "Freedombone Configuration" \
  112. --title "Remote Backup ${entering_remote_backups_ctr}" \
  113. --form "\nPlease specify the SSH login details:" 11 55 4 \
  114. "Username:" 1 1 "" 1 16 16 15 \
  115. "Domain:" 2 1 "" 2 16 16 15 \
  116. "SSH port:" 3 1 "22" 3 16 5 4 \
  117. "Password:" 4 1 "" 4 16 20 100 \
  118. 2> $data
  119. sel=$?
  120. case $sel in
  121. 1) entering_remote_backups_done="yes";;
  122. 255) entering_remote_backups_done="yes";;
  123. esac
  124. remote_ssh_username=$(cat $data | sed -n 1p)
  125. remote_ssh_domain=$(cat $data | sed -n 2p)
  126. remote_ssh_port=$(cat $data | sed -n 3p)
  127. remote_ssh_password=$(cat $data | sed -n 4p)
  128. if [[ $remote_ssh_username != "" && \
  129. $remote_ssh_domain != "" && \
  130. $remote_ssh_port != "" && \
  131. $remote_ssh_password != "" ]]; then
  132. if [ ${#remote_ssh_password} -lt $MINIMUM_PASSWORD_LENGTH ]; then
  133. dialog --title "Password quality check" --msgbox "The password given was too short. It must be at least $MINIMUM_PASSWORD_LENGTH characters" 6 40
  134. else
  135. echo "$remote_ssh_username@$remote_ssh_domain:$remote_ssh_port//home/$remote_ssh_username $remote_ssh_password" >> $FRIENDS_SERVERS_LIST
  136. entering_remote_backups_ctr=$((entering_remote_backups_ctr + 1))
  137. fi
  138. else
  139. entering_remote_backups_done="yes"
  140. fi
  141. done
  142. if [ -f $FRIENDS_SERVERS_LIST ]; then
  143. chown $MY_USERNAME:$MY_USERNAME $FRIENDS_SERVERS_LIST
  144. fi
  145. }
  146. function show_result {
  147. clear
  148. if (( $entering_remote_backups_ctr < 2 )); then
  149. echo 'No remote backup locations were specified'
  150. exit 0
  151. fi
  152. if [ ! -f $FRIENDS_SERVERS_LIST ]; then
  153. echo "No remote backups list found: $FRIENDS_SERVERS_LIST"
  154. exit 7358
  155. fi
  156. echo ''
  157. echo "Remote backups list: $FRIENDS_SERVERS_LIST"
  158. echo ''
  159. echo 'Contents:'
  160. echo ''
  161. cat $FRIENDS_SERVERS_LIST
  162. echo ''
  163. }
  164. interactive_configuration_remote_backups
  165. show_result
  166. exit 0