Skip to content

Instantly share code, notes, and snippets.

@rijulsudhir
Forked from Geczy/migrate.sh
Last active April 18, 2024 22:05
Show Gist options
  • Save rijulsudhir/5b9932ebfb96ae08431cc034ac70ace0 to your computer and use it in GitHub Desktop.
Save rijulsudhir/5b9932ebfb96ae08431cc034ac70ace0 to your computer and use it in GitHub Desktop.
Migrate Coolify to a new server
#!/bin/bash
# -- Shouldn't need to modify anything below --
backupSourceDir="/data/coolify/"
backupFileName="coolify_backup.tar.gz"
# Check if the source directory exists
if [ ! -d "$backupSourceDir" ]; then
echo "❌ Source directory $backupSourceDir does not exist"
exit 1
fi
echo "✅ Source directory exists"
# Get the names of all Docker containers
containerNames=$(docker ps -a --format '{{.Names}}')
# Initialize an empty string to hold the volume paths
volumePaths=""
# Loop over the container names
for containerName in $containerNames; do
# Get the volumes for the current container
volumeNames=$(docker inspect --format '{{range .Mounts}}{{.Name}}{{end}}' "$containerName")
# Loop over the volume names
for volumeName in $volumeNames; do
# Check if the volume name is not empty
if [ -n "$volumeName" ]; then
# Add the volume path to the volume paths string
volumePaths+=" /var/lib/docker/volumes/$volumeName"
fi
done
done
# Calculate the total size of the volumes
# shellcheck disable=SC2086
totalSize=$(du -csh $volumePaths 2>/dev/null | grep total | awk '{print $1}')
# Print the total size of the volumes
echo "✅ Total size of volumes to migrate: $totalSize"
# Print size of backupSourceDir
backupSourceDirSize=$(du -csh $backupSourceDir 2>/dev/null | grep total | awk '{print $1}')
echo "✅ Size of the source directory: $backupSourceDirSize"
# Check if the backup file already exists
if [ ! -f "$backupFileName" ]; then
echo "🚸 Backup file does not exist, creating"
# Recommend stopping docker before creating the backup
echo "🚸 It's recommended to stop all Docker containers before creating the backup
Do you want to stop Docker? (y/n)"
read -r answer
if [ "$answer" != "${answer#[Yy]}" ]; then
if ! systemctl stop docker; then
echo "❌ Docker stop failed"
exit 1
fi
echo "✅ Docker stopped"
else
echo "🚸 Docker not stopped, continuing with the backup"
fi
# shellcheck disable=SC2086
if ! tar --exclude='*.sock' -Pczf $backupFileName -C / $backupSourceDir $HOME/.ssh/authorized_keys $volumePaths; then
echo "❌ Backup file creation failed"
exit 1
fi
echo "✅ Backup file created"
else
echo "🚸 Backup file already exists, skipping creation"
fi
#!/bin/bash
backupFileName="coolify_backup.tar.gz"
# Check if Docker is a service
if systemctl is-active --quiet docker; then
# Stop Docker if it's a service
if ! systemctl stop docker; then
echo '❌ Docker stop failed';
exit 1;
fi
echo '✅ Docker stopped';
else
echo 'ℹ️ Docker is not a service, skipping stop command';
fi
echo '🚸 Saving existing authorized keys...';
cp ~/.ssh/authorized_keys ~/.ssh/authorized_keys_backup;
echo '🚸 Extracting backup file...';
if ! tar -Pxzf $backupFileName -C /; then
echo '❌ Backup file extraction failed';
exit 1;
fi
echo '✅ Backup file extracted';
echo '🚸 Merging authorized keys...';
cat ~/.ssh/authorized_keys_backup ~/.ssh/authorized_keys | sort | uniq > ~/.ssh/authorized_keys_temp;
mv ~/.ssh/authorized_keys_temp ~/.ssh/authorized_keys;
chmod 600 ~/.ssh/authorized_keys;
echo '✅ Authorized keys merged';
if ! curl -fsSL https://cdn.coollabs.io/coolify/install.sh | bash; then
echo '❌ Coolify installation failed';
exit 1;
fi
echo '✅ Coolify installed';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment