Database managementBackup and restore

Backup and restore

Memgraph uses snapshots and WAL to ensure the durability of the stored data. Learn how to safely backup and restore your data.

Create backup

Follow these steps to create database backup:

Create a snapshot

If necessary, create a snapshot of the current database state by running the following query in mgconsole or Memgraph Lab:

CREATE SNAPSHOT;

The snapshot is saved in the snapshots directory of the data directory (/var/lib/memgraph).

Lock the data directory

Durability files are deleted when an event is triggered, for example, exceeding the maximum number of snapshots.

To disable this behavior, run the following query in mgconsole or Memgraph Lab:

LOCK DATA DIRECTORY;

Copy files

Copy snapshot files (from the snapshots directory) and any additional WAL files (from the wal directory) to a backup location.

If you’ve just created a snapshot file there is no need to backup WAL files.

To copy the snapshot files from the Docker container first check the container ID by running docker ps then run the following command:

docker cp CONTAINER_ID:/var/lib/memgraph/snapshots/SNAPSHOT_FILE SNAPSHOT_FILE

CONTAINER_ID is the ID from docker ps, and SNAPSHOT_FILE is the snapshot’s file name.

Unlock the data directory

Run the following query in mgconsole or Memgraph Lab to unlock the directory:

UNLOCK DATA DIRECTORY;

Memgraph will delete the files which should have been deleted before locking and allow any future deletion of the durability files.

Restore data

Restore a snapshot to a running Memgraph instance using the RECOVER SNAPSHOT command:

RECOVER SNAPSHOT path=literal ( WITH CONFIG configsMap=configMap ) ? ( FORCE )? ;

Before modifying the local data directory, Memgraph will move all existing WALs and snapshots to a hidden .old directory. This directory is reused for subsequent recovery operations, meaning only a single backup is maintained at any time. Files already present in the .old directory will be deleted before moving the current files, ensuring only the most recent backup is preserved. By default, snapshots are stored in the local directory /var/lib/memgraph/snapshots/.

Snapshots can be recovered from:

  • Local filesystem - absolute or relative paths
  • S3-compatible storage - s3://bucket/path/to/snapshot
  • Remote servers - http://, https://, or ftp:// URLs

Local filesystem

RECOVER SNAPSHOT "/path/to/snapshot";

Use absolute paths when possible. Relative paths are resolved from the Memgraph execution directory. Memgraph copies the snapshot to its local snapshot directory, so ensure the file has appropriate read permissions.

If not, you might encounter the following error:

Failed to copy snapshot over to local snapshots directory.

S3-compatible storage

Provide AWS credentials using one of these methods (in order of precedence):

MethodExample
Query configWITH CONFIG {aws_region: ..., aws_access_key: ..., aws_secret_key: ...}
Environment variablesAWS_REGION, AWS_ACCESS_KEY, AWS_SECRET_KEY, AWS_ENDPOINT_URL
Runtime settingsaws.region, aws.access_key, aws.secret_key, aws.endpoint_url

Example:

RECOVER SNAPSHOT "s3://my-bucket/snapshots/backup.snapshot" WITH CONFIG {'aws_region': 'eu-west-1', 'aws_access_key': '...', 'aws_secret_key': 'secret'};

The FORCE flag

If the instance is not freshly started, add the FORCE flag to your command:

RECOVER SNAPSHOT "/path/to/snapshot" FORCE;

This will clear all existing data before applying the snapshot.

RECOVER SNAPSHOT is also the primary cure for a broken database — one that failed durability recovery on startup and came up empty because --storage-allow-recovery-failure was enabled. Running it against the broken database loads a known-good snapshot, clears the broken state, and leaves the durability directory in a clean state that recovers normally on the next restart. See recovery failure handling for the full set of restoration paths.

In order to query the snapshots currently present in the local data directory, execute the query:

SHOW SNAPSHOTS;

Its results contain the path to the file, the logical timestamp, the physical timestamp and the file size.

As of Memgraph v3.5, the SHOW SNAPSHOTS query does not return information regarding the next scheduled snapshot. A special query has been added:

