Skip to content

Commit

Permalink
[ADD] v1.1
Browse files Browse the repository at this point in the history
[CHG] switch to mysql config file
[CHG] the readme
[CHG] drop-database to drop-table
[CHG] bash echo output
  • Loading branch information
felix-berlin committed Jun 5, 2020
1 parent ef2073c commit 5f35599
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 68 deletions.
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
**The Discribtion follows!**
# Matomo backup

This repository contains one bash script and a MySQL config file.

This script is aimed to use on shared hostings.

## General Informations

This bash script follows the official backup recommandations on matomo.org. Learn more in the [FAQ article](https://matomo.org/faq/how-to/how-do-i-backup-and-restore-the-matomo-data/).
The only thing i added is the add-drop-table option for mysqldump.

**The script does the following steps:**

1. Backup Matomo database (MySQL/MariaDB)
2. Compress database backup with tar and gzip
3. Delete uncompressed database backup file
4. Backup Matomo's files
5. Removing old backups

## What do you need

- **NO root access. Yeh!**
- Access to SSH or SFTP
- The tar extension

## How to use this script

- After Cloning or downloading the repository you'll have to change the variable values in the matomo_backup.sh file. All values which need to be customized are marked with TODO in the script's comments.
- You also have to customized the database credentials in the config.cnf file. You can find your matomo credentials under /config/config.ini.php
- For security reasons I recommend to keep the repository files and backup files outside the public folder. The public folder is usually /var/www/matomo.

### Argument(s)

There is only one argument. If specified, the path to the backup can be determined.

Here is an Example:

```$ ./matomo_backup.sh /alternat/path/to/matomo/backup```
2 changes: 1 addition & 1 deletion config.cnf
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ extended-insert
no-autocommit
quick
single-transaction
add-drop-database
add-drop-table
95 changes: 29 additions & 66 deletions matomo_backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@
#
# Bash script for creating backups of Matomo.
#
# Version 1.0.0
#
# Usage:
# - With backup directory specified in the script: ./MatomoBackup.sh
# - With backup directory specified by parameter: ./MatomoBackup.sh <BackupDirectory> (e.g. ./MatomoBackup.sh /media/hdd/Matomo_backup)
#
# The script is based on an installation of Matomo using nginx and MariaDB, see https://decatec.de/home-server/Matomo-auf-ubuntu-server-18-04-lts-mit-nginx-mariadb-php-lets-encrypt-redis-und-fail2ban/
# Version 1.1.0
#

#
Expand All @@ -23,7 +17,7 @@ backupMainDir=$1

if [ -z "$backupMainDir" ]; then
# TODO: The directory where you store the Matomo backups (when not specified by args)
backupMainDir='/backup/matomo'
backupMainDir='/path/to/matomo/backup'
fi

currentDate=$(date +"%Y%m%d_%H%M%S")
Expand All @@ -32,31 +26,13 @@ currentDate=$(date +"%Y%m%d_%H%M%S")
backupdir="${backupMainDir}/${currentDate}/"

# TODO: The directory of your Matomo installation (this is a directory under your web root)
pathToMatomo='/html/matomo'

matomoBackupFiles='.htaccess robots.txt config/config.ini.php plugins/'
pathToMatomo='/path/to/matomo/'

# TODO: The service name of the web server. Used to start/stop web server (e.g. 'systemctl start <webserverServiceName>')
webserverServiceName='apache'

# TODO: Your web server user
webserverUser='root'

# TODO: The name of the database system (ome of: mysql, mariadb, postgresql)
databaseSystem='mysql'
matomoBackupFiles='config/config.ini.php plugins/ .htaccess robots.txt'

# TODO: Your Matomo database name
matomoDatabase='databaseName'

# TODO: Your Matomo database host
dbHost='localhost'

# TODO: Your Matomo database user
dbUser='databaseUser'

# TODO: The password of the Matomo database user
dbPassword='my_P@SsworD'

# TODO: The maximum number of backups to keep (when set to 0, all backups are kept)
maxNrOfBackups=12

Expand Down Expand Up @@ -88,53 +64,36 @@ fi
#
# Backup DB
#
if [ "${databaseSystem,,}" = "mysql" ] || [ "${databaseSystem,,}" = "mariadb" ]; then
echo
echo "Backup Matomo database (MySQL/MariaDB)..."

if ! [ -x "$(command -v mysqldump)" ]; then
errorecho "ERROR: MySQL/MariaDB not installed (command mysqldump not found)."
errorecho "ERROR: No backup of database possible!"
else
mysqldump --defaults-extra-file=config.cnf "${matomoDatabase}" > "${backupdir}/${fileNameBackupDb}"

echo
echo "Compress database backup with tar and gzip..."
tar -cpzf "${backupdir}/${currentDate}_matomo-db.tar.gz" -C ${backupdir} ${fileNameBackupDb}

echo
echo "Delete uncompressed database backup file"
rm "${backupdir}/${fileNameBackupDb}"
fi
echo
echo "1. Backup Matomo database (MySQL/MariaDB)..."

if ! [ -x "$(command -v mysqldump)" ]; then
errorecho "ERROR: MySQL/MariaDB not installed (command mysqldump not found)."
errorecho "ERROR: No backup of database possible!"
else
mysqldump --defaults-extra-file=config.cnf "${matomoDatabase}" > "${backupdir}/${fileNameBackupDb}"

echo "Done"
echo
elif [ "${databaseSystem,,}" = "postgresql" ]; then
echo "Backup Matomo database (PostgreSQL)..."

if ! [ -x "$(command -v pg_dump)" ]; then
errorecho "ERROR:PostgreSQL not installed (command pg_dump not found)."
errorecho "ERROR: No backup of database possible!"
else
PGPASSWORD="${dbPassword}" pg_dump "${nextcloudDatabase}" -h localhost -U "${dbUser}" -f "${backupdir}/${fileNameBackupDb}"
fi
echo "1.1 Compress database backup with tar and gzip..."
tar -cpzf "${backupdir}/${currentDate}_matomo-db.tar.gz" -C ${backupdir} ${fileNameBackupDb}
echo "Compress database completed!"

echo "Done"
echo
echo "1.2 Delete uncompressed database backup file"
rm "${backupdir}/${fileNameBackupDb}"
echo "Delete uncompressed database backup completed!"
fi


echo
echo "DONE!"
echo "Backup created: ${backupdir}"
echo "Database backup completed!"
echo

#
# Backup matomo
#
echo "Creating Matomo backup..."
echo "2. Backup Matomo's files..."
tar -cpzf "${backupdir}/${nameBackupFileDir}" -C "${pathToMatomo}" ${matomoBackupFiles}

echo "Done"
echo "File backup completed!"
echo

#
Expand All @@ -146,12 +105,16 @@ then

if [[ ${nrOfBackups} > ${maxNrOfBackups} ]]
then
echo "Removing old backups..."
echo "3. Removing old backups..."
ls -t ${backupMainDir} | tail -$(( nrOfBackups - maxNrOfBackups )) | while read -r dirToRemove; do
echo "${dirToRemove}"
rm -r "${backupMainDir}/${dirToRemove:?}"
echo "Done"
echo "Removing old backups completed!"
echo
done
fi
fi
fi

echo
echo "DONE!"
echo "Backup created: ${backupdir}"

0 comments on commit 5f35599

Please sign in to comment.