This document explains how to create and restore data backups with Vitess. Vitess uses backups for two purposes:
- Provide a point-in-time backup of the data on a tablet.
- Bootstrap new tablets in an existing shard.
Vitess stores data backups on a Backup Storage service, which is a pluggable interface.
Currently, we have plugins for:
- A network-mounted path (e.g. NFS)
- Google Cloud Storage
- Amazon S3
Vitess also supports multiple ways to generate data backups. This is called a Backup engine, which is a pluggable interface
Currently, we have plugins for:
- Builtin: Copy all the database files into specified storage. This is the default.
- Percona Xtrabackup
Before you can back up or restore a tablet, you need to ensure that the tablet is aware of the Backup Storage system and Backup engine that you are using. To do so, use the following command-line flags when starting a vttablet that has access to the location where you are storing backups.
Note that for the Google Cloud Storage plugin, we currently only support Application Default Credentials. It means that access to Cloud Storage is automatically granted by virtue of the fact that you’re already running within Google Compute Engine or Container Engine.
For this to work, the GCE instances must have been created with the scope that grants read-write access to Cloud Storage. When using Container Engine, you can
do this for all the instances it creates by adding
--scopes storage-rw to the
gcloud container clusters create command.
Creating a backup
Run the following vtctl command to create a backup:
vtctl Backup <tablet-alias>
If the engine is
builtin, in response to this command, the designated tablet performs the following
sequence of actions:
Switches its type to
BACKUP. After this step, the tablet is no longer used by vtgate to serve any query.
Stops replication, get the current replication position (to be saved in the backup along with the data).
Shuts down its mysqld process.
Copies the necessary files to the Backup Storage implementation that was specified when the tablet was started. Note if this fails, we still keep going, so the tablet is not left in an unstable state because of a storage failure.
Restarts replication (with the right semi-sync flags corresponding to its original type, if applicable).
Switches its type back to its original type. After this, it will most likely be behind on replication, and not used by vtgate for serving until it catches up.
If the engine is
xtrabackup, we do not do any of the above. The tablet can
continue to serve traffic while the backup is running.
Restoring a backup
When a tablet starts, Vitess checks the value of the
-restore_from_backup command-line flag to determine whether
to restore a backup to that tablet.
- If the flag is present, Vitess tries to restore the most recent backup from the Backup Storage system when starting the tablet.
- If the flag is absent, Vitess does not try to restore a backup to the tablet. This is the equivalent of starting a new tablet in a new shard.
As noted in the Prerequisites section, the flag is generally enabled all of the time for all of the tablets in a shard. By default, if Vitess cannot find a backup in the Backup Storage system, the tablet will start up empty. This behavior allows you to bootstrap a new shard before any backups exist.
-wait_for_backup_interval flag is set to a value greater than zero,
the tablet will instead keep checking for a backup to appear at that interval.
This can be used to ensure tablets launched concurrently while an initial backup
is being seeded for the shard (e.g. uploaded from cold storage or created by
another tablet) will wait until the proper time and then pull the new backup
when it’s ready.
vttablet ... -backup_storage_implementation=file \ -file_backup_storage_root=/nfs/XXX \ -restore_from_backup
vtctl provides two commands for managing backups:
ListBackups displays the existing backups for a keyspace/shard in chronological order.
vtctl ListBackups <keyspace/shard>
RemoveBackup deletes a specified backup for a keyspace/shard.
RemoveBackup <keyspace/shard> <backup name>
Bootstrapping a new tablet
Bootstrapping a new tablet is almost identical to restoring an existing tablet. The only thing you need to be cautious about is that the tablet specifies its keyspace, shard and tablet type when it registers itself at the topology. Specifically, make sure that the following additional vttablet parameters are set:
-init_keyspace <keyspace> -init_shard <shard> -init_tablet_type replica|rdonly
The bootstrapped tablet will restore the data from the backup and then apply changes, which occurred after the backup, by restarting replication.
We recommend to take backups regularly e.g. you should set up a cron job for it.
To determine the proper frequency for creating backups, consider the amount of time that you keep replication logs and allow enough time to investigate and fix problems in the event that a backup operation fails.
For example, suppose you typically keep four days of replication logs and you create daily backups. In that case, even if a backup fails, you have at least a couple of days from the time of the failure to investigate and fix the problem.
The back-up and restore processes simultaneously copy and either compress or decompress multiple files to increase throughput. You can control the concurrency using command-line flags:
- The vtctl Backup command uses the
- vttablet uses the
If the network link is fast enough, the concurrency matches the CPU usage of the process during the backup or restore process.