Compare commits

...

4 Commits

2 changed files with 273 additions and 1 deletions

View File

@@ -13,6 +13,7 @@ show_help(){
echo "" echo ""
echo "-- MODES --" echo "-- MODES --"
echo " access | control over remote access to the device (mac-based-filtering using nftables)" echo " access | control over remote access to the device (mac-based-filtering using nftables)"
echo " transfer | simplified file-transfer with backup-option"
exit 0 exit 0
} }
@@ -35,6 +36,19 @@ if [[ "$1" == "access" ]]; then
exit 0 exit 0
fi fi
if [[ "$1" == "transfer" ]]; then
shift
if [[ -x "./sub/transfer.sh" ]]; then
./sub/transfer.sh "$@"
else
echo "Command 'transfer' not found!" >&2
exit 1
fi
exit 0
fi
while getopts ":cyhv" opt; do while getopts ":cyhv" opt; do
case $opt in case $opt in
c) print_config ;; c) print_config ;;

258
src/sub/transfer.sh Executable file
View File

@@ -0,0 +1,258 @@
#!/bin/bash
check_rsync(){
if command -v rsync &>/dev/null
then
echo "rsync is installed. Check passed. Bye."
HAD_BEST_SUCCESS="true"
else
echo "Please install rsync."
while [[ -z "$HAD_BEST_SUCCESS" ]]; do
read -p "Install now? [y/N] " QUESTION
if [[ -z "$QUESTION" ]] || [[ "$QUESTION" = "n" ]] || [[ "$QUESTION" = "N" ]]; then
echo "Alright, bye."
HAD_BEST_SUCCESS="true"
fi
if [[ "$QUESTION" = "y" ]] || [[ "$QUESTION" = "Y" ]]; then
HAD_BEST_SUCCESS="true"
echo "Trying to install rsync..."
if command -v apt &>/dev/null
then
sudo apt update
sudo apt install rsync -y
echo "Attempted to install rsync. Please execute the check again. Bye."
fi
if command -v dnf &>/dev/null
then
sudo dnf update
sudo dnf install rsync -y
echo "Attempted to install rsync. Please execute the check again. Bye."
fi
if command -v pacman &>/dev/null
then
sudo pacman -S rsync
echo "Attempted to install rsync. Please execute the check again. Bye."
fi
fi
done
fi
}
check_openssl(){
if command -v openssl &>/dev/null
then
echo "openssl is installed. Check passed. Bye."
HAD_BEST_SUCCESS_O="true"
else
echo "Please install openssl."
while [[ -z "$HAD_BEST_SUCCESS_O" ]]; do
read -p "Install now? [y/N] " QUESTION
if [[ -z "$QUESTION" ]] || [[ "$QUESTION" = "n" ]] || [[ "$QUESTION" = "N" ]]; then
echo "Alright, bye."
HAD_BEST_SUCCESS_O="true"
fi
if [[ "$QUESTION" = "y" ]] || [[ "$QUESTION" = "Y" ]]; then
HAD_BEST_SUCCESS_O="true"
echo "Trying to install openssl..."
if command -v apt &>/dev/null
then
sudo apt update
sudo apt install openssl -y
echo "Attempted to install openssl. Please execute the check again. Bye."
fi
if command -v dnf &>/dev/null
then
sudo dnf update
sudo dnf install openssl -y
echo "Attempted to install openssl. Please execute the check again. Bye."
fi
if command -v pacman &>/dev/null
then
sudo pacman -S openssl
echo "Attempted to install openssl. Please execute the check again. Bye."
fi
fi
done
fi
}
check_borg(){
if command -v borg &>/dev/null
then
echo "borg is installed. Check passed. Bye."
HAD_BEST_SUCCESS_B="true"
else
echo "Please install borg."
while [[ -z "$HAD_BEST_SUCCESS_B" ]]; do
read -p "Install now? [y/N] " QUESTION
if [[ -z "$QUESTION" ]] || [[ "$QUESTION" = "n" ]] || [[ "$QUESTION" = "N" ]]; then
echo "Alright, bye."
HAD_BEST_SUCCESS_B="true"
fi
if [[ "$QUESTION" = "y" ]] || [[ "$QUESTION" = "Y" ]]; then
HAD_BEST_SUCCESS_B="true"
echo "Trying to install borg..."
if command -v apt &>/dev/null
then
sudo apt update
sudo apt install borg -y
echo "Attempted to install borg. Please execute the check again. Bye."
fi
if command -v dnf &>/dev/null
then
sudo dnf update
sudo dnf install borg -y
echo "Attempted to install borg. Please execute the check again. Bye."
fi
if command -v pacman &>/dev/null
then
sudo pacman -S borg
echo "Attempted to install borg. Please execute the check again. Bye."
fi
fi
done
fi
}
backup_encrypt(){
echo "Creating encrypted backup..."
local GEN_PASSPHRASE="$1" # "gen" oder leer
if [[ -z "$SOURCE" || -z "$DESTINATION" ]]; then
echo "SOURCE and DESTINATION must be set"
return 1
fi
if [[ ! -e "$SOURCE" ]]; then
echo "Source does not exist: $SOURCE"
return 1
fi
local PASSPHRASE=""
if [[ "$GEN_PASSPHRASE" == "gen" ]]; then
PASSPHRASE="$(openssl rand -base64 48)"
echo "======================================================"
echo "BORG PASSPHRASE (please note down)"
echo
echo "$PASSPHRASE"
echo
echo "The phrase won't be shown again and will not be saved."
echo "======================================================"
echo
fi
local ARCHIVE_NAME
ARCHIVE_NAME="backup-$(date +%F_%H-%M-%S)"
BORG_PASSPHRASE="$PASSPHRASE" \
borg create \
--stats \
--progress \
--compression lz4 \
"$DESTINATION::$ARCHIVE_NAME" \
"$SOURCE"
local RC=$?
unset BORG_PASSPHRASE
if [[ $RC -eq 0 ]]; then
echo "Backup successful: $ARCHIVE_NAME"
else
echo "Backup failed (exit code $RC)"
fi
exit 0;
return $RC
}
backup(){
if [[ -z "$ENCRYPT" ]]; then
backup_encrypt
exit 0;
fi
echo "Creating unencrypted backup..."
if [[ -z "$SOURCE" || -z "$DESTINATION" ]]; then
echo "SOURCE and DESTINATION must be set"
return 1
fi
if [[ ! -e "$SOURCE" ]]; then
echo "Source does not exist: $SOURCE"
return 1
fi
local ARCHIVE_NAME
ARCHIVE_NAME="backup-$(date +%F_%H-%M-%S)"
borg create \
--stats \
--progress \
--compression lz4 \
"$DESTINATION::$ARCHIVE_NAME" \
"$SOURCE"
local RC=$?
if [[ $RC -eq 0 ]]; then
echo "Unencrypted backup successful: $ARCHIVE_NAME"
else
echo "Backup failed (exit code $RC)"
fi
return $RC
}
transfer(){
exit 0;
}
show_help(){
echo "-- HELP --"
echo " -y | Answer all questions with yes"
echo " -s SOURCE | Source path (e.g. /srv/fileshare/main/)"
echo " -d DESTINATION | Destination path (e.g. using ssh)"
echo " -b | Transfer as backup"
echo " -e | encrypt destination using a passphrase"
echo " -h | Show this help text"
echo " -v | Get Version Info"
echo " -c | Check the usability of this tool"
exit 0
}
while getopts ":s:d:Bvhyce" opt; do
case $opt in
s) SOURCE="$OPTARG";;
d) DESTINATION="$OPTARG";;
v) exit 0 ;;
c) check_rsync; check_borg; check_openssl; exit 0 ;;
e) ENCRYPT="1" ;;
B) backup ;;
h) show_help ;;
y) ALWAYS_YES="1" ;;
\?) echo "Unknown option: -$OPTARG" >&2; exit 1 ;;
:) echo "Option -$OPTARG needs an argument" >&2; exit 1 ;;
esac
done
echo "Nothing to do."
exit 0