SHOW NEXT SNAPSHOT;

If the periodic snapshot background job is active, the result will return the path and the time at which the snapshots will be created.

If you are using Memgraph pre v2.22, follow these steps to restore data from a backup:

Empty the `wal` directory

If you want to restore data only from the snapshot file, ensure that the wal directory is empty:

  1. Find the container ID using a docker ps command, then enter the container using:
docker exec -it CONTAINER_ID bash
  1. Position yourself in the /var/lib/memgraph/wal directory and rm *

Stop the instance

Run the following command

docker stop CONTAINER_ID

Start the instance

You can start the instance with the backed up files in two ways.

Option 1

You can start the instance by adding a -v ~/snapshots:/var/lib/memgraph/snapshots flag to the docker run command, where the ~/snapshots represents a path to the location of the directory with the back-up snapshot, for example:

docker run -p 7687:7687 -p 7444:7444 -v ~/snapshots:/var/lib/memgraph/snapshots memgraph/memgraph

If you want to copy both WAL and snapshot files start the instance by adding a -v ~/snapshots:/var/lib/memgraph/snapshots -v ~/wal:/var/lib/memgraph/wal flags to the docker run command, where the ~/snapshots represents a path to the location of the backed-up snapshot directory, and ~/wal represents a path to the location of the backed-up wal directory for example:

docker run -p 7687:7687 -p 7444:7444 -v ~/snapshots:/var/lib/memgraph/snapshots -v ~/wal:/var/lib/memgraph/wal memgraph/memgraph

Option 2

The other option is to copy the backed-up snapshot file into the snapshots directory after creating the container and start the database. So the commands should look like this:

docker create -p 7687:7687 -p 7444:7444 -v snapshots:/var/lib/memgraph/snapshots --name memgraphDB memgraph/memgraph
tar -cf - sample_snapshot_file | docker cp -a - memgraphDB:/var/lib/memgraph/snapshots

The sample_snapshot_file is the snapshot file you want to use to restore the data. Due to the nature of Docker file ownership, you need to use tar to copy the file as STDIN into the non-running container. It will allow you to change the ownership of the file to the memgraph user inside the container.

After that, start the database with:

docker start -a memgraphDB

The -a flag is used to attach to the container’s output so you can see the logs.

Once memgraph is started, change the snapshot directory ownership to the memgraph user by running the following command:

docker exec -it -u 0 memgraphDB bash -c "chown memgraph:memgraph /var/lib/memgraph/snapshots"

Otherwise, Memgraph will not be able to write the future snapshot files and will fail.

Database dump

The database dump contains a record of the database state in the form of Cypher queries. It’s equivalent to the SQL dump in relational DBs. Database dump preserves nodes, relationships, indexes, constraints and triggers.

You can run the queries constituting the dump to recreate the state of the DB as it was at the time of the dump.

To dump the Memgraph DB, run the following query:

DUMP DATABASE;

If you are using Memgraph Lab, you can dump the database, that is, the queries to recreate it, to a CYPHERL file in the Import & Export section of the Lab.

Storage modes

Memgraph has the option to work in IN_MEMORY_ANALYTICAL, IN_MEMORY_TRANSACTIONAL or ON_DISK_TRANSACTIONAL storage modes.

Memgraph always starts in the IN_MEMORY_TRANSACTIONAL mode in which it creates periodic snapshots and write-ahead logging as durability mechanisms, and also enables creating manual snapshots.

In the IN_MEMORY_ANALYTICAL mode, Memgraph offers no periodic snapshots and write-ahead logging. Users can create a snapshot with the CREATE SNAPSHOT; Cypher query. During the process of snapshot creation, other transactions will be prevented from starting until the snapshot creation is completed.

In the ON_DISK_TRANSACTIONAL mode, durability is supported by RocksDB since it keeps its own WAL files. Memgraph persists the metadata used in the implementation of the on-disk storage.

Backup in multi-tenancy Enterprise

With multi-tenancy, each database keeps its own snapshots and WAL files, so you back up and restore each database separately. This section covers the data directory layout, how to back up a database and how long snapshots are kept. To restore one database, see Restore a single database.

