|
Howto: Backup Mysql databases using the Linux command line. |
|
|
|
Written by Josh
|
|
Wednesday, 16 July 2008 |
|
So you want to make an automated backup of your mysql database using linux? It's pretty simple. You'll need ssh access to your server to do this. - SSH into your server.
- Create a new directory where you want to store your mysql backup files. The easiest place to make the directory is in your home directory (eg. /home/user/mysql ). To create the directory type mkdir /home/user/mysql
- I recommend setting the directory to not be world readable/writable. To do this: chmod 700 mysql
- Change directory to the new directory you made in the previous step: cd mysql
- Now to create the script. Use your favorite commandline editor. I prefer pico (or nano): nano -w mysqlbackup.sh
- Copy and paste the following text into the editor.
#!/bin/bash
DBNAME=databasename DBUSER=databaseuser
DBPASS=password DATE=`date +%Y%m%d`
mysqldump --opt -u $DBUSER -p$DBPASS $DBNAME > /home/username/mysql/mysql-database-username-backup-$DATE.sql
tar -cz -C / home/username/mysql/mysql-database-username-backup-$DATE.sql > /home/username/mysql/mysql-database-username-backup-$DATE.sql.tar.gz
rm /home/username/mysql/mysql-database-username-backup-$DATE.sql
|
- Edit the bold text to fit your environment.
- Hit control X to exit. Save the file.
- I would recommend making the file to be not world readable/writable. Only your account should be able to access the file. To do this: chmod 600 mysqlbackup.sh
- Now run the script to make sure it works.
- Add the script to a cronjob to automate the backup weeky/daily/hourly.
|
|
Last Updated ( Wednesday, 16 July 2008 )
|