Database_backup
Chapter 74: Database Backup and Restore
Section titled “Chapter 74: Database Backup and Restore”Comprehensive Database Backup Strategies
Section titled “Comprehensive Database Backup Strategies”74.1 MySQL/MariaDB Backup
Section titled “74.1 MySQL/MariaDB Backup”mysqldump
Section titled “mysqldump”# Full backupmysqldump -u root -p --all-databases > full_backup.sql
# Single databasemysqldump -u root -p mydb > mydb.sql
# Multiple databasesmysqldump -u root -p --databases db1 db2 db3 > backup.sql
# With optionsmysqldump -u root -p \ --single-transaction \ --routines \ --triggers \ --events \ mydb > mydb.sql
# Quick backup (no locks)mysqldump -u root -p --single-transaction --quick mydb > backup.sql
# Lock tables (for MyISAM)mysqldump -u root -p --lock-tables mydb > backup.sql
# Compressed backupmysqldump -u root -p mydb | gzip > backup.sql.gz
# Partial backup (only tables)mysqldump -u root -p mydb table1 table2 > tables.sqlRestore
Section titled “Restore”# Restore databasemysql -u root -p mydb < backup.sql
# Restore compressedgunzip < backup.sql.gz | mysql -u root -p
# Restore to different databasemysql -u root -p newdb < backup.sql
# Execute SQL directlymysql -u root -p -e "source backup.sql"Binary (Physical) Backup
Section titled “Binary (Physical) Backup”# Using xtrabackup (Percona)# Installsudo apt install percona-xtrabackup
# Full backupxtrabackup --backup --target-dir=/backup/full --user=root --password=root
# Prepare backupxtrabackup --prepare --target-dir=/backup/full
# Restorextrabackup --copy-back --target-dir=/backup/full
# Incremental backupxtrabackup --backup --target-dir=/backup/inc1 --incremental-basedir=/backup/full
# Stream backupxtrabackup --backup --stream=tar | gzip > backup.tar.gz74.2 PostgreSQL Backup
Section titled “74.2 PostgreSQL Backup”pg_dump
Section titled “pg_dump”# Plain SQL backuppg_dump -U user mydb > backup.sqlpg_dump -U user mydb -f backup.sql
# Custom format (compressed)pg_dump -U user -Fc mydb > backup.dump
# Directory format (parallel)pg_dump -U user -Fd mydb -f backup_dir
# Only schemapg_dump -U user --schema-only mydb > schema.sql
# Only datapg_dump -U user --data-only mydb > data.sql
# Compressedpg_dump -U user mydb | gzip > backup.sql.gzRestore
Section titled “Restore”# Plain SQL restorepsql -U user mydb < backup.sql
# Custom format restorepg_restore -U user -d mydb backup.dump
# Restore to different databasecreatedb -U user newdbpg_restore -U user -d newdb backup.dump
# Restore specific tablepg_restore -U user -d mydb --data-only --table=mytable backup.dumppg_basebackup (Physical)
Section titled “pg_basebackup (Physical)”# Full base backuppg_basebackup -h localhost -U replicator -D /backup/base -P -Xs
# With tar formatpg_basebackup -h localhost -U replicator -D /backup -Ft -z -P
# With replication slotpg_basebackup -h master -D /backup -U replicator -P --slot=replica_slot74.3 Automation
Section titled “74.3 Automation”Backup Script Example
Section titled “Backup Script Example”#!/bin/bashBACKUP_DIR="/backups"DATE=$(date +%Y%m%d_%H%M%S)DB_NAME="mydb"DB_USER="backup"DB_PASS="password"
# MySQL backupmysqldump -u $DB_USER -p$DB_PASS --single-transaction \ --routines --triggers $DB_NAME | gzip > \ $BACKUP_DIR/mysql_${DB_NAME}_${DATE}.sql.gz
# PostgreSQL backuppg_dump -U $DB_USER -Fc $DB_NAME | gzip > \ $BACKUP_DIR/pg_${DB_NAME}_${DATE}.dump.gz
# Keep only 7 daysfind $BACKUP_DIR -name "*.sql.gz" -mtime +7 -deletefind $BACKUP_DIR -name "*.dump.gz" -mtime +7 -delete
# Logecho "Backup completed: $DATE" >> /var/log/backup.logCron Schedule
Section titled “Cron Schedule”# Daily at 2 AM0 2 * * * root /usr/local/bin/db-backup.sh
# Every 6 hours# 0 */6 * * * root /usr/local/bin/db-backup.sh74.4 Interview Questions
Section titled “74.4 Interview Questions”Basic Questions
Section titled “Basic Questions”-
What is mysqldump?
- MySQL logical backup tool
-
What’s the difference between pg_dump and pg_basebackup?
- pg_dump: logical (SQL), pg_basebackup: physical
-
What does —single-transaction do?
- Wraps dump in transaction for consistent backup
-
What is point-in-time recovery?
- Restoring to specific point using WAL
-
How do you automate backups?
- Cron jobs with backup scripts
Summary
Section titled “Summary” Quick Reference+------------------------------------------------------------------+| || MySQL: || +----------------------------------------------------------+ || | mysqldump -u user -p db > backup.sql | || | mysql -u user -p db < backup.sql | || | xtrabackup --backup --target-dir=/backup | || +----------------------------------------------------------+ || || PostgreSQL: || +----------------------------------------------------------+ || | pg_dump -U user db > backup.sql | || | psql -U user db < backup.sql | || | pg_basebackup -D /backup | || +----------------------------------------------------------+ || |+------------------------------------------------------------------+