The examples use a database named customers:

CREATE DATABASE customers;

Data directory layout

The default memgraph database keeps its files directly in the data directory. Every other database has a directory under databases/, named after the database’s UUID, not its name:

DATA_DIRECTORY/
├── snapshots/            # default memgraph database
├── wal/
└── databases/
    ├── .durability/      # maps database names to UUIDs
    ├── memgraph/         # links back to DATA_DIRECTORY
    └── DATABASE_UUID/
        ├── snapshots/
        └── wal/

DATA_DIRECTORY is the value of the --data-directory flag: /var/lib/memgraph by default, and /var/lib/memgraph/mg_data in the Memgraph Helm charts.

To find the directory of a database, read the database_uuid field of SHOW STORAGE INFO ON DATABASE:

SHOW STORAGE INFO ON DATABASE customers;

The query also works on a suspended database. The name-to-UUID mapping is kept only in databases/.durability, which Memgraph reads at startup, so look up the UUID while the instance is running.

Back up a database

Snapshots are the files to back up: each one holds a full copy of one database. You don’t need to copy the whole data directory or the WAL files. To back up a database:

  1. Switch to the database and lock its data directory, so Memgraph doesn’t delete snapshots while you copy them. LOCK DATA DIRECTORY locks only the current database.

    USE DATABASE customers;
    LOCK DATA DIRECTORY;
  2. Copy the files from DATA_DIRECTORY/databases/DATABASE_UUID/snapshots/ to storage outside the instance, for example with rclone. Store them under the database name, because the UUID alone doesn’t tell you later which database a snapshot belongs to.

  3. Unlock the data directory:

    UNLOCK DATA DIRECTORY;

To include changes made since the last periodic snapshot, run CREATE SNAPSHOT; in the database before you lock it.

Keep snapshots long enough to restore

Memgraph keeps only the newest snapshots of each database: 3 by default, set with --storage-snapshot-retention-count. The count applies to every database separately. Older snapshots are deleted, with the WAL files they cover.

The local restore window is roughly the retention count times the snapshot interval (--storage-snapshot-interval). With a snapshot every 5 minutes and a retention count of 3, the oldest local snapshot is about 15 minutes old.

Snapshots on the instance’s own volume are not a backup: they’re lost with the volume, and they age out quickly. Copy them off the instance on a schedule that covers the point in time you might need to restore to.

Restore a single database Enterprise

You can replace the data of one database with a snapshot while the other databases on the instance keep serving queries. The steps are the same in Docker, on Linux and on Kubernetes; only copying a snapshot file onto the instance differs. On Kubernetes, also see Restore a single database on Kubernetes.

Before you start, you need:

  • A Memgraph user with access to the database you restore and these privileges: DURABILITY for RECOVER SNAPSHOT and SHOW SNAPSHOTS, MULTI_DATABASE_USE for USE DATABASE, and STATS for SHOW STORAGE INFO. Resuming a suspended database or restoring into a new database also needs MULTI_DATABASE_EDIT, and a new database needs access to the memgraph database.
  • In a high availability cluster, a connection to the MAIN instance. Run SHOW INSTANCES; on a coordinator to find it.

Resume the database if it's suspended

USE DATABASE on a suspended database fails, so resume it first:

RESUME DATABASE customers;

Choose the snapshot

