Skip to content

Instantly share code, notes, and snippets.

@cfg
Last active July 22, 2016 15:15
Show Gist options
  • Save cfg/d0cd908c2fe5bf1ca732c34d330400b4 to your computer and use it in GitHub Desktop.
Save cfg/d0cd908c2fe5bf1ca732c34d330400b4 to your computer and use it in GitHub Desktop.
Import individual MySQL .sql files that weren't dumped using --opt or --extended-insert (like from a VaultPress dump. Show some *very* basic detail about file size, number of lines (correlates roughly to # of rows), and timing.
#!/usr/bin/env bash
VP_DB="set_to_your_dbname"
[[ ! -d "done" ]] && mkdir done
[[ ! -d "error" ]] && mkdir error
echo "To prevent any writes to the database during the import, you should disable the site. Add this to your wp-config.php:"
echo "if( DB_NAME == '$VP_DB' ) die( 'This site is is disabled during import.' );"
read -p "Press Enter to continue. "
time for VP_SQL_FILE in *.sql ; do
[[ -z "$VP_DB" ]] && echo "You must set the 'VP_DB' variable first " && break
VP_IMPORT_INSERTS=$(grep -c '^INSERT' "$VP_SQL_FILE")
VP_TABLE=$(grep -m 1 '^CREATE TABLE' "$VP_SQL_FILE" | sed -r -e 's/^CREATE TABLE (`[^`]+`).*/\1/')
printf "Processing table %s from %s - %d inserts, %s." "$VP_TABLE" "$VP_SQL_FILE" "$VP_IMPORT_INSERTS" $(ls -lah "$VP_SQL_FILE" | cut -d ' ' -f 5)
time mysql $VP_DB --force -e "SET unique_checks=0; SET foreign_key_checks=0; SET autocommit=0; source $VP_SQL_FILE ; commit;"
VP_ERR="$?"
if [[ "$VP_ERR" -ne "0" ]] ; then
echo "*** IMPORT ERROR: MySQL returned errno $VP_ERR for $VP_SQL_FILE"
mv "$VP_SQL_FILE" error/
else
mv "$VP_SQL_FILE" done/
VP_SQL_ROWS=$(mysql $VP_DB -ss -N -e "SELECT COUNT(*) from $VP_TABLE")
if [[ "$VP_SQL_ROWS" -ne "$VP_IMPORT_INSERTS" ]] ; then
printf "*** WARNING: Finished processing %s - %d rows in table, %d insert statements in %s.\n" "$VP_TABLE" "$VP_SQL_ROWS" "$VP_IMPORT_INSERTS" "$VP_SQL_FILE"
else
printf "Finished processing %s - %d rows in table (expecting %d).\n" "$VP_TABLE" "$VP_SQL_ROWS" "$VP_IMPORT_INSERTS"
fi
fi
echo ""
done
echo "Make sure to delete the 'jetpack_options', 'vaultpress', and related WP options if the restore is happening on a different (eg, QA) server"
for foo in jetpack_options vaultpress _vp_signatures _vp_config_option_name_ignore _vp_config_post_meta_name_ignore vaultpress_service_ips_external_cidr jetpack_protect_key ; do
echo "wp option delete $foo"
done
printf "\n or you can use a loop: \n"
echo 'for foo in jetpack_options vaultpress _vp_signatures _vp_config_option_name_ignore _vp_config_post_meta_name_ignore vaultpress_service_ips_external_cidr jetpack_protect_key ; do'
echo ' wp option delete "$foo"'
echo 'done'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment