15.02.22

Moving a WordPress instance by hand

Over the years I’ve migrated a lot of WordPress sites. Although there are plugins that can take care of it, I have yet to find a more efficient way than just doing it by hand. The following is my recipe for migrating WordPress instances.

In the example we move a site to our local development environment.

Prerequisites

  • Shell (usually SSH) access to the server that runs the WordPress instance
  • PHP CLI (sudo apt install php-cli)
  • WP-CLI

Obtaining code and DB from the source server

  1. Make a tarball of the site’s files:
    tar czf example.com.tgz public_html
    If it’s only for testing and you’re in a hurry, exclude the WP Uploads directory:
    tar czf example.com.tgz --exclude=**/wp-content/uploads/* public_html
  2. Find database credentials:
    grep DB_ public_html/wp-config.php
  3. Using the found credentials, make a database dump:
    mysqldump somedb -u someuser -p'somepassword' | gzip > example.com.sql.gz

Now copy the tarball and ‘sql-ball’ over to your machine.

Setting up local environment

  1. Unpack the WordPress instance:
    tar xzf example.com.tgz
  2. Create and import the database, e.g.:
    sudo mysqladmin create example_com
    zcat example.com.sql.gz | sudo mysql example_com
  3. Update database credentials in wp-config.php
    For local development, I usually just take user ‘root’ with an empty password (the Gods of Security frown upon me)
  4. To not need HTTPS locally:
    a. Remove any line containing “FORCE_SSL_ADMIN” from wp-config.php
    b. Remove or rename (= deactivate) any plugin (from wp-content/plugins) that forces ssl, for instance ‘really-simple-ssl’
  5. Replace your WordPress base URL using wp-cli:
    wp search-replace https://example.com http://localhost:8080
  6. Run the local development server:
    PHP_CLI_SERVER_WORKERS=50 wp server --port=8080

You should now be able to access the WordPress site by visiting http://localhost:8080 !

Optional, but handy

Create an admin user for yourself:
wp user create myadmin [email protected] --role=administrator --user_pass=admin

Troubleshooting

If connecting to the database fails, it could be that an empty password is not accepted. As using root with an empty password is a bit of a hack, here’s another hack to make it work for newer versions of MySQL/MariaDB. Obtain a root MySQL shell and execute:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '';
FLUSH PRIVILEGES;

30.08.20

Moving commits to a rewritten git repository

The situation: a git repository had been fully prettify-ed using git filter-branch, but I kept working in a separate branch of the old version of the repository.

It seems that git filter-branch could not help me, as the new repository’s internal structure was no longer compatible with my old branch. Therefore, I used this quick and dirty shell script. It worked!

#!/bin/bash
#######################################
# Before running:
#   1. Replace 'oldproject', 'newproject' and the contents of STARTCOMMIT
#   2. Put this script in a directory above oldproject and newproject
#   3. chmod +x thisscript.sh
#
# Use at your own risk.
#######################################

cd oldproject

STARTCOMMIT="...";
COMMITS="$(git log --oneline $STARTCOMMIT..HEAD  | tac | awk '{print $1}')"

for COMMIT in $COMMITS; do
  echo "******************** $COMMIT *****************"
  MESG="$(git log --pretty=format:'%s' $COMMIT~1..$COMMIT)"
  FILES="$(git diff-tree --no-commit-id --name-only -r $COMMIT | grep -v package-lock)"

  # Copy files within commit
  for FILE in $FILES; do
    git show $COMMIT:$FILE > ../newproject/$FILE
  done

  cd ../newproject/

  # Run 'prettier' on the files
  # NOTE: this can be optimized by only using files from $FILES
  node_modules/.bin/prettier --no-config --single-quote --tab-width=4 \
  --arrow-parens always --use-tabs --print-width=140 --write "**/**.js"
  git add -A
  git commit -m "$MESG"

  cd ../oldproject
done

31.01.20

Ubuntu MATE freezes right after showing desktop

Today, my Ubuntu MATE 19.04 desktop froze and stalled the system, right after showing the main desktop and while populating the main panel’s system tray; screen went black, keyboard lighting jumped on and keyboard commands would no longer have effect. Switching terminals with CTRL-ALT-F* had no effect. Kernel panic?

