0% found this document useful (0 votes)
19 views7 pages

Backups Chap 7

This document discusses different methods for backing up MySQL databases, including logical backups using mysqldump and physical backups using XtraBackup. It provides steps for installing and using XtraBackup on CentOS and Ubuntu systems to back up databases while they are online. It also describes how to lock database instances for backup consistency in MySQL 8 and take backups of binary logs for point-in-time recovery.

Uploaded by

shawnmysql
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views7 pages

Backups Chap 7

This document discusses different methods for backing up MySQL databases, including logical backups using mysqldump and physical backups using XtraBackup. It provides steps for installing and using XtraBackup on CentOS and Ubuntu systems to back up databases while they are online. It also describes how to lock database instances for backup consistency in MySQL 8 and take backups of binary logs for point-in-time recovery.

Uploaded by

shawnmysql
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

BackUps

Chapter 7
Table of Contents
Introduction....................................................................................................................................3
Mysqldump......................................................................................................................................3
XtraBackup.....................................................................................................................................5
How to do it….......................................................................................................................................................6
CentOS:....................................................................................................................................................6
Ubuntu:.....................................................................................................................................................6
Locking Instances for Backup.......................................................................................................7
How to do it...........................................................................................................................................................7
Binary Log Backup.........................................................................................................................7
How to do it...........................................................................................................................................................7
Introduction
2 types of backups:
- Logical Backup (Mysqldump)
o Exports all the databases the databases, table structures, data, and stored routines
into a set of SQL statements that can be executed again to recreate the state of the
database
- Physical Backup (XtraBackup)
o Contains all the files on the system that the databases used to store all the
database entities.

Mysqldump
shell> mysqldump [options]

Full Backup of All Databases:

shell> mysqldump --all-databases > dump.sql

To Include The Routines and Events:

shell> mysqldump --all-databases --routines --events > dump.sql

Point in time recovery:

shell> mysqldump --all-databases --routines --events --single-transaction -


-master-data > dump.sql

Dumping Master Binary Coordinates:

shell> mysqldump --all-databases --routines --events --single-transaction -


-dump-slave > dump.sql

Specific Databases and Tables:

To back up only a specific database, execute this:


shell> mysqldump --databases employees > employees_backup.sql

To back up only a specific table, execute this:

shell> mysqldump --databases employees --tables employees >


employees_backup.sql

Ignore Tables:

shell> mysqldump --databases employees --ignore-table=employees.salary >


employees_backup.sql

Specific Rows:

shell> mysqldump --databases employees --tables employees --databases


employees --tables employees --where="hire_date>'2000-01-01'" >
employees_after_2000.sql

You can use the LIMIT clause to limit the results:

shell> mysqldump --databases employees --tables employees --databases


employees --tables employees --where="hire_date >= '2000-01-01' LIMIT 10"
> employees_after_2000_limit_10.sql

Backup From A Remote Server:

shell> mysqldump --all-databases --routines --events --triggers --hostname


<remote_hostname> > dump.sql

Only Schema and No Data:

shell> mysqldump --all-databases --routines --events --triggers --hostname


<remote_hostname> > dump.sql

Only Data and NO Schema:

shell> mysqldump --all-databases --no-create-db --no-create-info --


complete-insert > data.sql

Backup For Merging Data With Other Server:

Replace with new data:

shell> mysqldump --databases employees --skip-add-drop-table --no-createinfo


--replace > to_development.sql
Ignore Data:
o Instead of REPLACE, you can use the INSERT IGNORE statement when writing
to the dump file. This will keep the existing data on the server and insert new
data.

XtraBackup

XtraBackup is an open source backup software provided by Percona. It copies flat files
without shutting down the server, but to avoid inconsistencies, it uses a redo log file. It is
widely used by many companies as a standard backup tool. The advantages are that it is
very fast compared to logical backup tools and recovery is also very fast.
This is how Percona XtraBackup works (taken from the Percona XtraBackup
documentation):

1. It copies your InnoDB data files, which results in data that is internally
inconsistent; but then it performs crash recovery on the files to make them a
consistent, usable database again.

2. This works because InnoDB maintains a redo log, also called the transaction log.
This contains a record of every change to the InnoDB data. When InnoDB starts, it
inspects the data files and the transaction log, and performs two steps. It applies
committed transaction log entries to the data files, and it performs an undo
operation on any transactions that modified data but did not commit.

3. Percona XtraBackup works by remembering the log sequence number (LSN)


when it starts, and then copying away the data files. It takes some time to do this,
so if the files are changing, then they reflect the state of the database at different
points in time. At the same time, Percona XtraBackup runs a background process
that watches the transaction log files, and copies changes from it. Percona
XtraBackup needs to do this continually because the transaction logs are written
in a round-robin fashion, and can be reused after a while. Percona XtraBackup
needs the transaction log records for every change to the data files since it began
execution.

How to do it…

CentOS:

1. Install mysql-community-libs-compat:

shell> sudo yum install -y mysql-community-libs-compat

2. Install the Percona repository:


shell> sudo yum install
http://www.percona.com/downloads/percona-release/redhat/0.1-4/perco
na-release-0.1-4.noarch.rpm

3. Test the repository:

shell> yum list | grep xtrabackup

4. Install XtraBackup:

shell> sudo yum install percona-xtrabackup-24

Ubuntu:

1. Fetch the repository packages from Percona:

shell> wget https://repo.percona.com/apt/perconarelease_


0.1-4.$(lsb_release -sc)_all.deb

2. Install the downloaded package with dpkg. To do that, run the following
commands as root or with sudo:

shell> sudo dpkg -i percona-release_0.1-4.$(lsb_release -


sc)_all.deb

3. Remember to update the local cache:

shell> sudo apt-get update

4. After that, you can install the package:

shell> sudo apt-get install percona-xtrabackup-24

Locking Instances for Backup

As of MySQL 8, you can lock the instance for backup, which will allow the DML during
online backup and block all the operations that could result in an inconsistent snapshot.

How to do it...

Before you begin your backup, lock the instance for backup:
mysql> LOCK INSTANCE FOR BACKUP;

Perform the backup, and after completion, unlock the instance:

mysql> UNLOCK INSTANCE;

Binary Log Backup

You know that binary logs are needed for point-in-time recovery. In this section, you will
understand how to take a backup of binary logs. The process streams the binary logs from
the database server to a remote backup server. You can take the binary log backup from
either the slave or the master. If you are taking the binary log backup from the master and
the actual backup from the slave, you should use --dump-slave to get the corresponding
master log position. If you are using mydumper or XtraBackup, it gives both the master and
slave binary log positions.

How to do it...

1. Create a replication user on the server. Create a strong password:

mysql> GRANT REPLICATION SLAVE ON *.* TO 'binlog_user'@'%'


IDENTIFIED BY 'binlog_pass';

2. Check the binary logs on the server:

mysql> SHOW BINARY LOGS;

3. Log in to the backup server and execute the following command. This will copy
the binary logs from the MySQL server to the backup server. You can start using
nohup or disown:

shell> mysqlbinlog -u <user> -p<pass> -h <server> --read-fromremote-


server --stop-never
--to-last-log --raw server1.000008 &
shell> disown -a

4. Verify that the binary logs are being backed up:


shell> ls -lhtr server1.0000*

You might also like