2020-10-13 13:32:08 +02:00
|
|
|
#!/bin/bash
|
2023-06-28 21:25:15 +02:00
|
|
|
|
|
|
|
# Check minimum number of arguments
|
|
|
|
if [ $# -lt 3 ]; then
|
|
|
|
echo "ERROR: Not enough arguments. Please provide at least a volume name, backup hash, and version."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2024-01-11 03:04:13 +01:00
|
|
|
volume_name="$1" # Volume-Name
|
|
|
|
backup_hash="$2" # Hashed Machine ID
|
2024-01-11 10:58:35 +01:00
|
|
|
version="$3" # version to recover
|
2024-01-11 03:04:13 +01:00
|
|
|
|
|
|
|
# DATABASE PARAMETERS
|
|
|
|
database_type="$4" # Valid values; mariadb, postgress
|
|
|
|
database_container="$5" # optional
|
|
|
|
database_password="$6" # optional
|
|
|
|
database_name="$7" # optional
|
|
|
|
database_user="$database_name"
|
|
|
|
|
|
|
|
|
2023-11-16 23:02:09 +01:00
|
|
|
backup_folder="Backups/$backup_hash/backup-docker-to-local/$version/$volume_name"
|
2022-08-31 19:04:42 +02:00
|
|
|
backup_files="/$backup_folder/files"
|
2024-01-11 03:04:13 +01:00
|
|
|
backup_sql="/$backup_folder/sql/$database_name.backup.sql"
|
2023-06-28 21:25:15 +02:00
|
|
|
|
2024-01-11 11:04:03 +01:00
|
|
|
# DATABASE RECOVERY
|
2023-06-28 21:25:15 +02:00
|
|
|
|
2024-01-12 15:42:51 +01:00
|
|
|
if [ ! -z "$database_type" ]; then
|
2024-01-12 11:47:46 +01:00
|
|
|
if [ "$database_type" = "postgres" ]; then
|
|
|
|
if [ -n "$database_container" ] && [ -n "$database_password" ] && [ -n "$database_name" ]; then
|
|
|
|
echo "Recover PostgreSQL dump"
|
|
|
|
export PGPASSWORD="$database_password"
|
|
|
|
cat "$backup_sql" | docker exec -i "$database_container" psql -U "$database_user" -d "$database_name"
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo "ERROR: Failed to recover PostgreSQL dump"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
elif [ "$database_type" = "mariadb" ]; then
|
|
|
|
if [ -n "$database_container" ] && [ -n "$database_password" ] && [ -n "$database_name" ]; then
|
|
|
|
echo "recover mysql dump"
|
|
|
|
cat "$backup_sql" | docker exec -i "$database_container" mariadb -u "$database_user" --password="$database_password" "$database_name"
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo "ERROR: Failed to recover mysql dump"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
exit 0
|
2023-06-28 21:25:15 +02:00
|
|
|
fi
|
2023-03-27 01:49:58 +02:00
|
|
|
fi
|
2024-01-11 03:04:13 +01:00
|
|
|
echo "A database backup exists, but a parameter is missing."
|
|
|
|
exit 1
|
2024-01-12 11:47:46 +01:00
|
|
|
fi
|
2023-06-28 21:22:02 +02:00
|
|
|
|
2024-01-11 11:04:03 +01:00
|
|
|
# FILE RECOVERY
|
|
|
|
|
|
|
|
echo "Inspect volume $volume_name"
|
|
|
|
docker volume inspect "$volume_name"
|
|
|
|
exit_status_volume_inspect=$?
|
|
|
|
|
2024-01-11 10:58:35 +01:00
|
|
|
if [ $exit_status_volume_inspect -eq 0 ]; then
|
|
|
|
echo "Volume $volume_name already exists"
|
|
|
|
else
|
|
|
|
echo "Create volume $volume_name"
|
|
|
|
docker volume create "$volume_name"
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo "ERROR: Failed to create volume $volume_name"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2023-06-28 21:22:02 +02:00
|
|
|
if [ -d "$backup_files" ]; then
|
|
|
|
echo "recover files"
|
|
|
|
docker run --rm -v "$volume_name:/recover/" -v "$backup_files:/backup/" "kevinveenbirkenbach/alpine-rsync" sh -c "rsync -avv --delete /backup/ /recover/"
|
2023-06-28 21:25:15 +02:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo "ERROR: Failed to recover files"
|
|
|
|
exit 1
|
|
|
|
fi
|
2023-06-28 21:32:09 +02:00
|
|
|
exit 0
|
2023-06-28 21:25:15 +02:00
|
|
|
else
|
|
|
|
echo "ERROR: $backup_files doesn't exist"
|
|
|
|
exit 1
|
2021-11-11 13:03:19 +01:00
|
|
|
fi
|
2023-06-28 21:22:02 +02:00
|
|
|
|
2023-06-28 21:25:15 +02:00
|
|
|
echo "ERROR: Unhandled case"
|
|
|
|
exit 1
|