Jenkins jobs speed up

Running a test or build server’s database service in tmpfs do reduce build time.
If an application uses some service like MySQL or a file server building the application involves a lot of the relevant tests.
Most often the data for the tests is being seed every job run. If there are spare memory on the server it would reduce the job build/test time quite significantly if the service is running in tmpfs.

However, like in a case with MySQL the mysql database has to initiated and may be some database user set up before the service can be used.
Below is a MySQL example. Feel free to change the approach.

  1. create the data snapshot

  2. service stop mysqld
    cd /root # just to avoid being anywhere in /var/lib/mysql path
    mkdir /backup
    tar czf /backup/mysql.tgz # create mysql binary archive (snapshot)

  3. clean up the data

  4. rm -rf /var/lib/mysql/*

  5. mount the tmpfs

  6. mountpoint -q /var/lib/mysql && umount /var/lib/mysql
    mount -t tmpfs -o size=4G,mode=0755,uid=mysql,gid=mysql tmpfs /var/lib/mysql
    tar xzf /backup/mysql.tgz -C /

  7. start the service
  8. service start mysqld

A bash script example to restore the service to snapshot

#!/bin/bash
service stop mysqld
mountpoint -q /var/lib/mysql && umount /var/lib/mysql
mount -t tmpfs -o size=4G,mode=0755,uid=mysql,gid=mysql tmpfs /var/lib/mysql
tar xzf /backup/mysql.tgz -C /
service start mysqld