Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve db scripts #2608

Merged
merged 10 commits into from
May 26, 2022
18 changes: 9 additions & 9 deletions config/homebin/db_backup
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# are imported automatically during an initial provision if
# the databases exist per the import-sql.sh process.
set -eo pipefail
set -u

trap 'rm -rf $TMPFIFODIR' EXIT; TMPFIFODIR=$(mktemp -d); mkfifo $TMPFIFODIR/dbnames

Expand All @@ -16,14 +17,13 @@ gzip=$(get_config_value "general.db_backup.gzip")
exclude_list=$(get_config_values "general.db_backup.exclude")

vvv_info " * Fetching Database names"
mysql --user="root" --password="root" -e 'show databases' | \
grep -v -F "Database" > $TMPFIFODIR/dbnames &
while read db_name
do
mysql -e 'show databases' | grep -v -F "Database" > $TMPFIFODIR/dbnames &
while read db_name; do
# skip these databases
[ "${db_name}" == "mysql" ] && vvv_info " - skipped <b>${db_name}</b>" && continue;
[ "${db_name}" == "information_schema" ] && vvv_info " - skipped <b>${db_name}</b>" && continue;
[ "${db_name}" == "performance_schema" ] && vvv_info " - skipped <b>${db_name}</b>" && continue;
[ "${db_name}" == "sys" ] && vvv_info " - skipped <b>${db_name}</b>" && continue;
[ "${db_name}" == "test" ] && vvv_info " - skipped ${db_name}" && continue;
[ "${db_name}" == "wordpress_unit_tests" ] && vvv_info " - skipped <b>${db_name}</b>" && continue;

Expand All @@ -35,12 +35,12 @@ do
done

if [ ${skip} == "true" ]; then
vvv_info " - skipped <b>${db_name}</b>" && continue;
vvv_info " - excluded <b>${db_name}</b>" && continue;
fi

# don't back up databases with no tables
mysql_cmd="SHOW TABLES FROM \`${db_name}\`" # Required to support hyphens in database names
db_exist=$(mysql -u root -proot --skip-column-names -e "${mysql_cmd}")
db_exist=$(mysql --skip-column-names -e "${mysql_cmd}")
if [ "$?" == "0" ]; then
if [ "" == "${db_exist}" ]; then
vvv_info " - skipped <b>${db_name}</b><info>, no tables in database to back up</info>" && continue;
Expand All @@ -53,15 +53,15 @@ ext=".sql"
if [[ "${gzip}" == "True" ]]; then
ext=".sql.gz"
fi
count=0
count=1
tomjn marked this conversation as resolved.
Show resolved Hide resolved
for db in "${databases[@]}"
do
OUTPUT=$(printf "<info> - %2s/%s Backing up </info><b>%-23s</b><info> to </info><b>'database/backups/%s${ext}'</b>" "${count}" "${#databases[@]}" "'${db}'" "${db}")
vvv_output "${OUTPUT}"
if [[ "${gzip}" == "True" ]]; then
mysqldump -uroot -proot "${db}" | gzip > "/srv/database/backups/${db}.sql.gz"
mysqldump "${db}" | gzip > "/srv/database/backups/${db}.sql.gz"
else
mysqldump -uroot -proot "${db}" > "/srv/database/backups/${db}.sql";
mysqldump "${db}" > "/srv/database/backups/${db}.sql";
fi
let "count=count+1"
done
Expand Down
2 changes: 1 addition & 1 deletion config/homebin/db_restore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#
# Restore individual SQL files for each database.
/srv/database/import-sql.sh
. /srv/database/import-sql.sh
54 changes: 48 additions & 6 deletions database/sql/import-sql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# through {vvv-dir}/database/data
#
# Let's begin...
set -eo pipefail
set -u

source /srv/provision/provision-helpers.sh

Expand All @@ -25,6 +27,26 @@ if [[ -f /srv/config/config.yml ]]; then
VVV_CONFIG=/srv/config/config.yml
fi

FORCE_RESTORE="0"
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
-f|--force) # quick mode
FORCE_RESTORE="1"
shift # past argument
;;
--*|-*)
echo "Unknown option $1"
exit 1
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
shift # past argument
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters

run_restore=$(shyaml get-value general.db_restore 2> /dev/null < ${VVV_CONFIG})
exclude_list=$(get_config_values "general.db_restore.exclude")
include_list=$(get_config_values "general.db_restore.include")
Expand Down Expand Up @@ -58,13 +80,33 @@ then
db_name=$(basename "${file}" .sql.gz)
fi

# if we specified databases, only restore specified ones
if [[ "${#@}" -gt 0 ]]; then
FOUND=0
for var in "$@"; do
if [[ "${var}" == "${db_name}" ]]; then
FOUND=1
break;
fi
done
if [[ "${FOUND}" -eq 0 ]]; then
continue;
fi
fi

