Backup-Script for TYPO3

Last modified:

Why an own TYPO3 backup?

Most providers offer backups of the entire web space (database and files) as standard. The problem is that there is usually no guarantee for this (usually to be found in the provider's general terms and conditions). This means that in the worst case you may be left without a website or have to hope to find an old copy somewhere.

These are the advantages of our backup programme

  • additional security through your own copy and therefore independent of the provider
  • different backup intervals and strategies can be configured
  • easy manual execution possible (if it has to be done quickly)
  • easy mirroring of data to other servers
  • Simple and fast restoring possible
  • no licensing costs, the software is published under the "freeware" licence conditions GPL v2.0

Which data must be backed up with a TYPO3 backup?

For a complete TYPO3 backup, the following data must usually be backed up:

  • the database
  • the webroot directory
  • the subdirectory "fileadmin" in the TYPO3 folder
  • the subdirectory "typo3conf" in the TYPO3 folder
  • the subdirectory "uploads" in the TYPO3 folder

Since TYPO3 itself is available for download in almost every version, a complete backup of the typo3 folder is not necessary.

What does the TYPO3 backup programme do?

The programme is a bash script that runs under a suitable Unix environment.

2 different types of backups can be made:

  • only the TYPO3 database(call with parameter "-d")
  • TYPO3 database and all files(standard call)

The data is collected in a (tar) archive and then compressed in a password-protected way. Finally, a mail is sent to a stored address containing the most important data and any errors that may have occurred:

Ex.:

backup scope: database and files
number of errors in errorlog: 1 (see below)
size of database dump: 51M
complete size of backup: 415M
end of backup process: 2021-05-25_15:14:58
---------------------------------------------------------------------
Warning: Using a password on the command line interface can be insecure.

A mirroring of the data, in order to then also be independent of the server availability, is also no problem. The files can be played onto other servers at any time via a secure connection. Password protection provides additional security and ensures that only authorised employees have access to the data.

Backup strategies

The backup programme can map almost any backup strategy with appropriate knowledge of shell programming.

The following concept is provided by default:

  • a distinction is made between two archives: an archive that contains only the database and an archive that contains both the database and all files.
  • on the web server there are (outside the root path, i.e. not accessible via the web) two corresponding subdirectories "db" and "all" for this purpose.
  • Since the database develops much more dynamically than the files, and the latter are usually stored locally on a computer anyway, the database should be backed up at least daily. We recommend a time around midday, as most providers carry out a nightly backup, which reduces potential data loss to a maximum of approx. 12 hours.
  • The database backup is kept for 7 days and then deleted by the programme during the next run.  This value is arbitrary and can be changed depending on the backup size and server capacity.
  • In view of the size, a complete backup should take place less frequently and be kept for longer. Monthly backups, which are kept for three months, are provided by default. These are also arbitrarily selected values that can be changed without any problems.

Note:
Please note that the backups may contain personal data (e.g. form data). For data protection reasons, these may generally only be stored for a certain period of time. (see also TYPO3 and DSGVO)

Prices and costs

The programme is licensed under the GNU General Public License and is therefore provided free of charge. We charge the following costs for the installation:

 

Our performance Non-Profit Companies
Installation, implementation of a backup strategy, testing and commissioning 390,00 € 450,00 €
Mirroring the data to another server 195,00 € 225,00 €

Installation, implementation of a backup strategy, testing and commissioning
Non-Profit: 390,00 €
Companies 450,00 €
Mirroring the data to another server
Non-Profit: 195,00 €
Companies 225,00 €

Code

You can use and adapt the code below. Please note the terms of the GNU GPL licence. In particular:

  • a guarantee on the functionality is excluded
  • any liability is excluded
  • the copyright notice must be preserved/continued (also in case of modifications)
#!/bin/bash

# Copyright 2021 Oliver Wassenaar (WACON Internet GmbH)

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more  details: <http://www.gnu.org/licenses/>.

# get command line parameter
for arg in "$@"
do
    case $arg in
        -d|--database)
        DB_ONLY=1
        shift
        ;;
        -n|--nomail)
        NO_MAIL=1
        shift
        shift
        ;;
    esac
done

# config parameter 
sitename=wacon
targetdir=<TARGETDIR>/backup
projectdir=<WEB_ROOTPATH>
backupfile=$sitename$(date +"%Y%m%d")_$$
dbfile=db.sql_$$
logfile=$targetdir/log/log_$$
errorlog=$targetdir/log/error_$$
mailtext=$targetdir/mailtxt_$$

# db credentials
dbname=<DB_NAME>
host=<DB_HOST>
user=<DB_USER>
password=<DB_PASSWORD>


# password of backup file 
zippw=<MYPASSWD>

# comma separated list of mail receiver
mailreceiver="<TO_ADDRESS1>, <TO_ADDRESS2>, ..."
mailsender="<SENDER_ADDRESS>"

# delete all log files older than 30 days
find $targetdir/log -maxdepth 1 -type f -name '*' -mtime +30 -exec rm {} \;

exec 1>>$logfile 2>>$errorlog

echo "Begin backup: $(date +%F_%T)"

# delete all db backups older than 7 days (e.g. to run the db backup every day)
find $targetdir/archive/db -maxdepth 1 -type f -name '*' -mtime +7 -exec rm {} \;

# database dump
mysqldump --opt --no-tablespaces -h$host -u$user -p$password $dbname -r $targetdir/$dbfile

cd $targetdir
tar cvf $backupfile $dbfile 

if ! [[ -n "$DB_ONLY" ]]
then
	# delete all files older than 90 days(in this case you shouldn't run the complete backup too frequently, e.g. once a month)
	find $targetdir/archive/all -maxdepth 1 -type f -name '*' -mtime +90 -exec rm {} \;
	cd $projectdir
	# files of rootpath(e.g. htaccess, favicon, etc.) 
	find . -maxdepth 1 -type f -print0 | xargs -0 tar rf $targetdir/$backupfile

	# important directories (uploads possibly not necessary)
	find fileadmin -type f -print0 | xargs -0 tar rvf $targetdir/$backupfile
	find typo3conf -type f -print0 | xargs -0 tar rvf $targetdir/$backupfile
	find uploads -type f -print0 | xargs -0 tar rvf $targetdir/$backupfile
fi

backupsize=$(du -h $targetdir/$backupfile | awk '{print $1}')
dbsize=$(du -h $targetdir/$dbfile | awk '{print $1}')

# compress files
cd $targetdir
zip -P $zippw $backupfile.zip $backupfile

if ! [[ -n "$DB_ONLY" ]]
then
	backupscope="database and files"
	# move to archive
	mv $backupfile.zip $targetdir/archive/all/
else
        backupscope="database ONLY"
        mv $backupfile.zip $targetdir/archive/db/
fi


#clean up
rm $backupfile
rm $dbfile 


if ! [[ -n "$NO_MAIL" ]]
then
	echo "backup scope: $backupscope" > $mailtext
	errno=$(wc -l $errorlog |  awk '{print $1}')
	echo "number of errors in errorlog: $errno (see below)" >> $mailtext
	echo "size of database dump: $dbsize" >> $mailtext
	echo "complete size of backup: $backupsize"  >> $mailtext
	echo "end of backup process: $(date +%F_%T)"  >> $mailtext
	echo "---------------------------------------------------------------------"  >> $mailtext

	cat $errorlog >> $mailtext

	cat $mailtext | mail -s "Backup: $sitename" $mailreceiver -r$mailsender 

	rm $mailtext
fi

 

 

Any more questions?

Do you still have questions or would you like a free consultation? We are happy to be at your disposal as TYPO3 service provider.