Open main menu

quick recipe on using mysqldump

Backup

cat ./backup.db.sh

#!/bin/sh

DB=wiki
backupdir="$HOME/backups";
if [ ! -d "$backupdir" ]; then
  mkdir -p $backupdir;
fi
backup="$backupdir/dump-$(date +%F).$(hostname)-$DB.sql";
# increment the filename if it already exists
# http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html#Shell-Parameter-Expansion
i=1
filename=$(basename "$backup") # foo.txt
extension=${filename##*.}      # .txt
file=${filename%.*}            # foo
while [ -f $backup ]; do
  backup="$backupdir/${file}.${i}.${extension}"
  i=$(( i+1 ))   # increments $i
done
/usr/bin/mysqldump $DB > $backup;
ls -al $backup;

Restore

mysql $DB < $backup