HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Moving WordPress database and files to new host

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
newwordpressdatabasefilesmovinghostand

Problem

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;}'  $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"

Solution

Make input validation more user-friendly

You ask to enter host, user and port, and if one of them is empty you exit.
If I entered a host and a user and accidentally pressed enter for port (empty), I would be very unhappy with the script.

It would be better to put the prompting logic in a loop,
and repeat forever until a non-empty value is entered.
Ideally in a function, to reduce code duplication.

Exit with non-zero code on error

When exiting with error,
it's recommended to use a non-zero exit code.

~ is the same as $HOME

I prefer writing ~ because it's shorter.

Use printf instead of echo -e

The flags of echo are not portable.
To get the behavior of echo -e,
I suggest to make it a habit to use printf instead.

Replace awk + grep with just awk

In this command you use a combination of awk and grep:

eval $(awk -F "[()']" '/^define\(/{printf "%s='\''%s'\''\n", $3, $5;}' < wp-config.php | grep DB_*)


You can do what grep does here in awk,
which will be more efficient.
The difference will be negligible in this example,
but it's good to make it a habit to avoid additional processes when easily possible.

Replace multiple sed calls when one is enough

This code rewrites the same file 3 times:

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

One sed would be enough, using multiple -e parameters.

Pointless cleaning up

Resetting the variables at the end of the script seem pointless.
This is only useful if you source this script, which seems unlikely.
If you run this script (as opposed to sourcing it),
then the variables you defined or modified will not be visible after the script has completed.

Code Snippets

eval $(awk -F "[()']" '/^define\(/{printf "%s='\''%s'\''\n", $3, $5;}' < wp-config.php | grep DB_*)
sed -i -e "s;\(define([[:space:]]*'DB_USER',[[:space:]]*\)\(.*\)\()\;\);\1'$local_db_user'\3;g"

Context

StackExchange Code Review Q#139131, answer score: 2

Revisions (0)

No revisions yet.