6th January 2009

Automatically backing up files before making changes

Sunday, June 1st, 2008

It’s best practice (and common sense) to make a backup of a file before you edit it. Unfortunately it’s easy to forget to do this. We use this simple script below to make a time/date stamped copy of a file before launching the editor (in this case vim). We create it as /usr/local/bin/bvi.


#!/bin/bash
[[ -r $1 ]] && cp $1{,.`date +%Y%m%d-%H%M`} || echo “$1 is a new file”

vim $1

We then add the following two aliases to our ~/.bashrc file to make sure it’s run automatically when we call vim.

alias vi=/usr/local/bin/bvi
alias vim=/usr/local/bin/bvi

This isn’t a replacement for good version control of important files, but it’s a good safety net. It’s also worth noting that this can leave a lot of old copies of files laying about, so it’s work cleaning out old copies every now and again.

Check, repair and optimize all MySQL databases

Wednesday, May 21st, 2008

The following command will check, repair and optimize all databases on your MySQL server.

mysqlcheck -u root -p --auto-repair --check --optimize --all-databases

It is the equivalent of calling CHECK TABLE, REPAIR TABLE, ANALYZE TABLE and OPTIMZE TABLE for each table in each database on your server.

We tend to run this command directly after our scheduled backups.