Mysqldump

From Freephile Wiki
Revision as of 12:05, 11 December 2014 by Freephile (talk | contribs) (improved file naming)

Jump to navigation Jump to search

quick recipe on using mysqldump

Backup[edit | edit source]

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[edit | edit source]

mysql $DB < $backup