mirror of
https://github.com/kevinveenbirkenbach/docker-volume-backup.git
synced 2024-10-31 23:33:11 +01:00
Optimized version by chat gpt
This commit is contained in:
parent
9fa37046ab
commit
7832c85de7
@ -1,36 +1,61 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
volume_name="$1" # Volume-Name
|
volume_name="$1" # Volume-Name
|
||||||
backup_hash="$2" # Hashed Machine ID
|
backup_hash="$2" # Hashed Machine ID
|
||||||
version="$3" # version to backup
|
version="$3" # version to backup
|
||||||
container="$4" # optional
|
container="${4:-}" # optional
|
||||||
mysql_root_password="$5" # optional
|
mysql_root_password="${5:-}" # optional
|
||||||
database="$6" # optional
|
database="${6:-}" # optional
|
||||||
backup_folder="Backups/$backup_hash/docker-volume-backup/$version/$volume_name"
|
backup_folder="Backups/$backup_hash/docker-volume-backup/$version/$volume_name"
|
||||||
backup_files="/$backup_folder/files"
|
backup_files="/$backup_folder/files"
|
||||||
backup_sql="/$backup_folder/sql/backup.sql"
|
backup_sql="/$backup_folder/sql/backup.sql"
|
||||||
|
|
||||||
echo "Inspect volume $volume_name"
|
echo "Inspect volume $volume_name"
|
||||||
docker volume inspect "$volume_name"
|
docker volume inspect "$volume_name"
|
||||||
exit_status_volume_inspect=$?
|
exit_status_volume_inspect=$?
|
||||||
|
|
||||||
if [ $exit_status_volume_inspect -eq 0 ]; then
|
if [ $exit_status_volume_inspect -eq 0 ]; then
|
||||||
echo "Volume $volume_name allready exists"
|
echo "Volume $volume_name already exists"
|
||||||
else
|
else
|
||||||
echo "Create volume $volume_name"
|
echo "Create volume $volume_name"
|
||||||
docker volume create "$volume_name"
|
docker volume create "$volume_name"
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo "ERROR: Failed to create volume $volume_name"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -f "$backup_sql" ]; then
|
if [ -f "$backup_sql" ]; then
|
||||||
if [ -n "$container" ] && [ -n "$mysql_root_password" ] && [ -n "$database" ]; then
|
if [ -n "$container" ] && [ -n "$mysql_root_password" ] && [ -n "$database" ]; then
|
||||||
echo "recover mysql dump"
|
echo "recover mysql dump"
|
||||||
cat $backup_sql | docker exec -i "$container" /usr/bin/mysql -u root --password="$mysql_root_password" $database
|
cat "$backup_sql" | docker exec -i "$container" /usr/bin/mysql -u root --password="$mysql_root_password" "$database"
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo "ERROR: Failed to recover mysql dump"
|
||||||
|
exit 1
|
||||||
|
else
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
echo "a database backup exists, but a parameter is missing. Files will be recovered instead."
|
fi
|
||||||
|
echo "A database backup exists, but a parameter is missing. Files will be recovered instead."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -d "$backup_files" ]; then
|
if [ -d "$backup_files" ]; then
|
||||||
echo "recover files"
|
echo "recover files"
|
||||||
docker run --rm -v "$volume_name:/recover/" -v "$backup_files:/backup/" "kevinveenbirkenbach/alpine-rsync" sh -c "rsync -avv --delete /backup/ /recover/"
|
docker run --rm -v "$volume_name:/recover/" -v "$backup_files:/backup/" "kevinveenbirkenbach/alpine-rsync" sh -c "rsync -avv --delete /backup/ /recover/"
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo "ERROR: Failed to recover files"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "ERROR: $backup_files doesn't exist"
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "ERROR: $backup_files and $backup_sql don't exist"
|
echo "ERROR: Unhandled case"
|
||||||
exit 1
|
exit 1
|
Loading…
Reference in New Issue
Block a user