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
mgconsoleor Memgraph Lab:CREATE SNAPSHOT;The snapshot is saved in the
snapshotsdirectory 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
mgconsoleor Memgraph Lab:LOCK DATA DIRECTORY;Copy files
Copy snapshot files (from the
snapshotsdirectory) and any additional WAL files (from thewaldirectory) 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
mgconsoleor 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
waldirectoryIf you want to restore data only from the snapshot file, ensure that the
waldirectory is empty:Find the container ID using a
docker pscommand, then enter the container using:docker exec -it CONTAINER_ID bashPosition yourself in the
/var/lib/memgraph/waldirectory andrm *
Stop the instance using
docker stop CONTAINER_IDStart the instance by adding a
-v ~/snapshots:/var/lib/memgraph/snapshotsflag to thedocker runcommand, where the~/snapshotsrepresents 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/memgraphIf 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/walflags to thedocker runcommand, where the~/snapshotsrepresents a path to the location of the backed-up snapshot directory, and~/walrepresents 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
snapshotsdirectory, and optionally, copy the backed-up WAL files into thewaldirectory. - 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
snapshotsdirectory and that thewaldirectory is empty. If you are restoring data from both the snapshot and WAL files, ensure they are the only files in thesnapshotandwaldirectories. - Start the database.