Where the snapshot is decides the path you pass to RECOVER SNAPSHOT in the next step:

  • Among the database’s own snapshots: switch to the database and list its snapshots. Use the path column as is.

    USE DATABASE customers;
    SHOW SNAPSHOTS;
  • In S3-compatible storage: use the s3:// URL. See S3-compatible storage for credentials.

  • On a server reachable over HTTP or FTP: use the http://, https:// or ftp:// URL.

  • In a file, for example a backup you copied off the instance: copy it onto the instance, into a directory the memgraph user can read, and use that path. The commands below use /var/lib/memgraph/restore/. SNAPSHOT_FILE is the snapshot’s file name.

    In Docker, copy the file into the container and make the memgraph user its owner. docker cp keeps the file’s owner from your machine, and Memgraph can’t read it until you change it. CONTAINER is the container’s name or ID:

    docker exec CONTAINER mkdir -p /var/lib/memgraph/restore
    docker cp ./SNAPSHOT_FILE CONTAINER:/var/lib/memgraph/restore/SNAPSHOT_FILE
    docker exec -u root CONTAINER chown memgraph:memgraph /var/lib/memgraph/restore/SNAPSHOT_FILE

    On Linux, copy the file with the memgraph user as its owner:

    sudo install -D -o memgraph -g memgraph -m 640 ./SNAPSHOT_FILE /var/lib/memgraph/restore/SNAPSHOT_FILE

    On Kubernetes, see Copy a snapshot onto the pod.

    If Memgraph can’t read the file, RECOVER SNAPSHOT fails with:

    Failed to copy snapshot over to local snapshots directory.

Restore the snapshot

You can restore over the existing database, or into a new database and switch to it once you’ve checked the data. If you haven’t restored from the snapshot before, restore it into a new database first: if the snapshot turns out to be damaged, the original database stays as it was.

In the examples, SNAPSHOT_PATH is the path or URL from the previous step.

Either way, Memgraph copies the snapshot into the database’s snapshots/ directory and moves the files that were there to .old directories (see Usage of .old directory). With --storage-backup-dir-enabled=false, it deletes them instead. Other databases on the instance aren’t affected.

In a high availability cluster, run RECOVER SNAPSHOT on MAIN. MAIN then sends the restored database to every REPLICA. On a REPLICA, the query fails with:

Failed to recover a snapshot. Replica instances are not allowed to create them.

Restore in place

Switch to the database and recover the snapshot with FORCE, which deletes the database’s current data first:

USE DATABASE customers;
RECOVER SNAPSHOT "SNAPSHOT_PATH" FORCE;
⚠️

Memgraph keeps the database’s previous snapshots and WAL files in .old directories only until the next RECOVER SNAPSHOT on that database, which deletes them. Copy them elsewhere first if you might need them.

⚠️

If the snapshot can’t be loaded, for example because the file is damaged, RECOVER SNAPSHOT fails after FORCE has already deleted the data. The database stays empty but still accepts queries, including writes, and it fails recovery the next time Memgraph starts. Its previous snapshots are still in its snapshots/ directory. Run RECOVER SNAPSHOT ... FORCE again with a good snapshot.

Restore into a new database

A new database is empty, so it doesn’t need FORCE, and the original stays untouched:

CREATE DATABASE customers_restored;
USE DATABASE customers_restored;
RECOVER SNAPSHOT "SNAPSHOT_PATH";

When the data is right, point your applications at customers_restored and grant users and roles access to it. Its files are in its own databases/DATABASE_UUID/ directory, so point your backup job at it too. Drop the original database once you no longer need it.

Check the result

Switch to the restored database, customers or customers_restored, and check its contents, for example the node and relationship counts in vertex_count and edge_count:

USE DATABASE DATABASE_NAME;
SHOW STORAGE INFO ON CURRENT DATABASE;

If you copied a snapshot file onto the instance, delete the copy once you no longer need it. In Docker:

docker exec CONTAINER rm /var/lib/memgraph/restore/SNAPSHOT_FILE

On Linux:

sudo rm /var/lib/memgraph/restore/SNAPSHOT_FILE

Restore when Memgraph doesn’t start

If a database’s files are damaged and Memgraph can’t recover it from any of its snapshots, Memgraph stops at startup. Start it with --storage-allow-recovery-failure=true, available from Memgraph 3.12: the damaged database comes up empty in a broken state, the others start normally, and you can restore it with the steps above. See recovery failure handling. On Kubernetes, see When the pod doesn’t start.

On a broken database, USE DATABASE, SHOW STORAGE INFO and RECOVER SNAPSHOT work, but SHOW SNAPSHOTS and other queries fail with:

Database is in the broken state because the recovery process failed.

Restore from a backup you copied off the instance. The database’s own snapshots are likely the damaged files: Memgraph already tried them at startup. A broken database doesn’t need FORCE.

