#!/bin/bash if [ "$(id -u)" != "0" ]; then echo -e "\033[31mThis script requires superuser rights.\033[0m" exit 0 fi trap 'echo -e "\033[31mSomething went wrong\033[0m"; exit 1' EXIT set -e export DEBIAN_FRONTEND=noninteractive BACKUP_LIST="./backups.list" BACKUP_DIR="/backups/files" # Checking the presence of a backups.list if [ ! -f "$BACKUP_LIST" ]; then echo "List file not found: $BACKUP_LIST" exit 1 fi # Creating main directories (if not created) mkdir -p "$BACKUP_DIR" # Reading a list of directories for backup while IFS= read -r DIR; do # Ignore lines starting with # if [[ "$DIR" =~ ^#.* ]]; then continue fi # Let's make sure the line is not empty if [ -n "$DIR" ]; then # Let's make sure the directory exists if [ ! -d "$DIR" ]; then echo "Directory not found: $DIR" continue fi # Formation of the archive name BASENAME=$(basename "$DIR") TIMESTAMP=$(date +%Y%m%d%H%M%S) ARCHIVE_NAME="${BASENAME}_${TIMESTAMP}.tar.gz" # Creating a directory to store backups of this folder BACKUP_FOLDER="${BACKUP_DIR}/${BASENAME}" mkdir -p "$BACKUP_FOLDER" # Creating an archive tar -czf "${BACKUP_FOLDER}/${ARCHIVE_NAME}" -C "$(dirname "$DIR")" "$BASENAME" # Checking the success of the backup if [ $? -eq 0 ]; then echo "Backup successfully created: ${BACKUP_FOLDER}/${ARCHIVE_NAME}" else echo "Error when creating a backup: ${BACKUP_FOLDER}/${ARCHIVE_NAME}" fi fi done < "$BACKUP_LIST" trap - EXIT echo "The backup of all specified directories has been completed successfully."