Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 150 lines (134 sloc) 4.67 KB
#!/bin/bash
set -e -f -u -o pipefail
wp_option() {
local option="$1" wp_config="$2"
readonly option wp_config
awk -F \' "/${option}/"' { print $4 }' "${wp_config}"
}
get_table_prefix() {
local wp_config="$1"
readonly wp_config
awk -F \' '/^\$table_prefix/ { print $2 }' "${wp_config}"
}
restore_from_backup() {
if [[ "$#" -ne 2 ]]; then
printf "Usage: restore_from_backup WP_CONFIG DUMP\\n" >&2
return 1
fi
local wp_config="$1" dump="$2"
readonly wp_config dump
local db_password db_name db_user
db_password="$(wp_option DB_PASSWORD "${wp_config}")"
db_name="$(wp_option DB_NAME "${wp_config}")"
db_user="$(wp_option DB_USER "${wp_config}")"
readonly db_password db_name db_user
# Uncompress the dump if necessary, or copy it to simplify the cleanup path.
local dump_tmp
dump_tmp="$(mktemp "${TMPDIR:-/tmp}/restore-dump-XXXXXXXXXXXX.sql")"
if [[ "${dump}" =~ .*.bz2 ]]; then
bzcat "${dump}" > "${dump_tmp}"
else
cat "${dump}" > "${dump_tmp}"
fi
printf "Restoring backup from %s\\n" "${dump_tmp}"
mysql --user="${db_user}" --password="${db_password}" \
"${db_name}" < "${dump_tmp}"
rm "${dump_tmp}"
}
output_sql_template() {
cat <<'END_OF_SQL_TEMPLATE'
[%# Output an SQL statement to rewrite data.
Args:
domain: replace www.domain with dev.domain.
table_prefix: table prefix configured in Wordpress
table: the table to update; will have table_prefix prepended to it.
column: the column to update.
-%]
[% BLOCK rewrite_column -%]
UPDATE `[% table_prefix %][% table %]`
SET `[% column %]` = REPLACE(`[% column %]`, 'www.[% domain %]', 'dev.[% domain %]')
WHERE `[% column %]` LIKE '%www.[% domain %]%';
[% END -%]
[%# The columns to rewrite.
-%]
[%
columns_to_rewrite = [
{ table = "cart66_sessions", column = "user_agent" },
{ table = "cart66_sessions", column = "user_data" },
{ table = "commentmeta", column = "meta_value" },
{ table = "comments", column = "comment_author_url" },
{ table = "options", column = "option_value" },
{ table = "postmeta", column = "meta_value" },
{ table = "posts", column = "post_content" },
{ table = "posts", column = "pinged" },
{ table = "posts", column = "guid" },
{ table = "users", column = "user_url" },
]
-%]
[%# Process all the (table, column) pairs.
Note that domain and table_prefix (used earlier) are supplied as flags.
-%]
[% FOREACH pair IN columns_to_rewrite -%]
[%- PROCESS rewrite_column
table = pair.table
column = pair.column
-%]
[% END -%]
END_OF_SQL_TEMPLATE
}
rewrite_database_contents() {
if [[ "$#" -ne 2 ]]; then
printf "Usage: rewrite_database_contents DOMAIN WP_CONFIG\\n" >&2
return 1
fi
local domain="$1" wp_config="$2"
readonly domain wp_config
local db_password db_name db_user table_prefix
db_password="$(wp_option DB_PASSWORD "${wp_config}")"
db_name="$(wp_option DB_NAME "${wp_config}")"
db_user="$(wp_option DB_USER "${wp_config}")"
table_prefix="$(get_table_prefix "${wp_config}")"
readonly db_password db_name db_user table_prefix
local sql_commands sql_template
sql_commands="$(mktemp "${TMPDIR:-/tmp}/restore-sql-commands-XXXXXXXXXXXX.sql")"
sql_template="$(mktemp "${TMPDIR:-/tmp}/restore-sql-template-XXXXXXXXXXXX.tt2")"
output_sql_template > "${sql_template}"
tpage --define domain="${domain}" --define table_prefix="${table_prefix}" \
"${sql_template}" > "${sql_commands}"
printf "Rewriting data with %s\\n" "${sql_commands}"
mysql --user="${db_user}" --password="${db_password}" \
"${db_name}" < "${sql_commands}"
rm "${sql_template}" "${sql_commands}"
}
main2() {
if [[ "$#" -ne 3 || ! -f "$2/wp-config.php" || ! -f "$3" ]]; then
printf "Usage: %s DOMAIN WORDPRESS_BASE_DIR DATABASE_DUMP\\n" "$0" >&2
return 1
fi
local domain="$1" wp_config="$2/wp-config.php" dump="$3"
readonly domain wp_config dump
printf "Domain: %s\\n" "${domain}"
printf "Wordpress config: %s\\n" "${wp_config}"
printf "Backup: %s\\n" "${dump}"
restore_from_backup "${wp_config}" "${dump}"
rewrite_database_contents "${domain}" "${wp_config}"
}
find_latest_dump() {
local prefix="$1"
# Only consider backups less than 12 hours old; the filenames contain date and
# timestamps so sorting and taking the last one will output the latest backup.
find "${HOME}/wordpress-backups/" -name "${prefix}*" -mmin -$((12 * 60)) \
| sort \
| tail -1
}
main() {
if [[ "$#" -ne 0 ]]; then
printf "Usage: %s\\n" "$0" >&2
printf "Finds the latest www backup and restores it to dev.\\n" >&2
return 1
fi
printf "Triggering backup.\\n"
backup-wordpress "/home/arianetobin/arianetobin.ie"
main2 arianetobin.ie "${HOME}/dev.arianetobin.ie" "$(find_latest_dump ariane)"
}
main "$@"