Created
November 7, 2023 23:43
-
-
Save maietta/0d73af87cad3d9b3d54dadfa49c21ed8 to your computer and use it in GitHub Desktop.
CapRover service app snapshot script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
# Define the backup base path | |
backup_base_path="backups" | |
# Check if the service name argument is provided | |
if [ $# -ne 1 ]; then | |
echo "Usage: $0 <service_name>" | |
exit 1 | |
fi | |
# Extract the service name argument | |
service_suffix="$1" | |
# Check if the service exists with the full name | |
if docker service ls --filter "name=srv-captain--$service_suffix" --quiet | grep -q .; then | |
full_service_name="srv-captain--$service_suffix" | |
else | |
# If the service with the full name does not exist, check without the prefix | |
if docker service ls --filter "name=$service_suffix" --quiet | grep -q .; then | |
full_service_name="$service_suffix" | |
else | |
echo "Service srv-captain--$service_suffix or $service_suffix not found. Aborting migration." | |
exit 1 | |
fi | |
fi | |
# Get the service name without the prefix | |
service_name="${full_service_name#srv-captain--}" | |
echo "Service $full_service_name found. Scaling it to 0 replicas and waiting for tasks to complete..." | |
# Scale the service to 0 replicas | |
docker service scale "$full_service_name=0" | |
# Wait for the service to scale down (adjust the sleep duration as needed) | |
sleep 10 | |
echo "Service $full_service_name has been scaled down to 0 replicas." | |
# Create a directory for the backup with a timestamp, or ensure it exists | |
backup_dir="$backup_base_path/$service_name" | |
mkdir -p "$backup_dir" | |
# Get the service information using docker inspect | |
docker service inspect "$full_service_name" > "$backup_dir/$service_name.json" | |
echo "Service information has been saved in $backup_dir/$service_name.json." | |
# List the volumes used by the service | |
volumes=$(docker service inspect --format '{{range .Spec.TaskTemplate.ContainerSpec.Mounts}}{{.Source}} {{end}}' "$full_service_name") | |
# Loop through the volumes and create tar backups using the specified command | |
for volume in $volumes; do | |
backup_command="docker run -v $volume:/volume --rm loomchild/volume-backup backup > "$backup_dir/$(basename $volume).tar"" | |
eval "$backup_command" | |
done | |
echo "Volume backups have been created in $backup_dir." | |
# Set the service back to its original number of replicas | |
original_replicas=$(docker service inspect --format "{{.Spec.Mode.Replicated.Replicas}}" "$full_service_name") | |
docker service scale "$full_service_name=$original_replicas" | |
echo "Service $full_service_name has been scaled back to $original_replicas replicas." | |
# Add your migration steps here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment