Last active
July 25, 2024 18:33
-
-
Save Geczy/83c1c77389be94ed4709fc283a0d7e23 to your computer and use it in GitHub Desktop.
Migrate Coolify to a new server
This file contains 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 | |
# This script will backup your Coolify instance and move everything to a new server. Docker volumes, Coolify database, and ssh keys | |
# 1. Script must run on the source server | |
# 2. Have all the containers running that you want to migrate | |
# Configuration - Modify as needed | |
sshKeyPath="$HOME/.ssh/your_private_key" # Key to destination server | |
destinationHost="server.example.com" | |
# -- 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" | |
# Check if the SSH key file exists | |
if [ ! -f "$sshKeyPath" ]; then | |
echo "β SSH key file $sshKeyPath does not exist" | |
exit 1 | |
fi | |
echo "β SSH key file exists" | |
# Check if we can SSH to the destination server, ignore "The authenticity of host can't be established." errors | |
if ! ssh -i "$sshKeyPath" -o "StrictHostKeyChecking no" -o "ConnectTimeout=5" root@$destinationHost "exit"; then | |
echo "β SSH connection to $destinationHost failed" | |
exit 1 | |
fi | |
echo "β SSH connection successful" | |
# Get the names of all running Docker containers | |
containerNames=$(docker ps --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 | |
# Define the remote commands to be executed | |
remoteCommands=" | |
# 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 - -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'; | |
" | |
# SSH to the destination server, execute the remote commands | |
if ! ssh -i "$sshKeyPath" -o "StrictHostKeyChecking no" root@$destinationHost "$remoteCommands" <$backupFileName; then | |
echo "β Remote commands execution or Docker restart failed" | |
exit 1 | |
fi | |
echo "β Remote commands executed successfully" | |
# Clean up - Ask the user for confirmation before removing the local backup file | |
echo "Do you want to remove the local backup file? (y/n)" | |
read -r answer | |
if [ "$answer" != "${answer#[Yy]}" ]; then | |
if ! rm -f $backupFileName; then | |
echo "β Failed to remove local backup file" | |
exit 1 | |
fi | |
echo "β Local backup file removed" | |
else | |
echo "πΈ Local backup file not removed" | |
fi |
I tried something very similar, but must have missed something, as I spent an entire day trying to figure out some errors in coolify. Thank you sincerely for posting this. As far as I can tell, everything works exactly as it did on my original VPS and it took literally seconds to do.
Here is an updated version with support for multiple volumes and some improvements
#!/bin/bash
# This script will backup your Coolify instance and move everything to a new server,
# including Docker volumes, Coolify database, and SSH keys.
# Configuration - Modify as needed
sshKeyPath="/home/user/.ssh/key" # Key to the destination server
destinationHost="192.168.1.1"
sshPort=22 # SSH port for the destination server
# -- Shouldn't need to modify anything below --
backupSourceDir="/data/coolify/"
backupFileName="coolify_backup.tar.gz"
# Ensure the script is run as root
if [ "$EUID" -ne 0 ]; then
echo "β Please run the script as root"
exit 1
fi
# Check if the source directory exists
if [ ! -d "$backupSourceDir" ]; then
echo "β Source directory $backupSourceDir does not exist"
exit 1
fi
echo "β
Source directory exists"
# Check if the SSH key file exists
if [ ! -f "$sshKeyPath" ]; then
echo "β SSH key file $sshKeyPath does not exist"
exit 1
fi
echo "β
SSH key file exists"
# Check if Docker is installed and running
if ! command -v docker >/dev/null 2>&1; then
echo "β Docker is not installed"
exit 1
fi
if ! systemctl is-active --quiet docker; then
echo "β Docker is not running"
exit 1
fi
echo "β
Docker is installed and running"
# Check if we can SSH to the destination server
if ! ssh -p "$sshPort" -i "$sshKeyPath" -o "StrictHostKeyChecking no" -o "ConnectTimeout=5" root@"$destinationHost" "exit"; then
echo "β SSH connection to $destinationHost failed"
exit 1
fi
echo "β
SSH connection successful"
# Get the names of all running Docker containers
containerNames=$(docker ps --format '{{.Names}}')
# Initialize an array to hold the volume paths
volumePaths=()
# Loop over the container names and get their volumes
for containerName in $containerNames; do
volumeNames=$(docker inspect --format '{{range .Mounts}}{{.Name}} {{end}}' "$containerName")
for volumeName in $volumeNames; do
if [ -n "$volumeName" ]; then
volumePaths+=("/var/lib/docker/volumes/$volumeName/_data")
fi
done
done
# Calculate and print the total size of the volumes and the source directory
totalSize=$(du -csh "${volumePaths[@]}" 2>/dev/null | grep total | awk '{print $1}')
echo "β
Total size of volumes to migrate: ${totalSize:-0}"
backupSourceDirSize=$(du -csh "$backupSourceDir" 2>/dev/null | grep total | awk '{print $1}')
echo "β
Size of the source directory: ${backupSourceDirSize:-0}"
# Check if the backup file already exists and create it if it does not
if [ ! -f "$backupFileName" ]; then
echo "πΈ Backup file does not exist, creating..."
# Optionally stop 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 -rp "Answer: " answer
if [[ "$answer" =~ ^[Yy]$ ]]; then
systemctl stop docker && systemctl stop docker.socket
echo "β
Docker stopped"
else
echo "πΈ Docker not stopped, continuing with the backup"
fi
# Create the backup tarball with progress feedback
tar --exclude='*.sock' -Pczf "$backupFileName" -C / "$backupSourceDir" "$HOME/.ssh/authorized_keys" "${volumePaths[@]}" --checkpoint=.1000
if [ $? -ne 0 ]; then
echo "β Backup file creation failed"
exit 1
fi
echo "β
Backup file created"
else
echo "πΈ Backup file already exists, skipping creation"
fi
# Define the remote commands to be executed
remoteCommands="
if systemctl is-active --quiet docker; then
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
cp ~/.ssh/authorized_keys ~/.ssh/authorized_keys_backup;
if ! tar -Pxzf - -C /; then
echo 'β Backup file extraction failed';
exit 1;
fi
echo 'β
Backup file extracted';
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';
"
# SSH to the destination server, execute the remote commands
if ! ssh -p "$sshPort" -i "$sshKeyPath" -o "StrictHostKeyChecking no" root@"$destinationHost" "$remoteCommands" <"$backupFileName"; then
echo "β Remote commands execution or Docker restart failed"
exit 1
fi
echo "β
Remote commands executed successfully"
# Clean up - Ask the user for confirmation before removing the local backup file
echo "Do you want to remove the local backup file? (y/n)"
read -rp "Answer: " answer
if [[ "$answer" =~ ^[Yy]$ ]]; then
if ! rm -f "$backupFileName"; then
echo "β Failed to remove local backup file"
exit 1
fi
echo "β
Local backup file removed"
else
echo "πΈ Local backup file not removed"
fi
im getting a msg saying i should be logged as azureuser `
azureuser@Coolifyserver:~$ sudo ./CoolifyBackup.sh
β
Source directory exists
β
SSH key file exists
β
Docker is installed and running
Please login as the user "azureuser" rather than the user "root".
β SSH connection to failed
azureuser@Coolifyserver:~$`
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot for the script