backup-server-ftp/install_backup.sh

149 lines
9.2 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
echo "
░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓██████▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓███████▓▒░
░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░
░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░
░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓███████▓▒░
░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░
░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░
░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░░▒▓██████▓▒░░▒▓█▓▒░░▒▓█▓▒░░▒▓██████▓▒░░▒▓█▓▒░
░▒▓█▓▒░▒▓███████▓▒░ ░▒▓███████▓▒░▒▓████████▓▒░▒▓██████▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░
░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░
░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░░▒▓██████▓▒░ ░▒▓█▓▒░ ░▒▓████████▓▒░▒▓█▓▒░ ░▒▓█▓▒░
░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░
░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░
░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓███████▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓████████▓▒░▒▓████████▓▒░
"
sleep 2
clear
# Проверка на выполнение скрипта с правами суперпользователя
if [ "$EUID" -ne 0 ]; then
echo "Error: the script must be run with superuser rights (sudo)."
exit 1
fi
WORK_DIR="/opt/adminlabs_backup"
BACKUP_SCRIPT="$WORK_DIR/backup.sh"
BACKUP_CONFIG="$WORK_DIR/backup.ini"
SCRIPT_URL="http://files.adminlabs.space/scripts/backup.sh"
CONFIG_URL="http://files.adminlabs.space/scripts/backup.ini"
# Создание рабочей директории, если она не существует
if [ ! -d "$WORK_DIR" ]; then
mkdir -p "$WORK_DIR" || { echo "Error: Failed to create directory $WORK_DIR"; exit 1; }
echo "Created directory $WORK_DIR."
else
echo "Directory $WORK_DIR already exists."
fi
# Функция для скачивания файла
download_file() {
local url="$1"
local dest="$2"
echo "Downloading $url..."
curl -o "$dest" "$url" || { echo "Error: Failed to download $url"; exit 1; }
}
# Проверка и копирование скрипта
if [[ -f "$BACKUP_SCRIPT" ]]; then
cp "$BACKUP_SCRIPT" "$WORK_DIR" || { echo "Error: Failed to move $BACKUP_SCRIPT to $WORK_DIR"; exit 1; }
else
download_file "$SCRIPT_URL" "$BACKUP_SCRIPT"
fi
# Проверка и копирование конфигурационного файла
if [[ -f "$BACKUP_CONFIG" ]]; then
cp "$BACKUP_CONFIG" "$WORK_DIR" || { echo "Error: Failed to move $BACKUP_CONFIG to $WORK_DIR"; exit 1; }
else
download_file "$CONFIG_URL" "$BACKUP_CONFIG"
fi
# Функция для редактирования файла backup.ini
edit_backup_config() {
clear
echo "Editing backup.ini..."
# Временный файл для редактирования
TEMP_FILE=$(mktemp)
# Чтение конфигурационного файла и отображение его содержимого
echo -e "\033[1;33m************************ Backup.ini ****************************\033[0m"
cat "$BACKUP_CONFIG"
echo -e "\n\033[1;33m************************ Backup.ini ****************************\033[0m"
echo -e "\n\n"
# Запрос на редактирование каждой секции
while true; do
echo -e "\033[1;34mWhich section would you like to edit? (B - Backup, F - FTP, S - SENT, Q - exit)\033[0m"
read -rp "Enter section >>> " section
case $section in
B)
echo "Editing [Backup] section:"
echo "Current settings:"
grep -A 5 "[Backup]" "$BACKUP_CONFIG"
echo "Enter new values (or press Enter to keep current):"
read -p "ENABLE_DIRS (true/false): " ENABLE_DIRS
read -p "ENABLE_FILES (true/false): " ENABLE_FILES
read -p "SKIP_FILES: " SKIP_FILES
read -p "SKIP_DIRS: " SKIP_DIRS
read -p "DIRS (comma separated): " DIRS
read -p "FILES (comma separated): " FILES
# Запись изменений во временный файл
sed "/[Backup]/,/^[/{s/ENABLE_DIRS=.*/ENABLE_DIRS=${ENABLE_DIRS:-true}/; s/ENABLE_FILES=.*/ENABLE_FILES=${ENABLE_FILES:-false}/; s/SKIP_FILES=.*/SKIP_FILES=${SKIP_FILES:-}/; s/SKIP_DIRS=.*/SKIP_DIRS=${SKIP_DIRS:-}/; s/DIRS=.*/DIRS=${DIRS:-/etc,/var/log}/; s/FILES=.*/FILES=${FILES:-/home/user/BashProject/backup.sh,/home/user/jet/restore.sh}/}" "$BACKUP_CONFIG" > "$TEMP_FILE"
mv "$TEMP_FILE" "$BACKUP_CONFIG"
;;
F)
echo "Editing [FTP] section:"
echo "Current settings:"
grep -A 4 "[FTP]" "$BACKUP_CONFIG"
read -p "FTP_SERVER: " FTP_SERVER
read -p "FTP_USER: " FTP_USER
read -p "FTP_PASS: " FTP_PASS
sed "/[FTP]/,/^[/{s/FTP_SERVER=.*/FTP_SERVER=${FTP_SERVER:-127.0.0.1}/; s/FTP_USER=.*/FTP_USER=${FTP_USER:-user}/; s/FTP_PASS=.*/FTP_PASS=${FTP_PASS:-password}/}" "$BACKUP_CONFIG" > "$TEMP_FILE"
mv "$TEMP_FILE" "$BACKUP_CONFIG"
;;
S)
echo "Editing [SENT] section:"
echo "Current settings:"
grep -A 6 "[SENT]" "$BACKUP_CONFIG"
read -p "ENABLE_SENT (true/false): " ENABLE_SENT
read -p "ENABLE_SENT_GOTIFY (true/false): " ENABLE_SENT_GOTIFY
read -p "ENABLE_SENT_TELEGRAM (true/false): " ENABLE_SENT_TELEGRAM
read -p "GOTIFY_URL: " GOTIFY_URL
read -p "GOTIFY_APP_TOKEN: " GOTIFY_APP_TOKEN
read -p "TELEGRAM_TOKEN: " TELEGRAM_TOKEN
read -p "CHAT_ID: " CHAT_ID
read -p 'TEXT_FINISH_MESSAGE: ' TEXT_FINISH_MESSAGE
sed "/[SENT]/,/^[/{s/ENABLE_SENT=.*/ENABLE_SENT=${ENABLE_SENT:-true}/; s/ENABLE_SENT_GOTIFY=.*/ENABLE_SENT_GOTIFY=${ENABLE_SENT_GOTIFY:-true}/; s/ENABLE_SENT_TELEGRAM=.*/ENABLE_SENT_TELEGRAM=${ENABLE_SENT_TELEGRAM:-true}/; s/GOTIFY_URL=.*/GOTIFY_URL=${GOTIFY_URL:-http://ваш_gotify_сервер}/; s/GOTIFY_APP_TOKEN=.*/GOTIFY_APP_TOKEN=${GOTIFY_APP_TOKEN:-0000000}/; s/TELEGRAM_TOKEN=.*/TELEGRAM_TOKEN=${TELEGRAM_TOKEN:-00000000}/; s/CHAT_ID=.*/CHAT_ID=${CHAT_ID:-00000000}/; s/TEXT_FINISH_MESSAGE=.*/TEXT_FINISH_MESSAGE="${TEXT_FINISH_MESSAGE:-Backup finished}"/" "$BACKUP_CONFIG" > "$TEMP_FILE"
mv "$TEMP_FILE" "$BACKUP_CONFIG"
;;
Q)
echo "Exiting configuration editor."
exit 0
;;
*)
echo "Invalid section. Please choose one of the following: Backup, FTP, SENT, END or exit."
;;
esac
done
rm -f "$TEMP_FILE"
}
# Запуск функции редактирования конфигурации
edit_backup_config
echo "Installation complete."