Tar and mysqldump Are Your Friends

December 05, 2007

The first step in my site migration project is to backup all my data, all my content, everything. Since my hosting platform is Linux based, this was surprisingly easy.

First I issued this command:

$ tar cvf archive.tar *

at the root of my account, /usr/www/users/mnichols, to create an archive off everything I have stored under my account at Pair.

Next I compressed this archive using gzip. Like so:

$ gzip archive.tar

The resulting file, archive.tar.gzip, was then copied to my local computer.

Some parts of this site, and parts of the other sites I have hosted over the years at Pair, have made use of MySQL databases; I've used various referrer and site monitoring tools that all required relational databases for support. Before leaving Pair I want to back up the contents and structure of these data stores.

Using MySQL on my laptop I can issue a command in this format:

$ mysqldump --add-drop-table -h <hostname> -u <username> -p <databasename> | bzip2 -c database_yyyymmdd.bak.sql.bz2

to not only dump the database, but to compress it as well. The resulting file will be stored in the directory on my local machine where I ran the command.

Once all 10 MySQL databases were dumped and compressed using this command I had a complete back up of my site: the presentation (HTML/CSS) files in the public_html directory and the underlying databases that feed the various sites their content.

Author's profile picture

Mark H. Nichols

I am a husband, cellist, code prole, nerd, technologist, and all around good guy living and working in fly-over country. You should follow me on Twitter.