Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Best way to back up MySQL
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Best way to back up MySQL

I need some suggestions about how to back up MySQL databases. As I understand mysqldump might not always be the best way to go about it when in production.

I am grateful for any suggestions. Thank you.

Comments

  • $db = array ( 'db_account' , 'db_game' , 'db_misc'); foreach($db as $name){ if(!file_exists('backup/'.date('Y_m_d').'/')) { $do = mkdir('backup/'.date('Y_m_d')); exec($do); } if(!file_exists('backup/'.date('Y_m_d').'/'.$name.'/')) { $do = mkdir('backup/'.date('Y_m_d').'/'.$name); exec($do); } $tmpDir = '/tmp/'; $sqlFile = $tmpDir.$name.date('d_m_Y_H_i').'.sql'; $backupFile = $name.'_'.date('d_m_Y_H_i').'.tgz'; $create = "mysqldump -u ".$mysql_user." --password='".$mysql_pass."' --default-character-set=latin1 --hex-blob ".$dbName." > ".$sqlFile; $createZip = "tar cvzf $backupFile $sqlFile"; exec($create); exec($createZip); unlink($sqlFile); }

    mysqldump is a good way then taring the same and then lets put in a folder where rsync will later do it job.

    This code can be executed by a cronjob to make it easier.

  • Thank you!

  • This is exactly what I use for the game servers. You can also get a dropbox account + use dropboxuploader api php

    https://github.com/jakajancar/DropboxUploader

    to send to dropbox.

  • I have this in my backup script

    [[ $MYSQL == "YES" ]] && {
        DATABASES=$(echo "SHOW DATABASES" | mysql -u$DBUSER -p"$DBPW" | grep -v Database)
        for db in $DATABASES; do
            mysqldump --single-transaction --skip-events --opt -u$DBUSER -p"$DBPW" $db > $TMP/$HOSTNAME.$db.$DATE.sql
        done
        tar -zcvf $TMP/$HOSTNAME.mysql.$DATE.tar.gz $TMP/*.sql
        rm -f $TMP/*.sql; }

    As you can see, using mysqldump. Have no idea if it's the best option (probably not) but I'm not running any production databases anyway.

  • socials said: As you can see, using mysqldump. Have no idea if it's the best option (probably not) but I'm not running any production databases anyway.

    What happens when mysqldump runs while multiple tables are being updated that have relations between each other?

  • @TarballZ said:
    What happens when mysqldump runs while multiple tables are being updated that have relations between each other?

    Lock tables, backup, unlock tables.

  • @Blanoz said:
    Lock tables, backup, unlock tables.

    Dont forget foriegn key checks ( if the DB's tables have any )

  • OliverOliver Member, Host Rep
  • gbshousegbshouse Member, Host Rep

    Try Percona's backup tools

  • I do master-slave replication and daily mysqldump on the slave.

  • As @gbshouse says - use Percona's backup tools otherwise you end up with inconsistencies.

  • +1 for Percona, if you have complex mySQL database.

  • SplitIceSplitIce Member, Host Rep

    +1 again for Percona.
    Mysqldump is also reasonable for a secondary backup (i.e a partial backup for if the other backup fails).

    Personally I have a Percona backup running daily, and a mysqldump partial backup of select tables run every 6 hours (in case of failure of backup, a backdated percona backup could be used + mysqldump for important tables).

    On top of this full disk (e.g LVM) backups are also wise.

  • https://github.com/shyaminayesh/SQLBackup/blob/master/backup.sh

    small bash for backup and upload to FTP if you are interesting. :)

  • MySQLDump piped to gzip then FTP elsewhere.

  • mysqldump is certainly acceptable. a better solution if you can is to setup a slave mysql server and run the dumps on slave.

  • @datarealm said:
    mysqldump is certainly acceptable. a better solution if you can is to setup a slave mysql server and run the dumps on slave.

    ^ This.

    Assuming:

    • Replication is not broken (and the slave has a full copy)
    • You have ability to bring "backup" instance offline

    You could then use any of the above suggestions on the slave without much impact on "production" instances IMHO

  • You can also do filesystem snapshots, mount the snapshot and then dump that. That will give you a 100% point-in-time backup

Sign In or Register to comment.