Database

How To Backup and Restore RabbitMQ Data & Configurations

Pinterest LinkedIn Tumblr

This guide covers backup and restore procedures for various types of data a RabbitMQ node may contain.

RabbitMQ backups are a JSON representation of your broker’s metadata. This includes users, vhosts, queues, exchanges, and bindings. Backups are made against a running cluster using the export command provided by the RabbitMQ management plugin. Messages are not included in the backup.

Related Content

RabbitMQ Cluster Status

To get the cluster status

sudo rabbitmqctl cluster_status

Output on my server

$ sudo rabbitmqctl cluster_status
/usr/lib/erlang/erts-12.1.5/bin/beam.smp: /usr/lib/libncursesw.so.6: no version information available (required by /usr/lib/erlang/erts-12.1.5/bin/beam.smp)
Cluster status of node [email protected] ...
Basics

Cluster name: [email protected]

Disk Nodes

[email protected]

Running Nodes

[email protected]

Versions

[email protected]: RabbitMQ 3.8.22 on Erlang 24.1.7

Maintenance status

Node: [email protected], status: not under maintenance

Alarms

(none)

Network Partitions

(none)

Listeners

Node: [email protected], interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Node: [email protected], interface: 0.0.0.0, port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
Node: [email protected], interface: [::], port: 15672, protocol: http, purpose: HTTP API

Feature flags

Flag: drop_unroutable_metric, state: disabled
Flag: empty_basic_get_metric, state: disabled
Flag: implicit_default_bindings, state: enabled
Flag: maintenance_mode_status, state: enabled
Flag: quorum_queue, state: enabled
Flag: user_limits, state: enabled
Flag: virtual_host_metadata, state: enabled

Downloading rabbitmqadmin

The rabbitmqadmin command line tool will be used for backup and restore.

The management plugin ships with a command line tool rabbitmqadmin. You need to enable the management plugin:

sudo rabbitmq-plugins enable rabbitmq_management

My output

~> sudo rabbitmq-plugins enable rabbitmq_management
Enabling plugins on node [email protected]:
rabbitmq_management
The following plugins have been configured:
  rabbitmq_management
  rabbitmq_management_agent
  rabbitmq_web_dispatch
Applying plugin configuration to [email protected]
Plugin configuration unchanged.

This plugin is used to perform some of the same actions as the Web-based UI, and which may be more convenient for automation tasks.

Once you enable the management plugin, download rabbitmqadmin  Python command line tool that interacts with the HTTP API. It can be downloaded from any RabbitMQ node that has the management plugin enabled at

http://{node-hostname}:15672/cli/

Once downloaded, make the file executable and move it to /usr/local/bin directory:

chmod +x rabbitmqadmin
sudo mv rabbitmqadmin /usr/local/bin

How to Backup RabbitMQ Configurations

RabbitMQ backup doesn’t include Messages since they are stored in a separate message store. It will only backup RabbitMQ users, vhosts, queues, exchanges, and bindings. The backup file is a JSON representation of RabbitMQ metadata.

To backup RabbitMQ configurations, use the command:

rabbitmqadmin export <backup-file-name>

Example:

$ rabbitmqadmin export rabbitmq-backup-config.json
Exported definitions for localhost to "rabbitmq-backup-config.json"

The export will be written to filerabbitmq-backup-config.json.

How to Restore RabbitMQ Configurations backup

To restore your RabbitMQ configurations from a backup, use the command:

rabbitmqadmin import <JSON backup file >

Example

$ rabbitmqadmin import rabbitmq-backup-config.json
Imported definitions for localhost from "rabbitmq-backup.json"

How to Backup RabbitMQ Data

RabbitMQ Definitions and Messages are stored in an internal database located in the node’s data directory. To get the directory path, run the following command against a running RabbitMQ node:

sudo rabbitmqctl eval 'rabbit_mnesia:dir().'

Sample output:

$ sudo rabbitmqctl eval 'rabbit_mnesia:dir().'
"/var/lib/rabbitmq/mnesia/[email protected]"

This directory contains many files:

# ls /var/lib/rabbitmq/mnesia/[email protected]
cluster_nodes.config  msg_stores		 rabbit_durable_exchange.DCD  rabbit_durable_queue.DCL	     rabbit_runtime_parameters.DCL  rabbit_user.DCD		rabbit_vhost.DCD
DECISION_TAB.LOG      nodes_running_at_shutdown  rabbit_durable_exchange.DCL  rabbit_durable_route.DCD	     rabbit_serial		    rabbit_user_permission.DCD	schema.DAT
LATEST.LOG	      quorum			 rabbit_durable_queue.DCD     rabbit_runtime_parameters.DCD  rabbit_topic_permission.DCD    rabbit_user_permission.DCL	schema_version

In RabbitMQ versions starting with 3.7.0 all messages data is combined in the msg_stores/vhosts directory and stored in a subdirectory per vhost. Each vhost directory is named with a hash and contains a .vhost file with the vhost name, so a specific vhost’s message set can be backed up separately.

To do RabbitMQ definitions and messages data backup, copy or archive this directory and its contents. But first, you need to stop RabbitMQ service

sudo systemctl stop rabbitmq-server

The example below will create an archive:

tar cvf rabbitmq-backup.tgz /var/lib/rabbitmq/mnesia/[email protected]

How to Restore RabbitMQ Data

To restore from Backup, extract the files from backup to the data directory.

Internal node database stores node’s name in certain records. Should node name change, the database must first be updated to reflect the change using the following rabbitmqctl command:

rabbitmqctl rename_cluster_node <oldnode> <newnode>

When a new node starts with a backed up directory and a matching node name, it should perform the upgrade steps as needed and proceed to boot.

Conclusion

In this guide we explored how to backup and restore rabbitmq data.

I am a Devops Engineer, but I would describe myself as a Tech Enthusiast who is a fan of Open Source, Linux, Automations, Cloud and Virtualization. I love learning and exploring new things so I blog in my free time about Devops related stuff, Linux, Automations and Open Source software. I can also code in Python and Golang.

Write A Comment