From 5151220a6a4a9c4b32218cec7abc26673f8503da Mon Sep 17 00:00:00 2001 From: stirelshka8 Date: Tue, 28 Jan 2025 13:45:30 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=20=D1=80=D0=B5=D0=B4=D0=B0=D0=BA=D1=82=D0=B8=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BE=D0=BD=D1=84?= =?UTF-8?q?=D0=B8=D0=B3=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backup.sh | 21 +++++-- install.sh | 48 --------------- install_backup.sh | 148 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+), 54 deletions(-) delete mode 100644 install.sh create mode 100755 install_backup.sh diff --git a/backup.sh b/backup.sh index 5f60de6..d692f8e 100755 --- a/backup.sh +++ b/backup.sh @@ -125,12 +125,20 @@ if [[ "$ENABLE_DIRS" == "true" ]]; then IFS=',' read -r -a DIR_ARRAY <<< "$DIRECTORIES" for DIR in "${DIR_ARRAY[@]}"; do # Проверка на исключение директорий - if [[ " ${SKIP_DIR_ARRAY[@]} " =~ " $DIR " ]]; then + if [[ " ${SKIP_DIR_ARRAY[*]} " =~ " $DIR " ]]; then echo "Directory $DIR is in the skip list, skipping." >> "$LOG_FILE" continue fi if [ -d "$DIR" ]; then + # Проверка наличия файлов и директорий для исключения в текущей директории + for EXCLUDED in "${SKIP_DIR_ARRAY[@]}"; do + if [ -e "$DIR/$EXCLUDED" ]; then + echo "Excluded item $EXCLUDED found in directory $DIR, skipping backup." >> "$LOG_FILE" + continue 2 # Пропускаем всю директорию, если найдено исключение + fi + done + cp -r "$DIR" "$TEMP_DIR/" && echo "Copy directory $DIR completed." >> "$LOG_FILE" || echo "Error while copying directory $DIR." >> "$LOG_FILE" else echo "Directory $DIR not found, skipping." >> "$LOG_FILE" @@ -146,7 +154,7 @@ if [[ "$ENABLE_FILES" == "true" ]]; then IFS=',' read -r -a FILE_ARRAY <<< "$FILES" for FILE in "${FILE_ARRAY[@]}"; do # Проверка на исключение файлов - if [[ " ${SKIP_FILE_ARRAY[@]} " =~ " $FILE " ]]; then + if [[ " ${SKIP_FILE_ARRAY[*]} " =~ " $FILE " ]]; then echo "File $FILE is in the skip list, skipping." >> "$LOG_FILE" continue fi @@ -158,11 +166,12 @@ if [[ "$ENABLE_FILES" == "true" ]]; then fi done else - echo "Backup of files is disabled." + echo "Backup of files is disabled." >> "$LOG_FILE" fi + # Упаковываем временную директорию в tar.gz архив -tar -czf "$WORK_DIR/$TAR_NAME" -C "$TEMP_DIR" . && echo "Created tar.gz archive: $(pwd)/$TAR_NAME" || { echo "Error while creating tar.gz archive."; rm -rf "$TEMP_DIR"; exit 1; } +tar -czf "$WORK_DIR/$TAR_NAME" -C "$TEMP_DIR" . && echo "Created tar.gz archive: $TAR_NAME" || { echo "Error while creating tar.gz archive."; rm -rf "$TEMP_DIR"; exit 1; } # Проверяем наличие файлов if [[ ! -f "$TAR_NAME" ]]; then @@ -200,9 +209,9 @@ fi # Удаление временной директории и архива после завершения работы скрипта rm -rf "$TEMP_DIR" || { echo "Error while deleting temp directory."} echo "Temp directory deleted." -rm "$TAR_NAME" || { echo "Error while deleting tar.gz archive."} +rm "$WORK_DIR/$TAR_NAME" || { echo "Error while deleting tar.gz archive."} echo "Tar.gz archive deleted." -rm "$LOG_FILE" || { echo "Error while deleting log file."} +rm "$WORK_DIR/$LOG_FILE" || { echo "Error while deleting log file."} echo "Log file deleted." echo "Sending message..." diff --git a/install.sh b/install.sh deleted file mode 100644 index f103135..0000000 --- a/install.sh +++ /dev/null @@ -1,48 +0,0 @@ -#!/bin/bash - -# Проверка на выполнение скрипта с правами суперпользователя -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 - mv "$BACKUP_SCRIPT" "$WORK_DIR" || { echo "Error: Failed to move $BACKUP_SCRIPT to $WORK_DIR"; exit 1; } -else - download_file "$SCRIPT_URL" "$BACKUP_SCRIPT" - mv "$BACKUP_SCRIPT" "$WORK_DIR" || { echo "Error: Failed to move $BACKUP_SCRIPT to $WORK_DIR"; exit 1; } -fi - -# Проверка и копирование конфигурационного файла -if [[ -f "$BACKUP_CONFIG" ]]; then - mv "$BACKUP_CONFIG" "$WORK_DIR" || { echo "Error: Failed to move $BACKUP_CONFIG to $WORK_DIR"; exit 1; } -else - download_file "$CONFIG_URL" "$BACKUP_CONFIG" - mv "$BACKUP_CONFIG" "$WORK_DIR" || { echo "Error: Failed to move $BACKUP_CONFIG to $WORK_DIR"; exit 1; } -fi - -echo "Installation complete." diff --git a/install_backup.sh b/install_backup.sh new file mode 100755 index 0000000..d749906 --- /dev/null +++ b/install_backup.sh @@ -0,0 +1,148 @@ +#!/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." +