# skip these databases
[ "${db_name}" == "mysql" ] && continue;
[ "${db_name}" == "information_schema" ] && continue;
[ "${db_name}" == "performance_schema" ] && continue;
[ "${db_name}" == "sys" ] && continue;
[ "${db_name}" == "test" ] && continue;

vvv_info " * Creating the <b>${db_name}</b><info> database if it doesn't already exist, and granting the wp user access"
if [ "1" == "${FORCE_RESTORE}" ]; then
vvv_info " * Forcing restore of <b>${db_name}</b><info> database, and granting the wp user access"
mysql -e "DROP DATABASE IF EXISTS \`${db_name}\`"
else
vvv_info " * Creating the <b>${db_name}</b><info> database if it doesn't already exist, and granting the wp user access"
fi

skip="false"

Expand All @@ -83,8 +125,8 @@ then
fi
done

mysql -u root --password=root -e "CREATE DATABASE IF NOT EXISTS \`${db_name}\`"
mysql -u root --password=root -e "GRANT ALL PRIVILEGES ON \`${db_name}\`.* TO wp@localhost IDENTIFIED BY 'wp';"
mysql -e "CREATE DATABASE IF NOT EXISTS \`${db_name}\`"
mysql -e "GRANT ALL PRIVILEGES ON \`${db_name}\`.* TO wp@localhost IDENTIFIED BY 'wp';"

[ "${db_name}" == "wordpress_unit_tests" ] && continue;

Expand All @@ -93,17 +135,17 @@ then
fi

mysql_cmd="SHOW TABLES FROM \`${db_name}\`" # Required to support hyphens in database names
db_exist=$(mysql -u root -proot --skip-column-names -e "${mysql_cmd}")
db_exist=$(mysql --skip-column-names -e "${mysql_cmd}")
if [ "$?" != "0" ]
then
vvv_error " * Error - Create the <b>${db_name}</b><error> database via init-custom.sql before attempting import"
else
if [ "" == "${db_exist}" ]; then
vvv_info " * Importing <b>${db_name}</b><info> from <b>${file}</b>"
if [ "${file: -3}" == ".gz" ]; then
gunzip < "${file}" | mysql -u root -proot "${db_name}"
gunzip < "${file}" | mysql "${db_name}"
else
mysql -u root -proot "${db_name}" < "${file}"
mysql "${db_name}" < "${file}"
fi
vvv_success " * Import of <b>'${db_name}'</b><success> successful</success>"
else
Expand Down
6 changes: 5 additions & 1 deletion provision/core/mariadb/provision.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function check_mysql_root_password() {
plugin='mysql_native_password';
SQL
)
root_matches=$(mysql -u root --password=root -s -N -e "${sql}")
root_matches=$(mysql -u root -proot -s -N -e "${sql}")
if [[ $? -eq 0 && $root_matches == "1" ]]; then
# mysql connected and the SQL above matched
vvv_success " * The database root password is the expected value"
Expand Down Expand Up @@ -106,6 +106,10 @@ function mysql_setup() {
cp -f "/srv/provision/core/mariadb/config/my.cnf" "/etc/mysql/my.cnf"
vvv_info " * Copied /srv/provision/core/mariadb/config/my.cnf to /etc/mysql/my.cnf"

cp -f "/srv/provision/core/mariadb/config/root-my.cnf" "/root/.my.cnf"
tomjn marked this conversation as resolved.
Show resolved Hide resolved
chmod 0644 "/root/.my.cnf"
vvv_info " * Copied /srv/provision/core/mariadb/config/root-my.cnf to /root/.my.cnf"

cp -f "/srv/provision/core/mariadb/config/root-my.cnf" "/home/vagrant/.my.cnf"
chmod 0644 "/home/vagrant/.my.cnf"
vvv_info " * Copied /srv/provision/core/mariadb/config/root-my.cnf to /home/vagrant/.my.cnf"
Expand Down
6 changes: 3 additions & 3 deletions provision/provision-helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export -f vvv_success
# @arg $2 string a default value to fall back upon
function get_config_value() {
local value=$(shyaml get-value "${1}" 2> /dev/null < "${VVV_CONFIG}")
echo "${value:-$2}"
echo "${value:-${2:-}}"
}
export -f get_config_value

Expand All @@ -311,7 +311,7 @@ export -f get_config_value
# @arg $2 string a default value to fall back upon
function get_config_values() {
local value=$(shyaml get-values "${1}" 2> /dev/null < "${VVV_CONFIG}")
echo "${value:-$2}"
echo "${value:-${2:-}}"
}
export -f get_config_values

Expand All @@ -332,7 +332,7 @@ export -f get_config_type
# @arg $2 string a default value to fall back upon
function get_config_keys() {
local value=$(shyaml keys "${1}" 2> /dev/null < "${VVV_CONFIG}")
echo "${value:-$2}"
echo "${value:-${2:-}}"
}
export -f get_config_keys

Expand Down