|
#!/bin/bash |
|
set +x |
|
|
|
# Author Josef Glatz <mailto@josefglatz.at> |
|
# |
|
# This script can be used as simple backup before upgrading a matomo instance |
|
# |
|
# The script takes care of encoded html entities right in the database password |
|
# of the config.ini.php which looks like that is something coming from Mittwald's |
|
# managed application installer script. |
|
# Just remove the part of the script if you don't have to decode entities in the |
|
# application's database password! |
|
|
|
|
|
# Configuration: |
|
# --------------------------------------------------------------------- |
|
|
|
# Path to Matomo's config.ini.php |
|
MATOMO_CONFIG_FILE="/home/www/pACCOUNT/html/matomo/config/config.ini.php" |
|
|
|
# Backup target folder |
|
BACKUP_DIR="/home/www/pACCOUNT/backup/matomo" |
|
# Matomo application folder |
|
MATOMO_DIR="/home/www/pACCOUNT/html/matomo" |
|
|
|
# --------------------------------------------------------------------- |
|
|
|
|
|
|
|
# Array of commands to check |
|
commands=("sed" "grep" "head" "mysqldump" "tar" "date" "mkdir" "tr" "php" "cut") |
|
|
|
# Function to check if a command is available |
|
check_command() { |
|
command -v "$1" >/dev/null 2>&1 |
|
} |
|
|
|
# Loop through commands and check availability |
|
for cmd in "${commands[@]}"; do |
|
if check_command "$cmd"; then |
|
echo "Command $cmd found. Nice." |
|
else |
|
echo "Error: $cmd is not installed or not in PATH" |
|
missing=true |
|
fi |
|
done |
|
|
|
# Exit if any used command is missing |
|
if [ "$missing" = true ]; then |
|
echo "Some required commands are missing. Please install them and try again." |
|
exit 1 |
|
else |
|
echo "All required commands are available. Continuing script..." |
|
fi |
|
|
|
|
|
# Collect database credentials |
|
DB_NAME=$(head -n 20 $MATOMO_CONFIG_FILE | grep "^dbname" | sed 's/dbname = //' | tr -d '"') |
|
DB_USER=$(head -n 20 $MATOMO_CONFIG_FILE | grep "^username" | sed 's/username = //' | tr -d '"') |
|
DB_PASSWORD=$(head -n 20 $MATOMO_CONFIG_FILE | grep "^password" | sed 's/password = //' | tr -d '"') |
|
DB_HOST=$(head -n 20 $MATOMO_CONFIG_FILE | grep "^host" | sed 's/host = //' | tr -d '"') |
|
|
|
if [[ -z "$DB_NAME" || -z "$DB_USER" || -z "$DB_PASSWORD" || -z "$DB_HOST" ]]; then |
|
echo "Error: Not all database credentials can be extracted from the first 20 lines of the config file $MATOMO_CONFIG_FILE." >&2 |
|
exit 1 |
|
fi |
|
|
|
# cleanup db password |
|
# reason: Mittwald Application Installer writes html entities when installing Matomo |
|
echo "Decode HTML entities in database password string" |
|
DB_PASSWORD=$(php -r ' |
|
$args = array_slice($argv, 1); |
|
echo html_entity_decode($args[0]); |
|
' "$DB_PASSWORD") |
|
|
|
|
|
# Create backup directory with timestamp |
|
TIMESTAMP=$(date +"%Y%m%d_%H%M%S") |
|
CURRENT_BACKUP_DIR="$BACKUP_DIR/matomo_backup_$TIMESTAMP" |
|
mkdir -p "$CURRENT_BACKUP_DIR" |
|
|
|
# Step 1: database backup |
|
echo "Executing Matomo database backup..." |
|
PW_SAFE="$DB_PASSWORD" |
|
mysqldump --no-tablespaces -h "$DB_HOST" -u "$DB_USER" -p"$PW_SAFE" "$DB_NAME" > "$CURRENT_BACKUP_DIR/matomo_db_$TIMESTAMP.sql" |
|
|
|
if [ $? -eq 0 ]; then |
|
echo "Database backup successful." |
|
else |
|
echo "Datbase backup failed!" >&2 |
|
exit 1 |
|
fi |
|
|
|
# Step 2: file backup |
|
echo "Executing Matomo file backup..." |
|
tar -czf "$CURRENT_BACKUP_DIR/matomo_files_$TIMESTAMP.tar.gz" -C "$MATOMO_DIR" . |
|
if [ $? -eq 0 ]; then |
|
echo "File backup successful." |
|
else |
|
echo "File backup failed!" >&2 |
|
exit 1 |
|
fi |
|
|
|
echo "" |
|
echo "-------------------------------------------" |
|
echo "" |
|
echo "Backup successfully." |
|
echo "" |
|
echo "-------------------------------------------" |
|
echo "" |
|
echo "Backup files saved in $CURRENT_BACKUP_DIR." |
|
echo "Please check https://matomo.org/faq/how-to-install/faq_138/ for further details about matomo backup/restore." |
|
echo "" |
|
echo "All existing backups created by this script require $(du -sh $BACKUP_DIR | cut -f1) on the hard disk." |
|
echo "Please consider deleting old / not needed backups manually!" |