freedombone-remote 5.0KB

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