Hyper Open Edge Cloud

How To Add a Matomo Backup Function

How To Add a Matomo Backup Function
  • Last Update:2022-07-15
  • Version:001
  • Language:en

Agenda

  1. Create a new bash script file for backup
  2. Edit file "software.cfg"
  3. Edit file "matomo-instance.cfg.in"
  4. Edit file "buildout.hash.cfg"
  5. The content of file "matomo-backup.sh.in"
  6. Verification

In the process of using matomo, we need to regularly save some configuration and plugin information to facilitate recovery in case of problems.

This tutorial will teach you how to add a matomo backup function with a bash template file.

Prerequisites

Create a new bash script file for backup

Create a new file named "matomo-backup.sh.in" in path "~/srv/projet/slapos/software/matomo-tutorial"
For backup some important files,we need a bash script, to generate it in instance, we need to provide a template script file.

Edit file "software.cfg"

  • Add a new line ../../component/diffutils/buildout.cfg in variable extends of section buildout, it will download the component diff, which will be used in the bash script to check if the contents of the two files are the same.
  • In the keyword context of section template-matomo-instance, add some keywords that will be passed to the script by the instance configuration file we added before ( that means those keywords will be passed to file matomo-instance.cfg first, and then in matomo-instance.cfg they will be repassed to the script we added, and finally be used as a variable in script ), like the location of Php bin file, like the directory of some components who will be used.
  • Create a new section named template-matomo-backup.sh , it will extend the download base section we added before and read the corresponding template file name in buildout.hash.cfg, then help us download the template script file we added.
[buildout]
extends=
....
      ../../component/diffutils/buildout.cfg
[template-matomo-instance]
....
context = ....
      key php_location apache-php:location
      key matomo_backup_sh template-matomo-backup.sh:target
      key findutils_location findutils:location
      key diffutils_location diffutils:location

# download matomo-backup.sh.in
[template-matomo-backup.sh] 
<= matomo-download

Edit file "matomo-instance.cfg.in"

Add the following sections:
  • Add a new section instance-parameter, where we accept parameters passed by software.cfg, and also who will be used by the script.
  • In order to generate the script file in the instance, we need a section matomo-backup.sh, in variable rendered, use ${directory:scripts} as the location of script (this varibale is defined in LAMP), and in keyword context, we pass the parameters in section instance-parameter and the section  php-bin to it cause we need them in script.
# parameters required by the configuration instance
[instance-parameter]
matomo = ${:document-root}
dir-backup = ${directory:backup}
find-bin = {{ findutils_location }}
diff-bin = {{ diffutils_location }}

[matomo-backup.sh]
recipe = slapos.recipe.template:jinja2
template = {{ matomo_backup_sh }}
rendered = ${directory:scripts}/matomo-backup
context =
  section parameter_dict instance-parameter
  key php_bin php-bin:wrapper-path
depends =

Edit file "buildout.hash.cfg"

[template-matomo-backup.sh]
filename = matomo-backup.sh.in
md5sum =

Add variable "part-list" in file "software.cfg"

[custom-application-deployment]
...
part-list = matomo-backup.sh

This variable will be readed by Lamp stack and make the bash file work.

The content of file "matomo-backup.sh.in"

And about the content of this file, it will create a temporary folder and copy the matomo configuration files into it, then check if the action has been totally done, if it has, it will change the name of the folder to the time of backup, and if not, it will remove the whole folder. That means this script backs up matomo's configuration and plugin information in a transactional way. 
#!/bin/sh

set -e
set -x

#checkout if directory and matomo resources exist

if [ ! -d {{ parameter_dict['document-root'] }}/matomo/config ]; then
  exit 1;
fi

if [ ! -f {{ parameter_dict['document-root'] }}/matomo/config/config.ini.php ]; then
  exit 0;
fi

trap 'rm -rf "$TMPFILE"' EXIT TERM INT

#we reserve backup folder in a month
{{ parameter_dict['find-bin'] }}/bin/find {{ parameter_dict['dir-backup'] }} -mtime +30 -type d |{{ parameter_dict['find-bin'] }}/bin/xargs rm -rf

#backup

TMPFILE=$(mktemp -d -p "{{ parameter_dict['dir-backup'] }}")

{{ php_bin }} {{ parameter_dict['document-root'] }}/matomo/console plugin:list > $TMPFILE/plugins_list
cp -rf {{ parameter_dict['document-root'] }}/matomo/config $TMPFILE/config
cp -rf {{ parameter_dict['document-root'] }}/matomo/plugins $TMPFILE/plugins

#check if copy-action finish well

if test ! -z "$({{ parameter_dict['diff-bin'] }}/bin/diff -r $TMPFILE/config {{ parameter_dict['document-root'] }}/matomo/config)"; then
  exit 1
fi

if test ! -z "$({{ parameter_dict['diff-bin'] }}/bin/diff -r $TMPFILE/plugins {{ parameter_dict['document-root'] }}/matomo/plugins)"; then
  exit 1
fi

#check if the file plugins_list has been created
if [ ! -f $TMPFILE/plugins_list ]; then
  exit 1
fi

#if all things go well, change the name of folder with date now
if [ -d $TMPFILE ]; then
  mv -f $TMPFILE {{ parameter_dict['dir-backup'] }}/`date +%m-%d-%Y-%T`
fi

exit 0

Check output bash file

run the command in terminal to rebuild:
cd ~/srv/project/slapos/software/matomo-tutorial
../../update-hash
slapos node software --all
slapos node instance --all

Normally, now you can find corresponding output file in directory "~/srv/runner/instance/slappartX/etc/run"

X is the slappart where you apache server runs on, if you are not sure, you can check it with the command

slapos node status