Developing and testing against live

This blog post is more than 7 years old, so the content may be out of date.

I usually develop locally on a VM. I generally have more confidence if I'm developing on a platform which is as much like live as possible - and this usually means copying the live database to my local dev environment.

Here's a script I use to help develop this blog:

#!/bin/bash
 
# location of the remote file
origin="deglos.com:/backups/current.sql.gz"
 
# Local path to store the db backup
path=/var/www/vhosts/blog.deglos.com/database/
file=current.sql.gz
 
# Local database name
db=deglos_blog
 
##################################################
 
 
# Unix timestamps...
#   Modified-time of the file:  stat -c %Y
#   Current time:               date +%s
#   1 day (in seconds):         86400
if [ -e $file ]; then
  age=$[`date +%s` - `stat -c %Y $path$file`]
fi
 
if [ ! $age ] || [ $age -gt 86400   ]; then
  echo "Fetching remote file"
  scp $origin $file
fi
 
echo "Extracting the database backup from the gzip archive"
# Copy to /tmp
cp -f $path$file /tmp
 
# gunzip the file
gunzip -f /tmp/$file 
 
# remove the .gz from the file variable
file=${file:0:${#file}-3}
 
# reset the DB
echo "Removing the old database"
mysql -e "DROP DATABASE $db; CREATE DATABASE $db;"
 
# Import the DB
echo "Importing from the database backup"
mysql $db < /tmp/$file
 
echo "Done"

The live database is backed up every day (on a cron-job) with this script:

#!/bin/bash
 
# Store backup in this directory:
dir="/backups"
 
# Backups will be stored in the pattern $filename-date-time.sql.gz
filename="blog.deglos.com"
 
# Database to backup
db=blog
 
#################################
 
# Bash script to dump the blog database, zip it, and move it to $dir.
date=`date +%Y%m%d`
time=`date +%H%M%S`
filename=$filename-$date-$time.sql
 
mysqldump -C $db > /tmp/$filename
gzip /tmp/$filename
mv /tmp/$filename.gz $dir
 
# symlink current.sql.gz
rm -f $dir/current.sql.gz
ln -s $dir/$filename.gz $dir/current.sql.gz

It's not particularly elegant, but it's simple and functional :-)

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <apache>, <bash>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.