I've written a script in an attempt to try to automate moving hosts from old hosts to our new docker containers.
All feedback and input appreciated!
#!/bin/bash
# Generate private public ssh-keys
if [ ! -f "$HOME/.ssh/id_rsa" ] && [ ! -f "$HOME/.ssh/id_rsa.pub" ]; then
ssh-keygen -b 4096
fi
read -p "Enter your ssh host:" ssh_host
read -p "Enter your ssh username:" ssh_user
read -p "Enter your remote ssh port:" ssh_port
# REMOTE SSH
if [ -z "$ssh_user" ]; then
echo -e "Config: SSH Username missing"
echo $ssh_user
exit
fi
if [ -z "$ssh_host" ]; then
echo -e "Config: SSH Host is missing"
exit
fi
if [ -z "$ssh_port" ]; then
echo -e "Config: SSH Port missing"
exit
fi
ssh-copy-id $ssh_user@$ssh_host -p $ssh_port -i "$HOME/.ssh/id_rsa" &>/dev/null
# REMOTE DATABASE
read -p "Enter path for wp-config.php:" wp_config_path
# INIT
db_details="cat $wp_config_path/wp-config.php"
scp -P $ssh_port -r $ssh_user@$ssh_host:"$wp_config_path/*" .
# Might be dangerous if file contains malicious input in the values?
eval $(awk -F "[()']" '/^define\(/{printf "%s='\''%s'\''\n", $3, $5;}' < wp-config.php | grep DB_*)
if [ -z "$DB_USER" ]; then
echo -e "Config: RDB user missing"
exit
fi
if [ -z "$DB_PASSWORD" ]; then
echo -e "Config: RDB password missing"
exit
fi
if [ -z "$DB_NAME" ]; then
echo -e "Config: RDB name missing"
exit
fi
dump="mysqldump -u $DB_USER --password='$DB_PASSWORD' $DB_NAME"
read -p "Enter your LOCAL database user:" local_db_user
read -p "Enter your LOCAL database name:" local_db_name
read -s -p "Enter your local database password:" local_db_password
printf "\033c"
ssh $ssh_user@$ssh_host -p $ssh_port $dump | mysql -u $local_db_user --password=$local_db_password $local_db_name
sed -i -e "s;\(define([[:space:]]*'DB_USER',[[:space:]]*\)\(.*\)\()\;\);\1'$local_db_user'\3;g" wp-config.php
sed -i -e "s;\(define([[:space:]]*'DB_PASSWORD',[[:space:]]*\)\(.*\)\()\;\);\1'$local_db_password'\3;g" wp-config.php
sed -i -e "s;\(define([[:space:]]*'DB_NAME',[[:space:]]*\)\(.*\)\()\;\);\1'$local_db_name'\3;g" wp-config.php
echo -e "Database imported to: $local_db_name"
echo -e "Cleaning up..."
# CLEANING UP
# This is bad, and should NEVER be used on hosts with active pub/private key authentication.
ssh $ssh_user@$ssh_host -p $ssh_port 'echo "" > $HOME/.ssh/authorized_keys'
ssh_user=
ssh_host=
ssh_port=
DB_USER=
DB_PASSWORD=
DB_NAME=
local_db_name=
local_db_password=
local_db_user=
wp_config_path=
echo -e "Exiting"