In a high availability cluster, a broken database on a REPLICA recovers from MAIN on its own. Restore only a broken database on MAIN; see databases broken by recovery failure.

Once the database is restored, remove the flag and restart Memgraph, so a damaged database stops Memgraph at startup again instead of coming up empty.

Restore a single database on Kubernetes Enterprise

On Kubernetes, you restore a database with the same steps as in Restore a single database. This section covers what’s specific to the standalone and the high availability Memgraph Helm charts: finding the pod, copying a snapshot onto it, and starting a pod that restarts in a loop. You need kubectl access to the namespace Memgraph runs in.

Pods and data directory

The charts name the pods and containers as follows, and both mount the data volume at /var/lib/memgraph:

Helm chartPodContainerData directory
Standalone (memgraph)RELEASE_NAME-memgraph-0memgraph/var/lib/memgraph/mg_data
High availability (memgraph-high-availability)memgraph-data-ID-0memgraph-data/var/lib/memgraph/mg_data

RELEASE_NAME is the Helm release name; if it already contains memgraph, the chart doesn’t append -memgraph, and the pod is RELEASE_NAME-0. ID is the data instance’s id from the chart values. Check the exact names with kubectl get pods.

Copy a snapshot onto the pod

To restore from a snapshot file, copy it onto the pod’s data volume:

kubectl exec -n NAMESPACE POD -c CONTAINER -- mkdir -p /var/lib/memgraph/restore
kubectl cp -n NAMESPACE -c CONTAINER ./SNAPSHOT_FILE POD:/var/lib/memgraph/restore/SNAPSHOT_FILE

NAMESPACE is the Kubernetes namespace, POD and CONTAINER are the names from the table above, and SNAPSHOT_FILE is the snapshot’s file name. kubectl cp writes the file as the container’s user, so Memgraph can read it. Use /var/lib/memgraph/restore/SNAPSHOT_FILE as the path in RECOVER SNAPSHOT.

Once you no longer need the copy, delete it:

kubectl exec -n NAMESPACE POD -c CONTAINER -- rm /var/lib/memgraph/restore/SNAPSHOT_FILE

When the pod doesn’t start

If a database’s files are damaged, Memgraph stops at startup and the pod restarts in a loop. Add --storage-allow-recovery-failure=true to the instance’s flags (memgraphConfig in the standalone chart, the data instance’s args in the high availability chart) and upgrade the release. Helm replaces a list instead of merging it:

  • Standalone chart: keep the existing memgraphConfig entries and add the flag to them. The defaults set --data-directory=/var/lib/memgraph/mg_data; without it, Memgraph starts from an empty data directory.
  • High availability chart: args is a field of each item in the data list, so your values replace the whole data list. Copy the full list, with every data instance and all of its fields, and add the flag only to the failing instance’s args. Helm deletes the StatefulSet of any data instance missing from the list.

The upgrade doesn’t replace a pod that isn’t ready, so delete the pod; the StatefulSet recreates it with the flag:

kubectl delete pod -n NAMESPACE POD

Once the database is restored, remove the flag and upgrade the release again.

Usage of .old directory

By default, Memgraph backs up durability files to snapshots/.old and wal/.old directories before operations that could risk data loss.

When backup occurs:

  1. RECOVER SNAPSHOT query - Before loading an external snapshot, Memgraph moves existing files from snapshots/ to snapshots/.old and from wal/ to wal/.old. This protects your data if the newly loaded snapshot turns out to be corrupted.
  2. High Availability force sync - During replica’s force syncing. See this page for details.

Configuration:

Use the --storage-backup-dir-enabled flag to control this behavior:

  • true (default) - Old durability files are moved to .old directories
  • false - Old durability files are deleted immediately

Best practices

Memgraph can optimize restoring of snapshots in a multi-threaded manner. To enable multi-threaded restoration of a snapshot, you need to ensure the following flags are present:

  • --storage-parallel-schema-recovery=true
  • --storage-recovery-thread-count=<number_of_cores> where number_of_cores is the amount of CPU cores to parallelize the restoration process