Menu Content/Inhalt
Home arrow Howtos arrow Linux Command Line Tips arrow Howto: Backup Mysql databases using the Linux command line.
Howto: Backup Mysql databases using the Linux command line. Print E-mail
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.
 
  1. SSH into your server.
  2. 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
  3. I recommend setting the directory to not be world readable/writable. To do this: chmod 700 mysql
  4. Change directory to the new directory you made in the previous step: cd mysql
  5. Now to create the script. Use your favorite commandline editor. I prefer pico (or nano): nano -w mysqlbackup.sh
  6. 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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  1. Edit the bold text to fit your environment.
  2. Hit control X to exit. Save the file.
  3. 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
  4. Now run the script to make sure it works.
  5. Add the script to a cronjob to automate the backup weeky/daily/hourly.
Last Updated ( Wednesday, 16 July 2008 )