How to backup and restore data
While running, Memgraph generates various files in its data
directory, including the durability
files, that is, snapshots and WALs that contain Memgraph's data in a
recoverable format and are located in the wal
and snapshots
folders in the
data directory. On startup, Memgraph searches for previously saved durability
files and uses them to recreate the most recent database state.
When talking about the data directory in the context of backup and restore, we
are actually talking about two directories, snapshots
and wal
, which are
usually located in the /var/lib/memgraph
directory.
Snapshots are created periodically based on the value defined with the
--storage-snapshot-interval-sec
configuration flag, as well as upon exit based
on the configuration flag --storage-snapshot-on-exit
, defined by the
configuration file.
You can configure the exact snapshot creation behavior by defining the relevant. If you need help adjusting the configuration, check out the how-to guide on changing the configuration.
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 thewal
directory) to a backup location.If you've just created a snapshot file there is no need to backup WAL files.
To help copying the files from the Docker container, check out the Working with docker guide.
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
To restore data from a backup
- Docker 🐳
- Linux
Empty the
wal
directoryIf you want to restore data only from the snapshot file, ensure that the
wal
directory is empty:Find the container ID using a
docker ps
command, then enter the container using:docker exec -it CONTAINER_ID bash
Position yourself in the
/var/lib/memgraph/wal
directory andrm *
Stop the instance using
docker stop CONTAINER_ID
Start the instance by adding a
-v ~/snapshots:/var/lib/memgraph/snapshots
flag to thedocker 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 thedocker 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
- Before running an instance, copy the backed up snapshot into the
snapshots
directory, and optionally, copy the backed-up WAL files into thewal
directory. - If you are restoring data only from the snapshot file, ensure that the file
you want to use to restore the data is the only snapshot file in the
snapshots
directory and that thewal
directory is empty. If you are restoring data from both the snapshot and WAL files, ensure they are the only files in thesnapshot
andwal
directories. - Start the database.