diff --git a/backup.sh b/backup.sh index 03df46a..5f60de6 100755 --- a/backup.sh +++ b/backup.sh @@ -1,21 +1,29 @@ #!/bin/bash -CONFIG_DIR="backup.ini" -LOG_FILE="backup_log_$(date +"%Y-%m-%d_%H-%M-%S").txt" +#Author: stirelshka8 +#Description: Backup script +#License: MIT +#Version: 0.0.1test + +WORK_DIR="/opt/adminlabs_backup" +CONFIG_DIR="$WORK_DIR/backup.ini" +LOG_FILE="$WORK_DIR/backup_log_$(date +"%Y-%m-%d_%H-%M-%S").txt" + +mkdir -p "$WORK_DIR" # Инициализация логирования exec &> >(tee -a "$LOG_FILE") # Проверка на выполнение скрипта с правами суперпользователя if [ "$EUID" -ne 0 ]; then - echo "Error: the script must be run with superuser rights (sudo)." + echo "Error: the script must be run with superuser rights (sudo)." >> "$LOG_FILE" exit 1 fi # Проверка наличия необходимых утилит for cmd in tar ftp lftp curl; do if ! command -v "$cmd" &> /dev/null; then - echo "Error: $cmd is not installed." + echo "Error: $cmd is not installed." >> "$LOG_FILE" exit 1 fi done @@ -49,7 +57,7 @@ get_ini_value() { fi done < "$ini_file" - echo "Error: Key '$key' not found in section '$section'." + echo "Error: Key '$key' not found in section '$section'." >> "$LOG_FILE" exit 1 } @@ -74,7 +82,7 @@ get_sent_message() { -H "Content-Type: application/json" -d "{"title":$title,"message":$escaped_message,"priority":5}") if [[ "$response" -ne 200 ]]; then - echo "Gotify error: $response" + echo "Gotify error: $response" >> "$LOG_FILE" fi fi @@ -82,7 +90,7 @@ get_sent_message() { response=$(curl -s -o /dev/null -w "%{http_code}" -X POST "https://api.telegram.org/bot$TELEGRAM_TOKEN/sendMessage" -d "chat_id=$CHAT_ID&text=$title: $message") if [[ "$response" -ne 200 ]]; then - echo "Telegram error: $response" + echo "Telegram error: $response" >> "$LOG_FILE" fi fi fi @@ -105,7 +113,7 @@ FTP_PASS="$(get_ini_value "FTP" "FTP_PASS")" # Создаем временную директорию mkdir -p "$TEMP_DIR" -echo "Create temp directory: $TEMP_DIR" +echo "Create temp directory: $TEMP_DIR" >> "$LOG_FILE" # Преобразуем списки исключений в массивы IFS=',' read -r -a SKIP_FILE_ARRAY <<< "$SKIP_FILES" @@ -118,35 +126,35 @@ if [[ "$ENABLE_DIRS" == "true" ]]; then for DIR in "${DIR_ARRAY[@]}"; do # Проверка на исключение директорий if [[ " ${SKIP_DIR_ARRAY[@]} " =~ " $DIR " ]]; then - echo "Directory $DIR is in the skip list, skipping." + echo "Directory $DIR is in the skip list, skipping." >> "$LOG_FILE" continue fi if [ -d "$DIR" ]; then - cp -r "$DIR" "$TEMP_DIR/" && echo "Copy directory $DIR completed." || echo "Error while copying directory $DIR." + 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." + echo "Directory $DIR not found, skipping." >> "$LOG_FILE" fi done else - echo "Backup of directories is disabled." + echo "Backup of directories is disabled." >> "$LOG_FILE" fi # Проверка, включено ли резервное копирование файлов -echo "Enable backup of files: $ENABLE_FILES" +echo "Enable backup of files: $ENABLE_FILES" >> "$LOG_FILE" if [[ "$ENABLE_FILES" == "true" ]]; then IFS=',' read -r -a FILE_ARRAY <<< "$FILES" for FILE in "${FILE_ARRAY[@]}"; do # Проверка на исключение файлов if [[ " ${SKIP_FILE_ARRAY[@]} " =~ " $FILE " ]]; then - echo "File $FILE is in the skip list, skipping." + echo "File $FILE is in the skip list, skipping." >> "$LOG_FILE" continue fi if [ -f "$FILE" ]; then - cp "$FILE" "$TEMP_DIR/" && echo "Copy file $FILE completed." || echo "Error while copying file $FILE." + cp "$FILE" "$TEMP_DIR/" && echo "Copy file $FILE completed." >> "$LOG_FILE" || echo "Error while copying file $FILE." >> "$LOG_FILE" else - echo "File $FILE not found, skipping." + echo "File $FILE not found, skipping." >> "$LOG_FILE" fi done else @@ -154,7 +162,7 @@ else fi # Упаковываем временную директорию в tar.gz архив -tar -czf "$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: $(pwd)/$TAR_NAME" || { echo "Error while creating tar.gz archive."; rm -rf "$TEMP_DIR"; exit 1; } # Проверяем наличие файлов if [[ ! -f "$TAR_NAME" ]]; then @@ -168,15 +176,15 @@ if [[ ! -f "$LOG_FILE" ]]; then fi # Загружаем файлы на FTP-сервер -echo "Sending files ..." +echo "Sending files ..." >> "$LOG_FILE" lftp -u "$FTP_USER","$FTP_PASS" "$FTP_SERVER" < ftp_log.txt 2>&1 set ssl:verify-certificate no set net:timeout 10 set net:max-retries 3 set net:reconnect-interval-base 5 set net:reconnect-interval-multiplier 2 -put "/$TAR_NAME" -put "/$LOG_FILE" +put "$WORK_DIR/$TAR_NAME" +put "$WORK_DIR/$LOG_FILE" bye EOF @@ -185,6 +193,8 @@ if [ $? -ne 0 ]; then echo "Error while sending files." rm "$TAR_NAME" exit 1 +else + echo "Files sent successfully." fi # Удаление временной директории и архива после завершения работы скрипта diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..f103135 --- /dev/null +++ b/install.sh @@ -0,0 +1,48 @@ +#!/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."