freedombone-remote 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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=$USER
  33. # Filename of the remote backups list
  34. FRIENDS_SERVERS_LIST=/home/$MY_USERNAME/backup.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. # Title shown
  40. TITLE='Remote Backup'
  41. # Whether to include the capability of adding reciprocal user accounts
  42. # such that whoever is running a remote server can also use your server to
  43. # store backups
  44. RECIPROCAL="no"
  45. function show_help {
  46. echo ''
  47. echo 'freedombone-remote -u [username] -l [backup list filename] -m [min password length]'
  48. echo ''
  49. echo 'Creates an inventory of remote backup locations'
  50. echo ''
  51. echo ''
  52. echo ' -h --help Show help'
  53. echo ' -u --username User to create the backups.list file for'
  54. echo ' -l --list Remote backup list (usually /home/$USER/backup.list)'
  55. echo ' -m --min Minimum password length (characters)'
  56. echo ' -r --reciprocal Whether to add reciprocal user accounts'
  57. echo ' -t --title Title shown'
  58. echo ''
  59. exit 0
  60. }
  61. # Get the commandline options
  62. while [[ $# > 1 ]]
  63. do
  64. key="$1"
  65. case $key in
  66. -h|--help)
  67. show_help
  68. ;;
  69. # backup list filename
  70. # typically /home/$USER/backup.list
  71. -l|--list)
  72. shift
  73. FRIENDS_SERVERS_LIST="$1"
  74. ;;
  75. # username within /home
  76. -u|--user)
  77. shift
  78. MY_USERNAME="$1"
  79. ;;
  80. # Minimum password length
  81. -m|--min)
  82. shift
  83. MINIMUM_PASSWORD_LENGTH="$1"
  84. ;;
  85. # Title shown
  86. -t|--title)
  87. shift
  88. TITLE="$1"
  89. ;;
  90. # reciprocal user accounts
  91. -r|--reciprocal)
  92. shift
  93. RECIPROCAL="yes"
  94. ;;
  95. *)
  96. # unknown option
  97. ;;
  98. esac
  99. shift
  100. done
  101. function interactive_configuration_remote_backups {
  102. if [ ! $MY_USERNAME ]; then
  103. echo 'Please specify a username with the -u option'
  104. exit 7356
  105. fi
  106. if [ ! /home/$MY_USERNAME ]; then
  107. echo "The user /home/$MY_USERNAME does not exist on the system"
  108. exit 3689
  109. fi
  110. if [ ! $FRIENDS_SERVERS_LIST ]; then
  111. FRIENDS_SERVERS_LIST=/home/$MY_USERNAME/backup.list
  112. fi
  113. # clear any existing list
  114. if [ -f $FRIENDS_SERVERS_LIST ]; then
  115. rm -f $FRIENDS_SERVERS_LIST
  116. touch $FRIENDS_SERVERS_LIST
  117. fi
  118. # number of entries made
  119. entering_remote_backups_ctr=1
  120. entering_remote_backups_done="no"
  121. remote_ssh_username=""
  122. remote_ssh_domain=""
  123. remote_ssh_port=""
  124. remote_ssh_password=""
  125. remote_ssh_reciprocal_username=""
  126. remote_ssh_reciprocal_password=""
  127. while [[ $entering_remote_backups_done == "no" ]]
  128. do
  129. data=$(tempfile 2>/dev/null)
  130. trap "rm -f $data" 0 1 2 5 15
  131. if [[ $RECIPROCAL == "yes" ]]; then
  132. dialog --backtitle "Freedombone Configuration" \
  133. --title "$TITLE ${entering_remote_backups_ctr}" \
  134. --form "\nPlease specify the SSH login details for the remote server\n\nThe reciprocal entries are optional, and can be used if you wish to set up a user account on this system for whoever runs the remote server to also use for backups" 20 50 8 \
  135. "Username:" 1 1 "$remote_ssh_username" 1 23 16 15 \
  136. "Domain:" 2 1 "$remote_ssh_domain" 2 23 16 15 \
  137. "SSH port:" 3 1 "22" 3 23 5 4 \
  138. "Password:" 4 1 "$remote_ssh_password" 4 23 20 100 \
  139. "Reciprocal Username:" 5 1 "$remote_ssh_reciprocal_username" 5 23 20 100 \
  140. "Reciprocal Password:" 6 1 "$remote_ssh_reciprocal_password" 6 23 20 100 \
  141. 2> $data
  142. else
  143. dialog --backtitle "Freedombone Configuration" \
  144. --title "$TITLE ${entering_remote_backups_ctr}" \
  145. --form "\nPlease specify the SSH login details for the remote server" 15 50 4 \
  146. "Username:" 1 1 "$remote_ssh_username" 1 23 16 15 \
  147. "Domain:" 2 1 "$remote_ssh_domain" 2 23 16 15 \
  148. "SSH port:" 3 1 "22" 3 23 5 4 \
  149. "Password:" 4 1 "$remote_ssh_password" 4 23 20 100 \
  150. 2> $data
  151. fi
  152. sel=$?
  153. case $sel in
  154. 1) entering_remote_backups_done="yes";;
  155. 255) entering_remote_backups_done="yes";;
  156. esac
  157. remote_ssh_username=$(cat $data | sed -n 1p)
  158. remote_ssh_domain=$(cat $data | sed -n 2p)
  159. remote_ssh_port=$(cat $data | sed -n 3p)
  160. remote_ssh_password=$(cat $data | sed -n 4p)
  161. remote_ssh_reciprocal_username=$(cat $data | sed -n 5p)
  162. remote_ssh_reciprocal_password=$(cat $data | sed -n 6p)
  163. if [[ $remote_ssh_username != "" && \
  164. $remote_ssh_domain != "" && \
  165. $remote_ssh_port != "" && \
  166. $remote_ssh_password != "" ]]; then
  167. if [ ${#remote_ssh_password} -lt $MINIMUM_PASSWORD_LENGTH ]; then
  168. dialog --title "Password quality check" --msgbox "The password given was too short. It must be at least $MINIMUM_PASSWORD_LENGTH characters" 6 40
  169. else
  170. if [[ $RECIPROCAL == "yes" ]]; then
  171. if [[ $remote_ssh_reciprocal_username != "" && \
  172. $remote_ssh_reciprocal_password != "" ]]; then
  173. if [ ${#remote_ssh_reciprocal_password} -lt $MINIMUM_PASSWORD_LENGTH ]; then
  174. dialog --title "Password quality check" --msgbox "The reciprocal password given was too short. It must be at least $MINIMUM_PASSWORD_LENGTH characters" 6 40
  175. else
  176. echo ${remote_ssh_reciprocal_username}:${remote_ssh_reciprocal_password}::::/home/${remote_ssh_reciprocal_username}:bash | newusers
  177. echo "$remote_ssh_username@$remote_ssh_domain:$remote_ssh_port//home/$remote_ssh_username $remote_ssh_password" >> $FRIENDS_SERVERS_LIST
  178. remote_ssh_username=""
  179. remote_ssh_domain=""
  180. remote_ssh_port=""
  181. remote_ssh_password=""
  182. remote_ssh_reciprocal_username=""
  183. remote_ssh_reciprocal_password=""
  184. entering_remote_backups_ctr=$((entering_remote_backups_ctr + 1))
  185. fi
  186. else
  187. echo "$remote_ssh_username@$remote_ssh_domain:$remote_ssh_port//home/$remote_ssh_username $remote_ssh_password" >> $FRIENDS_SERVERS_LIST
  188. remote_ssh_username=""
  189. remote_ssh_domain=""
  190. remote_ssh_port=""
  191. remote_ssh_password=""
  192. remote_ssh_reciprocal_username=""
  193. remote_ssh_reciprocal_password=""
  194. entering_remote_backups_ctr=$((entering_remote_backups_ctr + 1))
  195. fi
  196. else
  197. echo "$remote_ssh_username@$remote_ssh_domain:$remote_ssh_port//home/$remote_ssh_username $remote_ssh_password" >> $FRIENDS_SERVERS_LIST
  198. remote_ssh_username=""
  199. remote_ssh_domain=""
  200. remote_ssh_port=""
  201. remote_ssh_password=""
  202. entering_remote_backups_ctr=$((entering_remote_backups_ctr + 1))
  203. fi
  204. fi
  205. else
  206. entering_remote_backups_done="yes"
  207. fi
  208. done
  209. if [ -f $FRIENDS_SERVERS_LIST ]; then
  210. chown $MY_USERNAME:$MY_USERNAME $FRIENDS_SERVERS_LIST
  211. fi
  212. }
  213. function show_result {
  214. clear
  215. if (( $entering_remote_backups_ctr < 2 )); then
  216. echo 'No remote backup locations were specified'
  217. exit 0
  218. fi
  219. if [ ! -f $FRIENDS_SERVERS_LIST ]; then
  220. echo "No remote backups list found: $FRIENDS_SERVERS_LIST"
  221. exit 7358
  222. fi
  223. echo ''
  224. echo "Remote backups list: $FRIENDS_SERVERS_LIST"
  225. echo ''
  226. echo 'Contents:'
  227. echo ''
  228. cat $FRIENDS_SERVERS_LIST
  229. echo ''
  230. }
  231. interactive_configuration_remote_backups
  232. show_result
  233. exit 0