You can easily backup and restore the Matomo data using the following commands.
Export your MySQL/MariaDB database in a file
You can use tools such as phpMyAdmin or mysqldump
to do this. The simplest way to make a full copy of the MySQL database is to run the mysqldump command with this set of parameters:
$ time mysqldump --extended-insert --no-autocommit --quick --single-transaction MATOMO_DATABASE_NAME -uMATOMO_DATABASE_USERNAME -p > matomo_backup_database.sql
We can then compress this backup before sending it to the backup server:
$ tar zcf matomo-mysql-database-$(date +%Y-%m-%d-%H.%M.%S).sql.tar.gz matomo_backup_database.sql
Restore the backed up database dump
you can use phpmyadmin to import the file that you previously created, or restore the DB backup by running the following command:
$ time mysql MATOMO_DATABASE_NAME -uMATOMO_DATABASE_USERNAME -p < matomo_backup_database.sql
This should create more than a dozen Matomo tables in your database and restore all your data.
Note: if you restore the backup in a “test” instance, you may be interested in disabling all email notifications.