Booting to recovery mode however did work. Tried some things to no avail:

  • Blacklisting the wifi driver (ath10k_pci)
  • Switching graphics card from Nvidia to Intel
  • systemctl disable bluetooth
  • systemctl disable NetworkManager
  • Backing up and removing ~/.config/autostart
  • Backing up and removing ~/.config/mate-session

No suspicious warnings to be seen in /var/log/syslog*, /var/log/dmesg*, /var/log/Xorg.0.log* or anything else, so likely not a hardware issue.

The problem turned out to be a corrupted directory somewhere under ~/.config. The fix was simply to restore a backup of the .config directory.

Suspected culprits:

  • ~/.config/gtk-2.0
  • ~/.config/mate
  • ~/.config/my-weather-indicator

14.11.18

Retrieving git submodules without a .git directory

This comes in handy when you have a copy of a git repository without the .git metadata directory included, for instance a .zip export from Bitbucket.

For use in a shell script.

if [ ! -d '.git' ]; then
    git init .

    # Reconstruct submodules using .gitmodules
    # Based on https://stackoverflow.com/a/11258810
    git config -f .gitmodules --get-regexp '^submodule\..*\.path
 
    while read path_key path
    do
        rm -rf $path
        url_key=$(echo $path_key | sed 's/\.path/.url/')
        branch_key=$(echo $path_key | sed 's/\.path/.branch/')
        url=$(git config -f .gitmodules --get "$url_key")
        branch=$(git config -f .gitmodules --get "$branch_key")
        git submodule add -b $branch --depth 1 $url $path
    done
fi

# Continue as we would /with/ a .git directory
git submodule foreach --recursive 'git fetch --tags'
git submodule update --recursive --init

7.09.16

Drupal 8 in a subdirectory with nginx

Assuming this nginx configuration as a starting point:

root /var/www/example.org/web;

location / {
  try_files $uri @rewrite;
}

location @rewrite {
  rewrite ^ /index.php;
}

You can configure your Drupal 8 instance to run in a subdirectory of the site with two simple steps.

In this example we change the instance’s base url from http://example.org/ to http://example.org/subdir/.

1. Change nginx configuration

Replace the ‘location /’ block with this:

location ~ /subdir/(.*) {
  try_files /$1 @rewrite;
}

2. Change Drupal settings.php

Add the following to your settings.php (or settings.X.php):

if(isset($GLOBALS['request'])) {
  $scriptName = $GLOBALS['request']->server->get('SCRIPT_NAME');
  $scriptName = preg_match('#^/subdir/#', $scriptName) ? : "/subdir$scriptName";
  $GLOBALS['request']->server->set('SCRIPT_NAME', $scriptName);
}

… and that’s it!

Hopefully there will be a cleaner way of doing this in the future…

Tested with Drupal 8.1.6.

16.11.14

Nginx custom autoindex without FancyIndex

Sending your friends an open directory in your site isn’t fun unless it has random ASCII art under it. :-)

Nginx lets one customise the look of open directories using the FancyIndex module. Unfortunately Debian Wheezy doesn’t include this module, unless you get nginx-extras from the wheezy-backports repository. As an alternative, I chose to hack up something in PHP for a similar effect.

Read the rest of this entry »

13.07.14

Recursive FLAC to MP3 converter

I like my music to be in MP3 v0, meaning the data has a variable bitrate of between 200-300 kbit/s. It makes for tiny MP3s that are in my opinion almost indistinguishable from ‘pure’ CD quality, sound-wise.

However, to get music in that format is not always possible, so my music directory has become littered with FLACs. Converting them all to v0 the simple way means the poor old NAS will have a hard time keeping up with other tasks. Enter flac2mp3.sh:

Read the rest of this entry »

25.06.14

Serial connection to the ReadyNAS 102

Although not officially documented, it is possible to set up a serial connection to the Netgear ReadyNAS 102. For this, one needs:

  • A USB-to-TTL converter like this one here. Or this one. Make sure the female pins (the individual wires) are separate, unless you find a TTL plug that looks exactly like the ReadyNAS’s port.
  • PuTTY or any other terminal emulator software that supports serial connections like TeraTerm, screen or minicom.

Read the rest of this entry »