Mit diesem bash-Skript wird automatisch ein Webseite via ftp vom Server herunterkopiert und auf Wunsch auch die MySQL-Datenbank kopiert. Das Skript kann auch mehrere Webseiten auf einmal sichern.
Zur Konfiguration wird eine eigene Konfigurationsdatei benötigt, welche die Daten für den FTP-Zugang und die Adresse der Webseite beinhaltet.
#!/bin/bash
# Copyright by Michael Mayer
# 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.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
######################################################################
## Beginn Konfigurationsbereich
##
# Vollständiger Pfad zur Konfigurationsdatei
ACCOUNT_LIST=/etc/joomla-backup.conf
# Zielverzeichnis für das Backup
BACKUP_TO=~/joomla-backup/
##
## Ende Konfigurationsbereich
######################################################################
DAY=`date "+%Y%m%d"`
cd $BACKUP_TO
# Unterverzeichis für das aktuelle Backup anlegen
if [ ! -e $DAY ]; then
mkdir $DAY
fi
cd $DAY
# Schleife für alle Webseiten
for nr in $(cut -d':' -f1 $ACCOUNT_LIST)
do
FETCH_SITE=$(grep "^$nr:" $ACCOUNT_LIST | cut -d':' -f2)
if [ $FETCH_SITE -eq 1 ]; then
# Auslesen der Konfigurationsdatei
USERNAME=$(grep "^$nr:" $ACCOUNT_LIST | cut -d':' -f3)
PASSWORD=$(grep "^$nr:" $ACCOUNT_LIST | cut -d':' -f4)
FTP_URL=$(grep "^$nr:" $ACCOUNT_LIST | cut -d':' -f5)
WWW_ROOT=$(grep "^$nr:" $ACCOUNT_LIST | cut -d':' -f6)
URL=$(grep "^$nr:" $ACCOUNT_LIST | cut -d':' -f7)
FETCH_DB=$(grep "^$nr:" $ACCOUNT_LIST | cut -d':' -f8)
WWW_ROOT=$(echo $WWW_ROOT | awk '{sub(/[/]+$/, "");print}' | awk '{sub(/^[/]+/, "");print}')
# Herunterladen der Webseite
wget --ftp-user=$USERNAME --ftp-password=$PASSWORD -xr ftp://$FTP_URL/$WWW_ROOT/*
if [ $FETCH_DB -eq 1 ]; then
# Auslesen der DB-Konfiguration von Joomla!
cd $FTP_URL/$WWW_ROOT
DB_USER=$(grep '$user' configuration.php | awk '{sub(/[;]+$/, "");print}' | cut -d\' -f2)
DB_PASSWORD=$(grep '$password' configuration.php | awk '{sub(/[;]+$/, "");print}' | cut -d\' -f2)
DB_HOST=$(grep '$host' configuration.php | awk '{sub(/[;]+$/, "");print}' | cut -d\' -f2)
DB_NAME=$(grep '$db ' configuration.php | awk '{sub(/[;]+$/, "");print}' | cut -d\' -f2)
# Erstellen des Backup-Skripts für die Datenbank
echo "<?php" > backup.php
echo "ignore_user_abort(true);" >> backup.php
echo "system(\"/usr/bin/mysqldump -u$DB_USER -p$DB_PASSWORD -h $DB_HOST $DB_NAME > joomla-db.sql\", \$file);" >> backup.php
touch joomla-db.sql
echo "if (\$file==0) {echo "0";} else {echo "1";}" >> backup.php
echo "?>" >> backup.php
# Backup-Skript auf den Server hochladen
ftp ftp://$USERNAME:$PASSWORD@$FTP_URL << END_FTP
cd $WWW_ROOT
put backup.php
put joomla-db.sql
chmod 0777 joomla-db.sql
quit
END_FTP
# Ausführen des DB-Backups
result=$(curl http://$URL/backup.php)
if [ $result -eq 0 ]; then
# Herunterladen der SQL-Datei
wget --ftp-user=$USERNAME --ftp-password=$PASSWORD ftp://$FTP_URL/$WWW_ROOT/joomla-db.sql
else
echo "FEHLER: Das Backup der Datenbank konnte nicht erstellt werden!"
fi
# Aufräumen
rm backup.php
ftp ftp://$USERNAME:$PASSWORD@$FTP_URL << END_FTP
cd $WWW_ROOT
delete backup.php
delete joomla-db.sql
quit
END_FTP
cd $BACKUP_TO/$DAY
fi
fi
done
Die Parameter für jede Webseite werden in einer Zeile durch *:* getrennt angegeben. Eine Zeile hat dabei folgende Parameter
<nr>:<fetch_site>:<username>:<passwort>:<ftp_url>:<www_root>:<http_url>:<fetch_db>
Die Parameter haben dabei folgende Bedeutung:
http_docs oder ähnlich)
* http_url URL des Webservers (ohne führendes http:)Zur Veranschaulichung ist hier ein Beispiel wie die Konfigurationsdatei aussehen kann:
1:1:user1:secret:ftp.example.com:httpdocs:www.example.com:1 2:1:user2:secret:ftp.example.de:httpdocs:www.example.de:0
In diesem Beispiel werden zwei Webseiten gesichert, bei der zweiten wird aber auf das Backup der Datenbank verzichtet.
Folgende Befehle legen den Besitzer der Datei fest und erlauben nur diesem den Schreib-/Lesezugriff:
$ chmod <linux-user> <config-file> $ chmod 0600 <config-file>
Diskussion