Restoring a web site from a backup

When chime was hacked, I decided to try to restore howyourbrainworks.net on crescendo.

The big picture of steps to take when "migrating" a drupal site are as follows:

  1. Make sure apache, mysql and php are installed and working (i.e. you need a LAMP stack)
  2. Copy the code base
  3. Configure apache so it will serve teh web folder of the code base
  4. Create an empty database for the web site to use initially. You always do a "fresh install" of the old code base first, before you restore the database
  5. Remove the sites/default/settings.php file and navigate to the site. That will put the code base in install mode on the new machine. Install the site to the empty database.
  6. When install is completed, restore the backed-up database
  7. Tidy up the installation by running the database update script, maybe rebuild the cache using drush, and check the site's status page for things that need attention.  

 

The steps I took were as follows:

1) Install apache on glissando with commands

sudo apt update
sudo apt install apache2
sudo a2enmod rewrite
sudo ufw allow in "Apache Full"

2) Install mysql and php with commands

sudo apt install mysql-server
sudo apt install php libapache2-mod-php php-mysql
sudo mysql_secure_installation utility

and follow prompts to set up root password.

2.5) alternatively to installing a LAMP stack

on a new server, you can set up an existing server (crescendo/twinkle) to serve multiple sites by following the excellent instructions here

3) copy code base from twinkle backup with commands

mkdir drupalsites
rsync -av jan@twinkle.bms.cityu.edu.hk:~/drupalsites/hybw drupalsites

 

4) Setup apache to point to hybw (NOT NEEDED IF YOU RAN STEP 2.5)

cd /etc/apache2/sites-available/
sudo gedit 000-default.conf 

Then edit the config file so that the <VirtualHost *:80> entry contains the following info:

ServerName howyourbrainworks.net
DocumentRoot /home/jan/drupalsites/hybw/web
<Directory /home/jan/drupalsites/>
  Require all granted
  Options FollowSymLinks
  AllowOverride All
</Directory>
<Directory /home/jan/drupalsites/hybw/web/>
  #Options Indexes FollowSymLinks Multiviews
  Options FollowSymLinks
  AllowOverride All
  Require all granted
</Directory>

   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined
 

and then 

sudo service apache2 restart

5) Create a new  empty database in mysql to receive the backup:

sudo mysql
CREATE DATABASE hybw;

CREATE USER hybwUSER@localhost IDENTIFIED BY '#######';

GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER
ON hybw.* 
TO hybwUSER@localhost;
grant LOCK TABLES on hybw.* to hybwUSER@localhost;
grant CREATE TEMPORARY TABLES on hybw.* to hybwUSER@localhost;

flush privileges;

quit;

6) Run installation on new empty database.

cd web/sites/default
mv settings.php settings.bak

Now use browser to navigate to site and run through install procedure with new empty database. (This can also be done with drush commands if you prefer)

7) Copy database from twinkle and restore it locally with commands

(Note that in the following example, the file name HYBW.11-02-2020.sql.gz needs to correspond to the most up to date backup file available, and the user name  and database name hybw need to be correct/adapted. These can be looked up in the appropriate backup script such as "backupHYBWdatabase.sh"

Now reinstate database from backup:

mkdir drupalsites/DatabaseBackups
rsync -av jan@twinkle.bms.cityu.edu.hk:~/drupalsites/DatabaseBackups/HYBW.11-02-2020.sql.gz drupalsites/DatabaseBackups
cd drupalsites/DatabaseBackups/
gunzip HYBW.11-02-2020.sql.gz
sudo mysql -u root -p hybw < HYBW.11-02-2020.sql

Check that the database was restored, and then create database user with:

sudo mysql
use hybw;
show tables;
quit;

8) Cleaning up

  • Do a cache rebuild with vendor/bin/drush cr.
  • Log into web site and go to admin status report. You will need to do a database update.
  • Remove write permission chmod o-w web/sites/default/settings.php and chmod o-w web/sites/default