Question: How do I dump a MySQL database while it's serving traffic?
Try: mysqldump --single-transaction --quick --lock-tables=false --routines --triggers --events -u backup -p production_db | gzip > backup_$(date +%Y%m%d).sql.gz
Info: --single-transaction creates a consistent snapshot without locking tables for InnoDB. --quick streams rows instead of loading the entire result into memory, while --routines, --triggers, and --events include stored procedures, triggers, and scheduled events in the backup.
Examples:
- $
mysqldump --single-transaction db | gzip | ssh backup 'cat > /backups/db.sql.gz'# Stream directly to a remote backup server - $
pg_dump -Fc production | gzip > backup.pgdump.gz# PostgreSQL database dump - $
mongodump --uri mongodb://localhost --archive=backup.gz --gzip# Compressed MongoDB export
Note: --single-transaction provides a consistent dump for transactional InnoDB tables without holding table locks for the duration of the dump. Non-transactional tables such as MyISAM require different locking considerations. Test your backup and restore process regularly to make sure the dump can actually be recovered when needed.
Leave a Reply