forked from felix-berlin/matomo-backup-restore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matomo_backup.sh
executable file
·92 lines (77 loc) · 2.01 KB
/
matomo_backup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
#
# Bash script for creating backups of Matomo.
#
# Version 1.1.1
#
#
# IMPORTANT
# You have to customize the following config files: .main.config.sh and .database.config.cnf
#
. config/.main.config.sh
# Function for error messages
errorecho() { cat <<< "$@" 1>&2; }
#
# Print information
#
echo "Backup directory: ${backupMainDir}" ${currentDate}
#
# Check if backup dir already exists
#
if [ ! -d "${backupdir}" ]
then
mkdir -p "${backupdir}"
else
errorecho "ERROR: The backup directory ${backupdir} already exists!"
exit 1
fi
#
# Backup DB
#
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/.database.config.cnf "${matomoDatabase}" > "${backupdir}/${fileNameBackupDb}"
echo
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
echo "1.2 Delete uncompressed database backup file"
rm "${backupdir}/${fileNameBackupDb}"
echo "Delete uncompressed database backup completed!"
fi
echo
echo "Database backup completed!"
echo
#
# Backup matomo
#
echo "2. Backup Matomo's files..."
tar -cpzf "${backupdir}/${nameBackupFileDir}" -C "${pathToMatomo}" ${matomoBackupFiles}
echo "File backup completed!"
echo
#
# Delete old backups
#
if [ ${maxNrOfBackups} != 0 ]
then
nrOfBackups=$(ls -l ${backupMainDir} | grep -c ^d)
if [[ ${nrOfBackups} > ${maxNrOfBackups} ]]
then
echo "3. Removing old backups..."
ls -t ${backupMainDir} | tail -$(( nrOfBackups - maxNrOfBackups )) | while read -r dirToRemove; do
echo "${dirToRemove}"
rm -r "${backupMainDir}/${dirToRemove:?}"
echo "Removing old backups completed!"
echo
done
fi
fi
rsync -e ssh -avz ${backupdir}/* ${remoteBackup}:${remotePath}
echo
echo "DONE!"
echo "Backup created: ${backupdir}"