Forum Moderators: open

Message Too Old, No Replies

Backing up MySQL to a remote server

         

csdude55

6:41 pm on May 27, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I've set up a 20G remote server, so now I'm trying to figure out the "best" way to backup MySQL to it.

Both servers are using CentOS. I'm using WHM / cPanel on the main server, but not the 20G backup server. My entire account is about 70G so I don't want to back up the whole thing... just the 12G MySQL database.

WHM has a Backup option, but it wants to do the whole account :-( Which is totally unnecessary, takes forever, and REALLY slows down the server while it's running! I thought about having it run backups daily at midnight, then set up a cron on the new server to rsync just the database from the backup, but the path changes every time (it creates a directory with the data) so that's a little complicated. So I consider that to be a last resort.

I can run mysqldump via MySQL, but it takes close to an hour to complete and the site throws errors while it's running. But I think that I could do it via PHP and a cron so that it would use the same directory and just always overwrite yesterday's backup. So it's a next-to-last resort.

I could just use rsync on the new server to copy the files from /var/lib/mysql/my_account/, but I think that would/could cause errors in the data when something is written during the copy. And that would almost definitely happen!

This server is considerably smaller than the main server so mirroring probably isn't the best solution.

I thought about modifying all (or almost all) of my scripts to insert to each database separately, but that would make every post slow :-( I obviously wouldn't use it to backup any logs or anything, just important data, but I would still worry that adding extra time each time a user submits something could cause a long term loss of traffic.

What else can I do? Priorities being minimal impact on performance and reliability. And I'm open to paying for a program if it's the best choice.

universenet

6:56 pm on May 27, 2021 (gmt 0)

Top Contributors Of The Month



csdude55
do you using cron for make backup periodicaly?

csdude55

7:20 pm on May 27, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



No, I used to use WHM's backup tool until recently. Last time I ran it, the server load jumped to 50+, and the server was completely unresponsive until it timed out after 2 hours.

I can't find a way to get it to JUST backup MySQL, so it tries to back up all of the user-submitted images that don't need to be backed up.

csdude55

8:13 pm on May 27, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



So maybe this... in PHP on the main server (not tested!):

<?php

$dump_result = $rsync_result = false;

exec("mysqldump --user=[USER] --password=[PASS] --host=localhost --single-transaction --quick database_name > /backup/database_name_backup.sql", null, $dump_result);

if ($dump_result)
exec("rsync -a /backup username@remote_host:/backup", null, $rsync_result);

if ($rsync_result)
mail('hostmaster@example.com',
'MySQL Backup complete',
'Done');

else
mail('hostmaster@example.com',
'MySQL Backup failed',
'Uh oh');

?>


Then set up a cron to run this script at like 2am every night.

In theory, "--single-transaction" would prevent it from locking the table, right? And "--quick" should prevent it from buffering in memory, so it wouldn't crash the server?

universenet

8:43 pm on May 27, 2021 (gmt 0)

Top Contributors Of The Month



No, I used to use WHM's backup tool until recently. Last time I ran it, the server load jumped to 50+, and the server was completely unresponsive until it timed out after 2 hours.


Fastest way for backup is using ssh and cron
backpu of even big mysql going good with ssh command

Creating backup
mysqldump -u root -p databasename > database.sql
or in oposite way (restore...import backup)
mysql -u root -p databasename < database.sql
and you can combine with cron depends of time when bakup finishing
I am doing this almost everyday
I do not know if you have root access , I am using my dedicated server
but you can always ask provider for root access because somtimes need just this

lammert

10:38 am on May 28, 2021 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Percona XtraBackup is an open source backup utility which doesn't lock the database. I use it for Initial State Transfers (full restores) between nodes in a multi-master cluster. As you are using MariaDB per your other posts here on the board, Mariabackup might be a better solution as it is fully supported by your database configuration.

LifeinAsia

4:44 pm on May 30, 2021 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Creating backup
mysqldump -u root -p databasename > database.sql

You can also use the gzip parameter to decrease the size of the backup file.
mysqldump -u root -p [password] [DatabaseName] | gzip > [FileName]
The slight downside is that you have to un-gzip the file when you restore, which may add a few seconds/minutes to the restore time.

I also had issues with the DB locking up during backups, so I use the following options for my daily B/Us:
--single-transaction --quick --lock-tables=false