Highland Linux User Group

Linux Community
It is currently Mon Feb 06, 2012 9:36 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Mysql backup
PostPosted: Thu Feb 18, 2010 5:42 am 
Offline
Moderator
User avatar

Joined: Tue Oct 03, 2006 12:27 pm
Posts: 162
Location: Inverness UK
There are a lot of scripts at which allow you to back up your database such as PhpMyadmin (http://www.phpmyadmin.net/home_page/downloads.php), mysqldumper (http://www.mysqldumper.de/) and etc... those are 2 of the best scripts for backing up your database.

But i have a better way (in my opinion)

Since having those php scripts i have to be careful about secure those script, security with open source software is a big issue, i intend to make things as simple as possible.

The beauty of Linux is giving you the Freedom, you can do whatever you want almost everything from just a command line interface.
you're hopeless with Windows servers.

lets get down to backing up business:
in Ubuntu i use mysqldump to back up database. to back up a specific database i use:

Code:
mysqldump --opt -uUSER -pPassword -hHOST DATABASE > /home/user/direcroty/to/backup_db.sql


Replase USER with your mysql username, Password with your password, HOST to servername and DATABASE to your database name
ex:
Code:
mysqldump --opt -unguyen -phlugcouk -hlocalhost forum > /home/user/direcroty/to/backup_db.sql


to avoid override daily backup you can add "$(date)" to the file name
ex:
Code:
mysqldump --opt -unguyen -phlugcouk -hlocalhost forum > /home/user/direcroty/to/backup_db$(date).sql

out put would look like this:

"backup_db Thu Feb 18 03:40:10 CET 2010.sql"

if you want to compress it to save space:

Code:
mysqldump -unguyen -phlugcouk -hlocalhost forum | gzip > /home/user/direcroty/to/backup_db$(date).sql.gz


out put would look like this:

"backup_db Thu Feb 18 03:40:10 CET 2010.sql.gz"

to un pack the file *.gz :

Code:
gzip -d /home/user/direcroty/to/backup_db.sql.gz


-------------------------------

now you have done the backing up, what about restore it?
simple
open your CLI:

connect to mysql server:

Code:
#mysql -uYouruser -hlocalhost -p


type in your password then you can see something like this:

Code:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 47719
Server version: 5.1.37-1ubuntu5.1 (Ubuntu)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>


now type

Code:
#use database;


change database to database you wanna restore

now do the restore:

Code:
#source > /home/user/direcroty/to/backup_db.sql


that's it type \q to exist mysql.

_________________
Computers are like air conditioners, They stop working properly when you open Windows!


Top
 Profile  
 
 Post subject: Backing up daily
PostPosted: Thu Feb 18, 2010 5:47 am 
Offline
Moderator
User avatar

Joined: Tue Oct 03, 2006 12:27 pm
Posts: 162
Location: Inverness UK
to back up daily you should use cron job to do the job

open your CLI type:

Code:
#crontab -e


add in
Code:
00 21 * * * mysqldump -unguyen -phlugcouk -hlocalhost forum | gzip > /home/user/direcroty/to/backup_db$(date).sql.gz

(all in 1 line)
Ctrl+X --> yes to save cron tab

the cron will run at 9:00pm every day

change it to suit your need.

any problem with cron please do not hesitate to ask.

_________________
Computers are like air conditioners, They stop working properly when you open Windows!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2010 Highlands Linux Users Group