Last active
October 9, 2025 22:37
-
-
Save ironprogrammer/c322963743746bea00085a2439c58f61 to your computer and use it in GitHub Desktop.
Quick WordPress on SQLite instances, with rollback and DB backup.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Spin up a SQLite WP site, e.g.: `wp-sqlite wp-603 6.0.3` | |
| # If omitted, version defaults to latest, and pins rollback to that version | |
| # @TODO consider using https://developer.wordpress.org/cli/commands/cli/alias/ for locals? | |
| wp-sqlite() { | |
| local wp_name='' | |
| local wp_ver='latest' | |
| # check required arg for site name | |
| if [ -z "$1" ]; then | |
| echo Usage: wp-sqlite site-name \[wp-version\] | |
| echo You must specify a site name. | |
| return 1 | |
| fi | |
| wp_name="$1" | |
| # optional wordpress version | |
| if [ ! -z "$2" ]; then | |
| wp_ver="$2" | |
| fi | |
| # confirm whether to continue | |
| echo Site Name: ${wp_name} | |
| echo WordPress Version: ${wp_ver} | |
| [[ "$(echo -n 'Continue with installation? [y/N] ' >&2; read -q; echo $REPLY)" == [Nn]* ]] \ | |
| && echo && echo Aborted. && return 1 | |
| # confirmed! do the deed... | |
| # set up folder | |
| echo && echo "Installing ${wp_name}..." | |
| mkdir ${wp_name} && cd ${wp_name} | |
| if [ "$?" -ne 0 ]; then return 1; fi | |
| # install wordpress files | |
| if [ ${wp_ver} != 'latest' ]; then | |
| wp core download https://wordpress.org/wordpress-${wp_ver}.zip | |
| else | |
| wp core download | |
| wp_ver=$(wp core version) | |
| fi | |
| if [ "$?" -ne 0 ]; then return 1; fi | |
| # create .env for `wp-reset` rollback option | |
| echo "wp_name=${wp_name}" > .env | |
| echo "wp_ver=${wp_ver}" >> .env | |
| # install sqlite | |
| curl https://raw.githubusercontent.com/aaemnnosttv/wp-sqlite-db/master/src/db.php -o wp-content/db.php | |
| # fix min required mysql version 5.5.5 introduced in 6.5; opt for 8.0 like canonical plugin uses | |
| # see https://make.wordpress.org/core/2023/12/08/raising-the-minimum-version-of-mysql-required-in-wordpress-6-5/ | |
| sed -i '' "s/'5.5'/'8.0'/g" wp-content/db.php | |
| # not needed after merge of update pr, https://github.com/aaemnnosttv/wp-sqlite-db/pull/61 | |
| # create database file | |
| touch wp-db.sqlite | |
| # create wp-config.php | |
| echo "<?php\n" >> wp-config.php | |
| echo "define( 'DB_DIR', __DIR__ );" >> wp-config.php | |
| echo "define( 'DB_FILE', 'wp-db.sqlite' );" >> wp-config.php | |
| echo -n "\n//$(cat wp-config-sample.php)" >> wp-config.php | |
| # prevent auto update for previous point versions | |
| wp config set WP_AUTO_UPDATE_CORE false --raw | |
| # set up site | |
| wp core install --url=${wp_name}.test --title=${wp_name} --admin_user=admin --admin_password=password --admin_email=admin@example.com --skip-email | |
| # FYI: valet still asks for password when checking `valet links`, so may as well just re-add | |
| echo "Gonna ask for sudo to set up Valet link..." | |
| valet >/dev/null 2>/dev/null && valet link ${wp_name} | |
| if [ "$?" -ne 0 ]; then return 1; fi | |
| # launch wp-admin | |
| wp admin | |
| } | |
| # Reset WordPress core to original version for this directory | |
| wp-reset() { | |
| if [ ! -f ".env" ]; then | |
| echo Directory not set up for `wp-reset` rollback. | |
| return 1 | |
| fi | |
| source .env | |
| if [ ${wp_ver} = '' ]; then | |
| echo There is no rollback \`wp_ver\` set. | |
| return 1 | |
| fi | |
| [[ "$(echo -n "Upgrade/rollback to WordPress ${wp_ver}? [y/N] " >&2; read -q; echo $REPLY)" == [Nn]* ]] \ | |
| && echo && echo Aborted. && return 1 | |
| echo | |
| wp core upgrade https://wordpress.org/wordpress-${wp_ver}.zip --force | |
| echo Make sure to reset or restore the database, if needed. | |
| } | |
| # Kills current throwaway site | |
| wp-kill() { | |
| if [ ! -f ".env" ]; then | |
| echo Directory not set up for \`wp-kill\`. | |
| return 1 | |
| fi | |
| source .env | |
| if [ ${wp_name} = '' ]; then | |
| echo There is no `wp_name` set. | |
| return 1 | |
| fi | |
| cd .. | |
| rm -r ${wp_name} \ | |
| && echo Site killed. | |
| } | |
| # Backup, restore, or reset SQLite database file | |
| alias db=wp-db | |
| wp-db() { | |
| if [ -z "$1" ]; then | |
| echo Usage: db backup\|restore\|reset | |
| return 1 | |
| fi | |
| local db_file="wp-db.sqlite" | |
| if [ $1 = 'restore' ]; then | |
| cp ${db_file}.bak ${db_file} \ | |
| && echo SQLite DB restored from ${db_file}.bak. | |
| elif [ $1 = 'backup' ]; then | |
| cp ${db_file} ${db_file}.bak \ | |
| && echo SQLite DB backed up to ${db_file}.bak. | |
| elif [ $1 = 'reset' ]; then | |
| source .env | |
| if [ ${wp_name} = '' ]; then | |
| echo There is no `wp_name` set. | |
| return 1 | |
| fi | |
| mv ${db_file} ${db_file}.bak \ | |
| && echo SQLite DB backed up to ${db_file}.bak. \ | |
| && touch ${db_file} \ | |
| && wp core install --url=${wp_name}.test --title=${wp_name} --admin_user=admin --admin_password=password --admin_email=admin@example.com --skip-email | |
| else | |
| echo Command \"$1\" not recognized. | |
| fi | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment