Math Jazz — Mathias Bynens’s shizzle, y’all



Note: This site might seem inactive… That’s because it is. Don’t worry though, I’m still coding webpages and stuff! If you’re interested, I suggest you get a translator and head over to Qiwi; or you could just check the latest site we’ve been working on: Apotheek Goethals – Debrabandere. Enjoy!

Creating and restoring MySQL backups using the command line

As some of you, dear visitors, noticed and even pointed out to me (thanks again!), Math Jazz was even more of a total mess than it usually is this morning. Luckily, I had database backups, but because of the friggin’ phpMyAdmin that doesn’t quite seem to support UTF-8 those created another problem. To cut a long story short: this site looked well funky, and in order to fix it I needed a way to execute MySQL queries from a file without messing with its contents first. Eventually, I found a 1337 solution, which I’m now going to share with you. However, first things first.

Backing up MySQL data

Earlier this week, my WordPress dashboard pointed me to this simple MySQL backup script. I’m pretty new to BASH and working with the shell in general, but yet I managed to customize the script for a bit.

export d=$(date +'%Y-%m-%d')
mkdir -p backup/$d
for i in `echo "show tables" | mysql -uusername -ppassword database|grep -v Tables_in_`;
do
 echo Backing up $i...
 mysqldump --add-drop-table --allow-keywords -q -a -c -uusername -ppassword database $i > backup/$d/$i.sql
done
echo 0m6, d0n3! ph34r my 1337 b4kcUp sk1ll2!!1

Store this file on your server as, say, sql-backup.sh. You can then execute it by typing bash sql-backup.sh in the shell.[1] The script will create a new folder, namely backup/ in the current working directory, and then save the backup files in subfolders based on the date. For example, if I’d run the script today, I would have my database backed up in the shiny new backup/2005-07-02/ folder. w00t!

Note: I’m currently using PuTTY for this kind of 1337 stuff, which seems to work fine — but then again I really don’t know of any alternatives.

Restoring MySQL data from backups

Now, let’s put these backups to use. As I mentioned in the preface of this article, I couldn’t simply import the .sql files using phpMyAdmin without messing things up. At first, I thought of writing a PHP script that would take a backup file, parse it contents and then execute the SQL stuff, but then I realised there just had to be an easier way. After Googlin’ around for a while, I stumbled upon mysql, the command-line tool. Especially the information on executing SQL statements from a text file proved itself useful whilst writing the following BASH script.

cd backup/2005-07-02/
for i in *;
do
 echo Restoring $i...
 mysql -uusername -ppassword database < $i
done
echo 0m6, d0n3! ph34r my 1337 r3570r3 sk1ll2!!1

This would restore the SQL data from all files in the backup/2005-07-02/ directory into the specified database. You’ll obviously have to edit the first line to make it match the backup folder you want to use.

Note: In the above scripts, you of course have to replace every instance of username and password with your MySQL username and password respectively, whereas database should be replaced by the MySQL database name you’re working with.

Filed under Site News, MySQL, BASH · July 2nd, 2005

Comments (9)

Listed below are the responses for this entry.

  1. Phu:
    This commenter’s Gravatar

    Glad it’s all back to normal. I used the same technique when migrating webhosts.

    Comment posted on July 2nd, 2005 @ 3:46 pm
  2. Remi Prevost:
    This commenter’s Gravatar

    I didn’t understand a single line of your code; but it reminds me that I had to backup my database, thanks ;)

    Comment posted on July 2nd, 2005 @ 8:07 pm
  3. Dante:
    This commenter’s Gravatar

    Ma7h1a5, 17’5 n07 1337 70 k33p u51n6 7h3 w0rd 1337.

    Comment posted on July 2nd, 2005 @ 9:39 pm
  4. Basil Crow:
    This commenter’s Gravatar

    Why make the directory with 777 permissions, isn’t the default umask good enough?

    Comment posted on July 3rd, 2005 @ 12:43 am
  5. Mathias:
    This commenter’s Gravatar

    Why make the directory with 777 permissions, isn’t the default umask good enough?

    I just have this thing for CHMOD 777. The default umask would be just as good, but really — all this doesn’t matter as you won’t place your backup files in a location that can be viewed from the web, right?

    Comment posted on July 3rd, 2005 @ 9:27 am
  6. Krijn Hoetmer:
    This commenter’s Gravatar

    And let’s keep correcting each other; phpMyAdmin does support UTF-8 ;-) At least for the latest version it’s on by default. I remember I had to wreck some configuration files for 2.5.4 (/libraries/select_lang.lib.php) but I don’t know which version you have and/or if you’re able to edit the source files.

    Also, if you force your browser to use UTF-8, phpMyAdmin suddenly supports it as well.

    Comment posted on July 4th, 2005 @ 11:16 am
  7. Jan!:
    This commenter’s Gravatar

    I just have this thing for CHMOD 777. The default umask would be just as good, but really — all this doesn’t matter as you won’t place your backup files in a location that can be viewed from the web, right?

    Actually, it can be dangerous on shared hosts. A mode of (0)777 means that anyone on the server can read from, write to and execute the file or directory. More than a bit dangerous, I’d say.

    Comment posted on July 4th, 2005 @ 6:01 pm
  8. Mathias:
    This commenter’s Gravatar

    phpMyAdmin does support UTF-8 ;-) At least for the latest version it’s on by default. I remember I had to wreck some configuration files for 2.5.4 (/libraries/select_lang.lib.php) but I don’t know which version you have and/or if you’re able to edit the source files.

    Also, if you force your browser to use UTF-8, phpMyAdmin suddenly supports it as well.

    Thanks for the info, Krijn! Unfortunately, I can’t have my way in the source files, but perhaps I’ll install phpMyAdmin somewhere on mathibus.com rather than on my übersecret server, so I can have that privilege. :) Oh, and the browser tweak trick thing sounds even more cool!

    Actually, it can be dangerous on shared hosts. A mode of (0)777 means that anyone on the server can read from, write to and execute the file or directory. More than a bit dangerous, I’d say.

    You’re right — I can’t expect all three people who’ll ever use this script not to use shared host, as I do. mkdir -m 777 -p backup/$d mkdir -p backup/$d.

    Comment posted on July 7th, 2005 @ 1:12 pm
  9. Favad Ali Khan:
    This commenter’s Gravatar

    It’s very good and helped me in taking backup of my db.

    Thanks a lot

    Comment posted on November 20th, 2007 @ 8:41 am

Trackbacks & Pingbacks (1)

Listed below are resources on the web that mention this article.

  1. Ian’s Blog: Creating and restoring MySQL backups using the command line by Math Jazz:
    This commenter’s Gravatar

    Creating and restoring MySQL backups using the command line by Math Jazz
    […] Back up WordPress MySQL database […]

    Pingback made on July 2nd, 2005 @ 4:25 pm