Search code examples
erlangbackupelixirmnesia

Backing up a Mnesia database with Elixir


I've got an Elixir app that uses a Mnesia database. It works fine and I can store and retrieve data no problem. I'm now looking at how to back up that database and, reading through the documentation, found that there is a :mnesia.backup function that should do what I want. However, when I call it, I get:

:mnesia.backup("myfile") {:error, {:EXIT, {:error, :function_clause}}}

And the logs show this:

[error] Mnesia(:nonode@nohost): ** ERROR ** Failed to abort backup. :mnesia_backup::abort_write["myfile"] -> {:badrecord, :backup}

That line in the log is confusing as I wasn't trying to abort the backup at all.

The :function_clause error suggests that an invalid argument is passed somewhere so I search online to see what I should pass to the backup function (the Erlang docs are particularly unclear about that) and found this question that suggests it should be the name of the backup file: what is the proper way to backup/restore a mnesia database?

I had a look at the mnesia code to see if I could find anything obvious but no joy there.

Can anybody tell me what I'm doing wrong please?

I am using Elixir 1.4.1 with Erlang/OTP 19 on Ubuntu 16.04 and I have a basic code example that demonstrates the problem if needed.


Solution

  • :mnesia.backup accepts a charlist as the file name, which are written in double quotes in Erlang syntax but single quotes in Elixir. The following should work:

    :mnesia.backup('myfile')
    

    I'd highly recommend going through this official Elixir crash course to quickly pick up Erlang syntax if you know Elixir syntax or vice versa.