freedombone/src/freedombone-remote

263 lines
9.7 KiB
Plaintext
Raw Normal View History

#!/bin/bash
2018-04-08 14:30:21 +02:00
# _____ _ _
# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# | __| _| -_| -_| . | . | | . | . | | -_|
# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
#
2018-04-08 14:30:21 +02:00
# Freedom in the Cloud
#
# Creates an inventory of remote backup locations
#
# License
# =======
#
2018-02-21 20:32:13 +01:00
# Copyright (C) 2015-2018 Bob Mottram <bob@freedombone.net>
#
# This program is free software: you can redistribute it and/or modify
2016-02-13 23:09:27 +01:00
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
2016-02-13 23:09:27 +01:00
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
2016-02-13 23:09:27 +01:00
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
NO_OF_ARGS=$#
2015-11-27 12:42:16 +01:00
PROJECT_NAME='freedombone'
2015-11-27 17:52:23 +01:00
export TEXTDOMAIN=${PROJECT_NAME}-remote
2015-11-27 12:42:16 +01:00
export TEXTDOMAINDIR="/usr/share/locale"
2018-03-02 20:17:02 +01:00
CONFIG_FILE="$HOME/${PROJECT_NAME}.cfg"
2016-06-04 15:25:46 +02:00
# User to create the list for
2015-04-10 13:56:51 +02:00
MY_USERNAME=$USER
# Filename of the remote backups list
2015-12-22 22:00:15 +01:00
FRIENDS_SERVERS_LIST=
# Minimum password length in characters
2018-03-02 20:17:02 +01:00
MINIMUM_PASSWORD_LENGTH=$(grep 'MINIMUM_PASSWORD_LENGTH=' "/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-passwords" | head -n 1 | awk -F '=' '{print $2}')
# How many remote locations were specified
entering_remote_backups_ctr=0
2015-07-03 20:46:43 +02:00
# Title shown
TITLE='Remote Backup'
# Whether to include the capability of adding reciprocal user accounts
# such that whoever is running a remote server can also use your server to
# store backups
RECIPROCAL="no"
function show_help {
2016-10-31 17:24:49 +01:00
echo ''
echo $"${PROJECT_NAME}-remote -u [username] -l [backup list filename] -m [min password length]"
echo ''
echo $'Creates an inventory of remote backup locations'
echo ''
echo ''
echo $' -h --help Show help'
echo $' -u --username User to create the backups.list file for'
echo $' -l --list Remote backup list (usually /home/$USER/backup.list)'
echo $' -m --min Minimum password length (characters)'
echo $' -r --reciprocal Whether to add reciprocal user accounts'
echo $' -t --title Title shown'
echo ''
exit 0
}
# Get the commandline options
2018-02-25 12:04:13 +01:00
while [ $# -gt 1 ]
do
2016-10-31 17:24:49 +01:00
key="$1"
2016-10-31 17:24:49 +01:00
case $key in
-h|--help)
show_help
;;
# backup list filename
# typically /home/$USER/backup.list
-l|--list)
shift
FRIENDS_SERVERS_LIST="$1"
;;
# username within /home
-u|--user)
shift
MY_USERNAME="$1"
;;
# Minimum password length
-m|--min)
shift
MINIMUM_PASSWORD_LENGTH="$1"
;;
# Title shown
-t|--title)
shift
TITLE="$1"
;;
# reciprocal user accounts
-r|--reciprocal)
shift
RECIPROCAL="yes"
;;
*)
# unknown option
;;
esac
shift
done
function interactive_config_remote_backups {
2018-03-02 20:17:02 +01:00
if [ ! "$MY_USERNAME" ]; then
2016-10-31 17:24:49 +01:00
echo $'Please specify a username with the -u option'
exit 7356
fi
2018-03-02 20:17:02 +01:00
if [ ! -d "/home/$MY_USERNAME" ]; then
2016-10-31 17:24:49 +01:00
echo $"The user /home/$MY_USERNAME does not exist on the system"
exit 3689
fi
2018-03-02 20:17:02 +01:00
if [ ! "$FRIENDS_SERVERS_LIST" ]; then
2016-10-31 17:24:49 +01:00
FRIENDS_SERVERS_LIST=/home/$MY_USERNAME/backup.list
fi
2016-10-31 17:24:49 +01:00
# clear any existing list
2018-03-02 20:17:02 +01:00
if [ -f "$FRIENDS_SERVERS_LIST" ]; then
rm -f "$FRIENDS_SERVERS_LIST"
touch "$FRIENDS_SERVERS_LIST"
2016-10-31 17:24:49 +01:00
fi
# number of entries made
entering_remote_backups_ctr=1
2016-10-31 17:24:49 +01:00
entering_remote_backups_done="no"
remote_ssh_username=""
remote_ssh_domain=""
remote_ssh_port=""
remote_ssh_password=""
remote_ssh_reciprocal_username=""
remote_ssh_reciprocal_password=""
while [[ $entering_remote_backups_done == "no" ]]
do
2018-03-02 20:17:02 +01:00
data=$(mktemp 2>/dev/null)
2016-10-31 17:24:49 +01:00
if [[ $RECIPROCAL == "yes" ]]; then
dialog --backtitle "Freedombone Configuration" \
--title "$TITLE ${entering_remote_backups_ctr}" \
2018-03-02 20:17:02 +01:00
--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 \
2016-10-31 17:24:49 +01:00
"Username:" 1 1 "$remote_ssh_username" 1 23 16 15 \
"Domain:" 2 1 "$remote_ssh_domain" 2 23 16 15 \
"SSH port:" 3 1 "2222" 3 23 5 4 \
"Password:" 4 1 "$remote_ssh_password" 4 23 20 100 \
"Reciprocal Username:" 5 1 "$remote_ssh_reciprocal_username" 5 23 20 100 \
"Reciprocal Password:" 6 1 "$remote_ssh_reciprocal_password" 6 23 20 100 \
2018-03-02 20:17:02 +01:00
2> "$data"
2016-10-31 17:24:49 +01:00
else
dialog --backtitle "Freedombone Configuration" \
--title "$TITLE ${entering_remote_backups_ctr}" \
2018-03-02 20:17:02 +01:00
--form "\\nPlease specify the SSH login details for the remote server" 15 50 4 \
2016-10-31 17:24:49 +01:00
"Username:" 1 1 "$remote_ssh_username" 1 23 16 15 \
"Domain:" 2 1 "$remote_ssh_domain" 2 23 16 15 \
"SSH port:" 3 1 "2222" 3 23 5 4 \
"Password:" 4 1 "$remote_ssh_password" 4 23 20 100 \
2018-03-02 20:17:02 +01:00
2> "$data"
2016-10-31 17:24:49 +01:00
fi
sel=$?
case $sel in
1) entering_remote_backups_done="yes";;
255) entering_remote_backups_done="yes";;
esac
2018-03-02 20:17:02 +01:00
remote_ssh_username=$(sed -n 1p < "$data")
remote_ssh_domain=$(sed -n 2p < "$data")
remote_ssh_port=$(sed -n 3p < "$data")
remote_ssh_password=$(sed -n 4p < "$data")
remote_ssh_reciprocal_username=$(sed -n 5p < "$data")
remote_ssh_reciprocal_password=$(sed -n 6p < "$data")
rm -f "$data"
2016-10-31 17:24:49 +01:00
if [[ $remote_ssh_username != "" && \
$remote_ssh_domain != "" && \
$remote_ssh_port != "" && \
$remote_ssh_password != "" ]]; then
2018-03-02 20:17:02 +01:00
if [ ${#remote_ssh_password} -lt "$MINIMUM_PASSWORD_LENGTH" ]; then
2016-10-31 17:24:49 +01:00
dialog --title "Password quality check" --msgbox "The password given was too short. It must be at least $MINIMUM_PASSWORD_LENGTH characters" 6 40
else
2016-10-31 17:24:49 +01:00
if [[ $RECIPROCAL == "yes" ]]; then
if [[ $remote_ssh_reciprocal_username != "" && \
$remote_ssh_reciprocal_password != "" ]]; then
2018-03-02 20:17:02 +01:00
if [ ${#remote_ssh_reciprocal_password} -lt "$MINIMUM_PASSWORD_LENGTH" ]; then
2016-10-31 17:24:49 +01:00
dialog --title "Password quality check" --msgbox "The reciprocal password given was too short. It must be at least $MINIMUM_PASSWORD_LENGTH characters" 6 40
else
2018-03-02 20:17:02 +01:00
echo "${remote_ssh_reciprocal_username}:${remote_ssh_reciprocal_password}::::/home/${remote_ssh_reciprocal_username}:bash" | newusers
echo "$remote_ssh_username@$remote_ssh_domain $remote_ssh_port /home/$remote_ssh_username $remote_ssh_password" >> "$FRIENDS_SERVERS_LIST"
2016-10-31 17:24:49 +01:00
remote_ssh_username=""
remote_ssh_domain=""
remote_ssh_port=""
remote_ssh_password=""
remote_ssh_reciprocal_username=""
remote_ssh_reciprocal_password=""
entering_remote_backups_ctr=$((entering_remote_backups_ctr + 1))
fi
else
2018-03-02 20:17:02 +01:00
echo "$remote_ssh_username@$remote_ssh_domain $remote_ssh_port /home/$remote_ssh_username $remote_ssh_password" >> "$FRIENDS_SERVERS_LIST"
2016-10-31 17:24:49 +01:00
remote_ssh_username=""
remote_ssh_domain=""
remote_ssh_port=""
remote_ssh_password=""
remote_ssh_reciprocal_username=""
remote_ssh_reciprocal_password=""
entering_remote_backups_ctr=$((entering_remote_backups_ctr + 1))
fi
else
2018-03-02 20:17:02 +01:00
echo "$remote_ssh_username@$remote_ssh_domain $remote_ssh_port /home/$remote_ssh_username $remote_ssh_password" >> "$FRIENDS_SERVERS_LIST"
2016-10-31 17:24:49 +01:00
remote_ssh_username=""
remote_ssh_domain=""
remote_ssh_port=""
remote_ssh_password=""
entering_remote_backups_ctr=$((entering_remote_backups_ctr + 1))
fi
2016-10-31 17:24:49 +01:00
fi
else
entering_remote_backups_done="yes"
fi
done
2018-03-02 20:17:02 +01:00
if [ -f "$FRIENDS_SERVERS_LIST" ]; then
chown "$MY_USERNAME":"$MY_USERNAME" "$FRIENDS_SERVERS_LIST"
2016-10-31 17:24:49 +01:00
fi
}
function show_result {
2016-10-31 17:24:49 +01:00
clear
2018-03-02 20:17:02 +01:00
if (( "$entering_remote_backups_ctr" < 2 )); then
2016-10-31 17:24:49 +01:00
echo $'No remote backup locations were specified'
exit 0
fi
2018-03-02 20:17:02 +01:00
if [ ! -f "$FRIENDS_SERVERS_LIST" ]; then
2016-10-31 17:24:49 +01:00
echo $"No remote backups list found: $FRIENDS_SERVERS_LIST"
exit 7358
fi
echo ''
echo $"Remote backups list: $FRIENDS_SERVERS_LIST"
echo ''
echo $'Contents:'
echo ''
2018-03-02 20:17:02 +01:00
cat "$FRIENDS_SERVERS_LIST"
2016-10-31 17:24:49 +01:00
echo ''
}
2018-03-02 20:17:02 +01:00
if [ ! "$FRIENDS_SERVERS_LIST" ]; then
2016-10-31 17:24:49 +01:00
FRIENDS_SERVERS_LIST=/home/$MY_USERNAME/backup.list
2015-12-22 22:00:15 +01:00
fi
interactive_config_remote_backups
show_result
exit 0