Tag Archives: mysql

Scripted MySQL DB Backups

Background

Recently my homeassistant local DB (RPI-based), which is an SQLite DB, got corrupted during an upgrade, so this was an incentive to migrate to holding the data in the MySQL server I have running anyways as;

  • This is more reliable
  • Less writes to the SDcard

But it made me realise that my MySQL server backups weren’t exactly regular, and I have a number of things using the DB (kodi, now homeassistant, website, etc.) that I’d be quite annoyed to lose.

Running backups using mysqldump is a normal way of doing things, but has the problem of recording the password in some way, shape or form. Fortunately to do a backup, only read-type permissions are required.

Backup Account

We create an account on the server, BACKUP_USER, that can only connect from the localhost, that has read-only permissions for all databases and tables

GRANT LOCK TABLES, SELECT, SHOW VIEW ON *.* TO 'BACKUP_USER'@'localhost' IDENTIFIED BY 'secretReadonlyPassword';
FLUSH PRIVILEGES;

Script and Cron Job

Then we have a simple bash script, executed daily/weekly, to trigger the backups, and only keep X number around.

#!/bin/bash

# Set the backup date
BACKUP_DATE=`date +%Y-%m-%d`
BACKUP_DIR=/backup_location/sql_database
BACKUP_HISTORY=20

# Dump the database
mysqldump -u BACKUP_USER -p 'secretReadonlyPassword' --all-databases --add-drop-database | gzip -c > $BACKUP_DIR/dump_$BACKUP_DATE.sql.gz

# Remove all but the latest backups
cd $BACKUP_DIR
ls -t | tail -n +$BACKUP_HISTORY | xargs rm --

Migrating WordPress Tiny EC2 Instance to Raspberry Pi

Having hosted this blog on a tiny AWS EC2 instance for some time, mostly to gain exposure to EC2, I decided to migrate it to a spare Raspberry Pi and use my home network connection. For the most part this was a fairly seamless transition.

Raspberry Pi Preparation

Basic setup was updating and locking down the RPI. The next step was to install a webserver. Rather then Apache2, I decided to install nginx instead as it is supposed to be a little lighter on resources.

sudo apt-get install nginx php5-fpm
sudo service nginx restart

Try to connect to http://$SERVER_IP and you should see a “Welcome to NGINX” page. If not then something has gone wrong and you need to look at /var/log/nginx/error.log

Next up is to install MySQL and the PHP connection API

sudo apt-get install mysql-server php5-mysql

Be sure to set a strong MySQL root password

At this point I suggest switching your MySQL config with the my-small.cnf as we are working on a relatively low memory device

sudo cp /usr/share/doc/mysql-server-5.5/examples/my-small.cnf /etc/mysql/my.cnf
sudo service mysql restart

Prepare the WordPress MySQL Account

mysql -u root -p
CREATE DATABASE `wordpressdb`;
GRANT ALL PRIVILEGES ON `wordpressdb`.* TO 'wordpress'@'localhost' IDENTIFIED BY 'long_complicated_wordpress_password';
FLUSH PRIVILEGES;
EXIT

At this point I configured nginx to allow PHP to be executed by editing /etc/nginx/sites-enabled/default in two areas:

server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6

root /var/www;
index index.html index.htm index.php;

...

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}

WordPress Migration

For this I made use of the excellent XCloner Plugin to backup my existing server, then used the manual method to restore the entire wordpress installation on the new server. No issues…

Some Performance Improvements

First some caching helpers:

sudo apt-get install memcached php5-memcache php-apc
sudo /etc/init.d/php5-fpm restart

To take advantage in WordPress install the following plugins:

  • nginx Compatibility
  • W3 Total Cache

From the W3 Total Cache settings page

  • enable Page Cache with memcached
  • enable Object Cache with APC

Moving the RRD

First up, install rrdtool on the RPI:

sudo apt-get install rrdtool

Then dump the old RRD from the original server. This is required as the database itself is very platform specific, and cannot be just copied between architectures.

rrdtool dump ./database.rrd > database.xml

On the new server, import it into a shiny new RRD:

rrdtool restore ./database.xml ./database.rrd

DNS Changes

Since this is hosted on a home connection, which has a public IP, we cannot guarantee that the IP address will stay the same. Like most tech-savy people I use Dynamic DNS to ensure I can access my home network.

In this case, I make a CNAME record for flat-planet.net, pointing towards my dynamic DNS record, and we are done.