How to backup

This page shows you how to backup an existing elabftw installation. It is important that you take the time to make sure that your backups are working properly.

_images/didyoubackup.jpg

There is basically three things to backup :

  • the MySQL database
  • your config.php file (unless you’re using Docker and you want the /etc/elabftw.yml file)
  • the uploaded files (in uploads/ folder)

Using elabctl

If you installed eLabFTW with elabctl, making a backup becomes very easy. Issue this command as root:

elabctl backup

Using a script

You’ll want to have a little script that do the backup automatically. Here is one way to do it. Adapt it to your needs: see script.

If you don’t remember your SQL user/password, look in the config.php file!

Make sure to synchronize your files to another computer. Because backuping to the same machine is only half useful.

Making it automatic using cron

A good backup is automatic.

If you’re under a GNU/Linux system, try:

export EDITOR=nano ; crontab -e

This will open a file:

_images/crontab.png

Add this line at the bottom:

00 04 * * * bash /path/to/backup.sh
or
00 04 * * * elabctl backup

This will run the script everyday at 4am.

How to backup a Docker installation

With elabctl

If you installed eLabFTW with elabctl, use:

elabctl backup

Without elabctl

  • Copy the /etc/elabftw.yml somewhere safe.
  • Copy the /var/elabftw folder somewhere safe (make a zip or tar archive).
  • Backup the MySQL database:
docker exec -it mysql bash -c 'mysqldump -u$MYSQL_USER -p$MYSQL_PASSWORD -r dump.sql $MYSQL_DATABASE'
docker cp mysql:dump.sql elabftw-$(date --iso-8601).sql
gzip --best elabftw-$(date --iso-8601).sql

How to restore a backup

You should have two files to start with. Those files are the ones generated by elabctl backup command:

  • mysql_dump-YYYY-MM-DD.sql.gz
  • uploaded_files-YYYY-MM-DD.zip

Let’s start by moving uploaded files and config file at the correct place:

unzip uploaded_files-YYYY-MM-DD.zip
mv var/elabftw/web/* /var/elabftw/web
mv etc/elabftw.yml /etc/elabftw.yml
# now fix the permissions
chown -R 100:101 /var/elabftw/web
chmod 600 /etc/elabftw.yml

Now we import the SQL database (the mysql container must be running):

gunzip mysql_dump-YYYY-MM-DD.sql.gz # uncompress the file
docker cp mysql_dump-YYYY-MM-DD.sql mysql:/ # copy it inside the mysql container
docker exec -it mysql bash # spawn a shell in the mysql container
mysql -uroot -p # login to mysql prompt
# here you type the password you put in MYSQL_ROOT_PASSWORD in the /etc/elabftw.yml file
Mysql> drop database elabftw; # delete the brand new database
Mysql> create database elabftw; # create a new one
Mysql> use elabftw; # select it
Mysql> source mysql_dump-YYYY-MM-DD.sql; # import the backup
Mysql> exit;

Now you should have your old install back :)

How to backup a NAS

If you’ve installed elabftw on a NAS, you don’t have elabctl nor a config file. So you’ll need to issue the commands manually (you can make a script after of course).

So the first thing is to create a folder where you’ll put the backups:

export BACKUP_FOLDER=/path/to/your/backup/folder
mkdir -p $BACKUP_FOLDER

Next we’ll save the database:

docker exec mysql bash -c 'mysqldump -u$MYSQL_USER -p$MYSQL_PASSWORD -r dump.sql $MYSQL_DATABASE'

The environment variables will be correctly replaced; convenient, isn’t it? So just copy paste this and it’ll work.

Next we copy the dump.sql file out of the container, and move it to our backup place:

export DUMP_FILE=${BACKUP_FOLDER}/$(date --iso-8601)-dump.sql
docker cp mysql:dump.sql $DUMP_FILE
# compress it
gzip -f --best $DUMP_FILE

Finally we make a zip of the uploaded files:

export ZIP_FILE=${BACKUP_FOLDER}/$(date --iso-8601)-uploaded_files.zip
zip -rq "$ZIP_FILE" /path/to/elabftw/files/web -x /path/to/elabftw/files/web/tmp\*

I’m not sure if you can export the config of the containers from the GUI menu. So maybe take a screenshot or save it manually. Worst case scenario if you lose completely this config it’s not a big deal.

Put all of the above in a script so you can run it easily next time (make a cronjob if possible). See the backup function of elabctl: https://github.com/elabftw/elabctl/blob/master/elabctl.sh#L40 for inspiration.