It is very important to do backup of your MySql database, you will probably realize it when it is too late.
A lot of web applications use MySql for storing the content. This can be blogs, and a lot of other things. When you have all your content as html files on your web server it is very easy to keep them safe from crashes, you just have a copy of them on your own PC and then upload them again after the web server is restored after the crash. All the content in the MySql database must also be backed up. A lot of web service providers say they do backup of all the files, but you should never blindly trust them. If you have spent a lot of time making the content and it is only stored in the Mysql server, you will feel very bad if it gets lost for ever. Backing it up once every month or so makes sure you never loose too much of your work in case of a server crash, and it will make you sleep better at night. It is easy and fast, so there is no reason for not doing it.
Backup of Mysql database
It is assumed that you have phpMyAdmin installed since a lot of web service providers use it.
0. Open phpMyAdmin.
1. Click Export in the Menu to get to where you can backup you MySql database. Image showing the export menu.
2. Make sure that you have selected to export your entire database, and not just one table. There should be as many tables in the export list as showing under the database name.
3. Select"SQL"-> for output format, Check "Structure" and "Add AUTO_INCREMENT" value. Check "Enclose table and field name with backquotes". Check "DATA", check use "hexadecimal for binary field". Export type set to "INSERT".
4. Check "Save as file", do not change the file name, use compression if you want. Then click "GO" to download the backup file.
Image showing step 2-4.
Restoring a backup of a MySql database
1. To restore a database, you click the SQL tab.
2. On the "SQL"-page , unclick the show query here again.
3. Browse to your backup of the database.
4. Click Go.
Image showing step 1-4.
phpMyAdmin has some file size limits so if you have large databases it may no be possible to backup using phpMyAdmin. Then you have to use the command line tools that comes with Mysql. Please note that this method is untested.
Mysql backup without phpMyAdmin
PHPMyAdmin can't handle large databases. In that case straight mysql code will help.
1. Change your directory to the directory you want to dump things to:
user@linux:~> cd files/blog
2. Use mysqldump (man mysqldump is available):
user@linux:~/files/blog> mysqldump --add-drop-table -h mysqlhostserver -u mysqlusername -p databasename (tablename tablename tablename) | bzip2 -c > blog.bak.sql.bz2 Enter password: (enter your mysql password) user@linux~/files/blog>
Example: mysqldump --add-drop-table -h db01.example.net -u dbocodex -p dbwp | bzip2 -c > blog.bak.sql.bz2 Enter password: my-password user@linux~/files/blog>
The bzip2 -c after the pipe | means the backup is compressed on the fly.
Mysql restore without phpMyAdmin
The restore process consists of unarchiving your archived database dump, and importing it into your Mysql database.
Assuming your backup is a .bz2 file, creating using instructions similar to those given for Backing up your database using Mysql commands, the following steps will guide you through restoring your database :
1. Unzip your .bz2 file:
user@linux:~/files/blog> bzip2 -d blog.bak.sql.bz2
Note: If your database backup was a .tar.gz called blog.bak.sql.tar.gz file, then,
tar zxvf blog.bak.sql.tar.gz
is the command that should be used instead of the above.
2. Put the backed-up sql back into mysql:
user@linux:~/files/blog> mysql -h mysqlhostserver -u mysqlusername -p databasename < blog.bak.sql Enter password: (enter your mysql password) user@linux~/files/blog:>