dokku-sync/dokku-sync.sh

103 lines
2.7 KiB
Bash
Raw Normal View History

2024-07-10 14:02:16 +00:00
#!/bin/sh
# vim: set ts=2 sw=2 et:
set -e
set -u
# Generate SSH keypair on control machine (in temporary directory)
TEMP_DIR=$(mktemp -d)
MIGRATION_SSH_KEY="$TEMP_DIR/dokku-sync-migration_ed25519"
SSH_KEY="/path/to/control/key"
ssh-keygen -t ed25519 -f "$SSH_KEY" -N ""
2024-07-10 14:02:16 +00:00
usage() {
echo "Usage: $0 --src USER@HOSTNAME --dest USER@HOSTNAME --privkey PATH_TO_PRIVATE_KEY"
echo
echo "Options:"
echo " --src Source SSH address in the format USER@HOSTNAME"
echo " --dest Destination SSH address in the format USER@HOSTNAME"
echo " --privkey Path to the private SSH key"
echo " -h, --help Show this help message"
2024-07-10 14:02:16 +00:00
}
dest_run() {
ssh -i "$SSH_KEY" "$SOURCE_SSH_ADDR" "$*"
}
source_run() {
ssh -i "$SSH_KEY" "$SOURCE_SSH_ADDR" "$*"
}
## ENTRYPOINT
# Loop and parse command line arguments
while [ "$#" -gt 0 ]; do
case "$1" in
--src)
SRC_SSH_ADDR="$2"
shift 2
;;
--dest)
DEST_SSH_ADDR="$2"
shift 2
;;
--privkey)
SSH_KEY="$2"
shift 2
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown option: $1"
usage
exit 1
;;
esac
done
# Split values for SRC and DEST
DEST_HOST=$(echo "$DEST_SSH_ADDR" | cut -d'@' -f2)
DEST_USER=$(echo "$DEST_SSH_ADDR" | cut -d'@' -f1)
SOURCE_HOST=$(echo "$SRC_SSH_ADDR" | cut -d'@' -f2)
SOURCE_USER=$(echo "$SRC_SSH_ADDR" | cut -d'@' -f1)
2024-07-10 14:02:16 +00:00
# Stop all remote services
source_run "dokku ps:stop --all"
2024-07-10 14:02:16 +00:00
source_run "dokku mariadb:list |tail -n+2 |xargs -iSERVICE -- dokku mariadb:stop SERVICE"
source_run "dokku postgres:list |tail -n+2 |xargs -iSERVICE -- dokku postgres:stop SERVICE"
source_run "dokku redis:list |tail -n+2 |xargs -iSERVICE -- dokku redis:stop SERVICE"
source_run "dokku mongo:list |tail -n+2 |xargs -iSERVICE -- dokku mongo:stop SERVICE"
2024-07-10 14:02:16 +00:00
source_run "systemctl stop docker"
source_run "systemctl stop docker.socket"
2024-07-10 14:02:16 +00:00
# Stop all local services
dest_run "dokku ps:stop --all"
dest_run "systemctl stop docker"
dest_run "systemctl stop docker.socket"
# Prepare directories
dest_run "mkdir -p /home/dokku/"
dest_run "mkdir -p /home/data/"
dest_run "mkdir -p /var/lib/dokku/"
2024-07-10 14:02:16 +00:00
# Copy all data
rsync -avz --delete "$SOURCE_SSH_ADDR:/home/dokku/" "$DEST_SSH_ADDR:/home/dokku/"
rsync -avz --delete "$SOURCE_SSH_ADDR:/var/lib/dokku/" "$DEST_SSH_ADDR:/var/lib/dokku/"
rsync -avz --delete "$SOURCE_SSH_ADDR:/home/data/" "$DEST_SSH_ADDR:/home/data/"
rsync -avz --delete "$SOURCE_SSH_ADDR:/home/git/" "$DEST_SSH_ADDR:/home/git/"
2024-07-10 14:02:16 +00:00
# FIXME: check if rsync succeeded
2024-07-10 14:02:16 +00:00
# Start all local services
dest_run "dokku apps:list"
2024-07-10 14:02:16 +00:00
# Patch URLs from imported domains
dest_run "find /home/dokku/ -name VHOST -or -name URLS -print0 | xargs -0 -iFILE -- sed -i \"s/$SOURCE_HOST/$DEST_HOST/g\" FILE"
2024-07-10 14:02:16 +00:00
echo "SUCCESS"