LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-12-2015, 04:33 PM   #1
mike2010
Member
 
Registered: Jan 2009
Posts: 132

Rep: Reputation: 15
MYSQL db question. To close or not to close ?


hello.

I need to backup about 40 sites that have mysql db's..on an every night basis.

What I used to do (years ago) cron wise was something along the lines of :

mysqldump -uadmin -p$(cat /etc/psa/psa.my/.psa.myshadow) MyActualDB > /var/www2/fartupback/myDB.sql

And I did that for every domain. This way it would keep a copy of the closed mysql DB's ...into sql file.

BUt is that really necessary? Could I just back them all up at once, with 1 command...since their in the same location ?

var/www2/fartupback/myDB1.sql
var/www2/fartupback/myDB2.sql

And can I do it without the closed off .sql? In other words, can I just drop them all at once in a different directory location...As an open mysql table ? And if done so that way. Is the chance significantly increased that I could have a corrupt table , if/ when I ever need to restore it like that ?
 
Old 04-12-2015, 07:56 PM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,647
Blog Entries: 4

Rep: Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933
The tables won't be corrupted, in any case, but the odds are high that mysqldump won't be able to run ... and also that, while it's running, no one else will be able to write anything.

Also: Never treat a database as "a collection of files," even if that is physically true. They're a database, and they must always be manipulated only by the SQL server and by its duly-appointed utilities. (Also, don't assume that the host operating system will actually prevent you from trying . . . so, "don't try.")
 
1 members found this post helpful.
Old 04-12-2015, 10:14 PM   #3
mike2010
Member
 
Registered: Jan 2009
Posts: 132

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by sundialsvcs View Post
The tables won't be corrupted, in any case, but the odds are high that mysqldump won't be able to run ... and also that, while it's running, no one else will be able to write anything.

Also: Never treat a database as "a collection of files," even if that is physically true. They're a database, and they must always be manipulated only by the SQL server and by its duly-appointed utilities. (Also, don't assume that the host operating system will actually prevent you from trying . . . so, "don't try.")
thanks.

so u recommend closing them and.SQL file'ing them first?

I brought that issue up...since I had a recent db failure with plesk CP. And tech support mentioned to back things up that way -

http://kb.odin.com/en/6586

Code:
I. Force InnoDB Recovery
Stop mysqld and back up all files located in the /var/lib/mysql/ directory:
# /etc/init.d/mysqld stop
# mkdir /root/mysql_backup
# cp -r /var/lib/mysql/* /root/mysql_backup/
thoughts ?
 
Old 04-13-2015, 02:11 AM   #4
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,474

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
As has been said DO NOT try and do things at O/S level with the running database files, there will be inconsistencies.

While you CAN dump all databases to a .sql file in one go it's much better practise to do it for each individual database as it makes restoration etc. easier to deal with if it's a single file per database rather than having to process the whole dump for a single database or table.

If you're at the stage where you've 40+ sites then have you considered doing MySQL replication? With replication you'll have an off-site "hot" copy of the database, and as it's on a replication client you can stop the replication, shut down the database, lock tables for backups etc. etc. and then restart replication and it'll all catch up.

This is my basic backup script http://centos.tips/mysql-backup/ which has options to run on a replication client or on a main server doing locking. It can also compress and optionally copy the files to another backup server.
 
Old 04-13-2015, 02:46 AM   #5
mike2010
Member
 
Registered: Jan 2009
Posts: 132

Original Poster
Rep: Reputation: 15
nice script ^

But ok, i'll do everything the .sql way.

Quote:
mysqldump -uadmin -p$(cat /etc/psa/psa.my/.psa.myshadow) MyActualDB > /var/www2/fartupback/myDB.sql
Since I just have to make the cron for each site once, and it's good forever, and backs up everynight. then my automatic backup (disk-to-disk hardwired) runs everynight around 3am to include the /var/www2/fartupback/* location. (all the .sql files)

thanks bud.
 
Old 04-13-2015, 02:55 AM   #6
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,474

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
You don't need to cron separately for each site, my script will allow you to specify in DBLIST so you can have "MyActualDB MyActualDB2 MySite MySite2" and it'll go through the list and do a separate dump for each site.
 
