diff --git a/acme.sh b/acme.sh index 7147ad8..0a4ea7c 100644 --- a/acme.sh +++ b/acme.sh @@ -5,20 +5,24 @@ if [ "$(id -u)" != "0" ]; then exit 0 fi -ACME_FILE="/data/secrets/$SERVER_DOMAIN/letsencrypt/$SERVER_DOMAIN.acme.json" -BACKUP_DIR="/backups/acme" +LE_DIR="/data/secrets/$SERVER_DOMAIN/letsencrypt" +BACKUP_DIR="/backups/letsencrypt" ARCHIVE_FILE="$BACKUP_DIR/$SERVER_DOMAIN.tar.gz" # load ACME_BACKUP_PASSWORD SECRETS_PATH=/data/secrets/$SERVER_DOMAIN/$SERVER_DOMAIN.env -source $SECRETS_PATH -trap 'echo -e "\033[31mSomething went wrong\033[0m"; exit 1' EXIT +trap 'echo -e "\033[31macme.sh: Something went wrong\033[0m"; exit 1' EXIT set -e export DEBIAN_FRONTEND=noninteractive -tar -czf - -C "$ACME_FILE" . | openssl enc -aes-256-cbc -e -out "$ARCHIVE_FILE" -pass pass:"$ACME_BACKUP_PASSWORD" -pbkdf2 -iter 100000 +source $SECRETS_PATH + +echo 'check BACKUP_DIR...' +mkdir -p "$BACKUP_DIR" + +tar -czf - -C "$LE_DIR" . | openssl enc -aes-256-cbc -e -out "$ARCHIVE_FILE" -pass pass:"$ACME_BACKUP_PASSWORD" -pbkdf2 -iter 100000 trap - EXIT -echo "All backups were made successfully. Outdated backups have been deleted" \ No newline at end of file +echo "$LE_DIR backuped successfully to $ARCHIVE_FILE" \ No newline at end of file diff --git a/files-all.sh b/files-all.sh new file mode 100644 index 0000000..0ca02cb --- /dev/null +++ b/files-all.sh @@ -0,0 +1,18 @@ +#!/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' ERR +set -e + +export DEBIAN_FRONTEND=noninteractive + +bash files.sh +bash acme.sh +bash cleaner.sh + +trap - ERR +echo "All files backups were made successfully. Outdated backups have been deleted" \ No newline at end of file diff --git a/files.sh b/files.sh index 23f02f9..1d12757 100644 --- a/files.sh +++ b/files.sh @@ -5,12 +5,12 @@ if [ "$(id -u)" != "0" ]; then exit 0 fi -trap 'echo -e "\033[31mSomething went wrong\033[0m"; exit 1' EXIT +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/backups.cfg +BACKUP_LIST=/data/$SRV_START_DIR/config/backups.cfg BACKUP_DIR="/backups/files" # Checking the presence of a backups.list @@ -19,7 +19,7 @@ if [ ! -f "$BACKUP_LIST" ]; then exit 1 fi -# Creating main directories (if not created) +echo 'check BACKUP_DIR...' mkdir -p "$BACKUP_DIR" echo 'Creating files backup...' @@ -41,7 +41,6 @@ while IFS= read -r DIR; do # Formation of the archive name BASENAME=$(basename "$DIR") - #TIMESTAMP=$(date +%Y%m%d%H%M%S) TIMESTAMP=$(date +%d-%m-%Y_%H%M%S) ARCHIVE_NAME="${BASENAME}_${TIMESTAMP}.tar.gz" @@ -49,7 +48,7 @@ while IFS= read -r DIR; do BACKUP_FOLDER="${BACKUP_DIR}/${BASENAME}" mkdir -p "$BACKUP_FOLDER" - # Creating an archive + echo 'Pack to tar.gz...' tar -czf "${BACKUP_FOLDER}/${ARCHIVE_NAME}" -C "$(dirname "$DIR")" "$BASENAME" # Checking the success of the backup @@ -61,5 +60,5 @@ while IFS= read -r DIR; do fi done < "$BACKUP_LIST" -trap - EXIT -echo "The backup of all specified directories has been completed successfully." \ No newline at end of file +trap - ERR +echo "The backup of all specified directories has been completed successfully" \ No newline at end of file diff --git a/mariadb-all.sh b/mariadb-all.sh new file mode 100644 index 0000000..ce8f9eb --- /dev/null +++ b/mariadb-all.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +if [ "$(id -u)" != "0" ]; then + echo -e "\033[31mThis script requires superuser rights\033[0m" + exit 0 +fi + +SECRETS_PATH=/data/secrets/$SERVER_DOMAIN/$SERVER_DOMAIN.env + +trap 'echo -e "\033[31mmariadb-all: Something went wrong\033[0m"; exit 1' ERR +set -e + +export DEBIAN_FRONTEND=noninteractive + +source $SECRETS_PATH + +if [ -z "$MARIADB_ROOT_PASSWORD" ]; then + echo "Error: MARIADB_ROOT_PASSWORD is not set or is empty" + exit 1 +fi + +DOCKER_SERVICE_NAME=mariadb +MYSQL_USER=root +MYSQL_DATABASE=mysql + +docker exec -i $DOCKER_SERVICE_NAME mariadb -u$MYSQL_USER -p$MARIADB_ROOT_PASSWORD $MYSQL_DATABASE -N -e " + SELECT s.SCHEMA_NAME + FROM information_schema.SCHEMATA s + WHERE SCHEMA_NAME NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys'); +" | while IFS= read -r dbname; do + bash mariadb.sh $dbname +done + +trap - ERR +echo "All mariadb databases backuped successfully" \ No newline at end of file diff --git a/mariadb.sh b/mariadb.sh new file mode 100644 index 0000000..8a4268e --- /dev/null +++ b/mariadb.sh @@ -0,0 +1,61 @@ +#!/bin/bash + +if [ "$(id -u)" != "0" ]; then + echo -e "\033[31mThis script requires superuser rights\033[0m" + exit 0 +fi + +if [ -z "$1" ]; then + echo "Please provide the MYSQL_DB as argument" + exit 1 +fi + +SECRETS_PATH=/data/secrets/$SERVER_DOMAIN/$SERVER_DOMAIN.env + +trap 'echo -e "\033[31mmariadb.sh: Something went wrong\033[0m"; exit 1' ERR +set -e + +export DEBIAN_FRONTEND=noninteractive + +source $SECRETS_PATH + +if [ -z "$MARIADB_ROOT_PASSWORD" ]; then + echo "Error: MARIADB_ROOT_PASSWORD is not set or is empty" + exit 1 +fi + +MYSQL_DB="$1" +MYSQL_USER=root + +CURRENT_MONTH=$(date +%Y-%m) +CURRENT_DATE=$(date +%Y-%m-%d) +CURRENT_DATETIME=$(date +%d-%m-%Y_%H%M%S) +BACKUPS_PATH=/backups/mariadb +DOCKER_SERVICE_NAME=mariadb + +BACKUP_FOLDER=$BACKUPS_PATH/$PGDB + +echo 'check BACKUP_FOLDER...' +mkdir -p "$BACKUP_FOLDER" + +echo 'Creating mariadb backups...' +cd "$BACKUP_FOLDER" +if [ -f 'dump_'"$MYSQL_DB"'.sql' ]; then + rm 'dump_'"$MYSQL_DB"'.sql' +fi +DB_BACKUP_FILENAME=$MYSQL_DB'_'$CURRENT_DATETIME'.tar.gz' + +echo 'Exec mysqldump...' +docker exec -t $DOCKER_SERVICE_NAME mysqldump -u $MYSQL_USER -p$MARIADB_ROOT_PASSWORD $MYSQL_DB > 'dump_'"$MYSQL_DB"'.sql' + +echo 'Pack to tar.gz...' +tar -cf - 'dump_'"$MYSQL_DB"'.sql' | gzip -9 > "$DB_BACKUP_FILENAME" +rm 'dump_'"$MYSQL_DB"'.sql' + +cd "$BACKUP_FOLDER" + +echo 'Write md5 sum...' +md5sum * > MD5SUMS + +trap - ERR +echo 'The backup of the '"$MYSQL_DB"' database was completed successfully' \ No newline at end of file diff --git a/all.sh b/postgres-all.sh similarity index 70% rename from all.sh rename to postgres-all.sh index e40d838..3aa68a5 100644 --- a/all.sh +++ b/postgres-all.sh @@ -5,7 +5,7 @@ if [ "$(id -u)" != "0" ]; then exit 0 fi -trap 'echo -e "\033[31mSomething went wrong\033[0m"; exit 1' EXIT +trap 'echo -e "\033[31mpostgres-all.sh: Something went wrong\033[0m"; exit 1' ERR set -e export DEBIAN_FRONTEND=noninteractive @@ -20,9 +20,5 @@ while IFS=, read -r dbname owner; do bash postgres.sh "$dbname" "$owner" done -bash files.sh -bash acme.sh -bash cleaner.sh - -trap - EXIT -echo "All backups were made successfully. Outdated backups have been deleted" \ No newline at end of file +trap - ERR +echo "All postgres databases backuped successfully" \ No newline at end of file diff --git a/postgres.sh b/postgres.sh index 5138fd1..08ece96 100644 --- a/postgres.sh +++ b/postgres.sh @@ -10,42 +10,44 @@ if [ -z "$1" ] || [ -z "$2" ]; then exit 1 fi -trap 'echo -e "\033[31mSomething went wrong\033[0m"; exit 1' EXIT +trap 'echo -e "\033[31mpostgres.sh: Something went wrong\033[0m"; exit 1' ERR set -e export DEBIAN_FRONTEND=noninteractive -# configuration settings PGDB="$1" PGUSER="$2" CURRENT_MONTH=$(date +%Y-%m) CURRENT_DATE=$(date +%Y-%m-%d) -#CURRENT_DATETIME=$(date +%d-%b-%Y_%H_%M_%Z) CURRENT_DATETIME=$(date +%d-%m-%Y_%H%M%S) BACKUPS_PATH=/backups/postgres DOCKER_SERVICE_NAME=postgres -# backup PostgreSQL database BACKUP_FOLDER=$BACKUPS_PATH/$PGDB -if [ ! -d "$BACKUP_FOLDER" ]; then - mkdir -p "$BACKUP_FOLDER" -fi + +echo 'check BACKUP_FOLDER...' +mkdir -p "$BACKUP_FOLDER" + echo 'Creating PostgreSQL backups...' cd "$BACKUP_FOLDER" if [ -f 'dump_'"$PGDB"'.sql' ]; then rm 'dump_'"$PGDB"'.sql' fi -db_backup_filename=$PGDB'_'$CURRENT_DATETIME'.tar.gz' +DB_BACKUP_FILENAME=$PGDB'_'$CURRENT_DATETIME'.tar.gz' +echo 'Exec pg_dump...' docker exec -t postgres pg_dump -U $PGUSER $PGDB > 'dump_'"$PGDB"'.sql' -tar -cf - 'dump_'"$PGDB"'.sql' | gzip -9 > "$db_backup_filename" +echo 'Pack to tar.gz...' +tar -cf - 'dump_'"$PGDB"'.sql' | gzip -9 > "$DB_BACKUP_FILENAME" rm 'dump_'"$PGDB"'.sql' cd "$BACKUP_FOLDER" + +echo 'Write md5 sum...' md5sum * > MD5SUMS -trap - EXIT +trap - ERR echo 'The backup of the '"$PGDB"' database was completed successfully' \ No newline at end of file