#!/bin/bash if [ "$(id -u)" != "0" ]; then echo -e "\033[31mThis script requires superuser rights.\033[0m" exit 0 fi trap 'echo -e "\033[31mfiles.sh: Something went wrong\033[0m"; exit 1' ERR set -e export DEBIAN_FRONTEND=noninteractive BACKUP_LIST=/data/$SRV_START_DIR/config/backups.cfg 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 echo 'check BACKUP_DIR...' mkdir -p "$BACKUP_DIR" echo 'Creating files backup...' # 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 +%d-%m-%Y_%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" echo 'Pack to tar.gz...' 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 - ERR echo "The backup of all specified directories has been completed successfully"