Old 04-13-2015, 04:39 AM   #7
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Quote:
Originally Posted by mike2010 View Post
Code:
I. Force InnoDB Recovery
Stop mysqld and back up all files located in the /var/lib/mysql/ directory:
# /etc/init.d/mysqld stop
# mkdir /root/mysql_backup
# cp -r /var/lib/mysql/* /root/mysql_backup/
thoughts ?
Yes, this is absolutely the wrong way. First your database is not accessible during this process. Secondly, mysqldump is provided to make backups of a database while it is running. Third the sum of the files is not necessarily a valid database.
Quote:
Originally Posted by sundialsvcs
The tables won't be corrupted, in any case, but the odds are high that mysqldump won't be able to run ... and also that, while it's running, no one else will be able to write anything.
Do you mean that during mysqldump no one is able to write in the database? AFAIK mysqldump is transparant to the user's access because mysqldump is running from within the database. Any write action which could interfere are simply deferred until the dump is completed. Therefor mysqldump should be preferred (and in fact the only) way to back up a database.

jlinkels
 
Old 04-13-2015, 08:59 AM   #8
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
mysqlhotcopy never gets any "love".
 
Old 04-13-2015, 09:20 AM   #9
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,474

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Quote:
Originally Posted by Habitual View Post
mysqlhotcopy never gets any "love".
Because the world and its dog use INNODB tables for anything serious.
 
Old 04-13-2015, 12:46 PM   #10
mike2010
Member
 
Registered: Jan 2009
Posts: 132

Original Poster
Rep: Reputation: 15
how about this route

Quote:
mysqldump -u username -ppassword all-databases > dump.sql
( hiding...incase he gets yelled at again..)

and does the above dump them with their own sql names? (news.sql , bikes.sql , etc) Or does it just throw everything in 1 big sql file .

Last edited by mike2010; 04-13-2015 at 12:49 PM.
 
Old 04-13-2015, 12:53 PM   #11
mike2010
Member
 
Registered: Jan 2009
Posts: 132

Original Poster
Rep: Reputation: 15
and should apache (httpd) be turned off, when doing such?

please respond quick (if possible)

I appreciate it much.
 
Old 04-13-2015, 01:40 PM   #12
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,474

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Quote:
Originally Posted by mike2010 View Post
and does the above dump them with their own sql names? (news.sql , bikes.sql , etc) Or does it just throw everything in 1 big sql file .
That dumps them in to one big sql file. Read the second paragraph of post #4 for my thoughts on this.
 
Old 04-13-2015, 05:41 PM   #13
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Quote:
Originally Posted by mike2010 View Post
and should apache (httpd) be turned off, when doing such?
No why?

Maybe you should sit and lean back for a while and understand how you interact with MySQL. What mysqldump does. How mysqldump interacts with other database processes. Hoe MySQL handles various concurrent jobs.

Again, mysqldump is a mysql process which lives together perfectly with live transactions on a live database.

And frankly I don't see the problem in backing up 40 databases in separate files. With a bash script you can perfectly well quiery mysql for all names of all databases and put those in a list. Then, using this list, you can exececute mysqldump on each and everyone of them.

jlinkels
 
Old 04-13-2015, 10:26 PM   #14
mike2010
Member
 
Registered: Jan 2009
Posts: 132

Original Poster
Rep: Reputation: 15
I was checking out the script TenTenths recommended just now. It seems decent, but there's a much shorter one here that was also recommended to me -

http://dev.mensfeld.pl/2013/04/backu...eparate-files/

If someone could help me get this right with this admin shadow'd password thing...i'd be really happy.

this part - -uadmin -p$(cat /etc/psa/psa.my/.psa.myshadow)

if u don't feel like clicking link..code is below

Code:
#! /bin/bash

TIMESTAMP=$(date +"F")
BACKUP_DIR="/backup/$TIMESTAMP"
MYSQL_USER="backup"
MYSQL=/usr/bin/mysql
MYSQL_PASSWORD="password"
MYSQLDUMP=/usr/bin/mysqldump
 
mkdir -p "$BACKUP_DIR/mysql"
 
databases=`$MYSQL --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema)"`
 
for db in $databases; do
  $MYSQLDUMP --force --opt --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db | gzip > "$BACKUP_DIR/mysql/$db.gz"
done

Last edited by mike2010; 04-13-2015 at 10:27 PM.
 
Old 04-14-2015, 11:30 PM   #15
mike2010
Member
 
Registered: Jan 2009
Posts: 132

Original Poster
Rep: Reputation: 15
looks like nobody cares..

 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Gimp 2.8: So Close, Yet So Far, Yet So Close LXer Syndicated Linux News 0 03-29-2012 11:42 PM
close terminal mean close background process? Niceman2005 Linux - General 7 05-27-2010 02:58 PM
how to close port 1050 please help how to close it. mr norm Linux - General 1 09-12-2008 07:57 AM
mplayer 'close all' doesn't close anything allelopath Linux - Software 2 12-08-2005 09:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration