Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
— | anwenderwiki:hoster:belwue:backup [2013/03/17 15:43] (aktuell) – angelegt - Externe Bearbeitung 127.0.0.1 | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
+ | {{tag> belwue backup mysql}} | ||
+ | |||
+ | ====== Backup des Webspaces und der Datenbank bei Belwue ====== | ||
+ | |||
+ | Weil man bei Belwue selbst für das Backup sorgen muss, habe ich folgendes Skript geschrieben: | ||
+ | |||
+ | - Download des Webspaces per rsync und ssh-key (muss vorher dort hinterlegt werden) | ||
+ | - Download der Datenbankinhalte mit Hilfe eines weiteren Skripts | ||
+ | |||
+ | ===== Voraussetzung ===== | ||
+ | * Installation von ssh, curl, rsync | ||
+ | * Könnte auch für die anderen Musterlösungen funktionieren, | ||
+ | |||
+ | Sinnvolle Voraussetzung ist, einen ssh-Schlüssel auf dem/den Webserver/ | ||
+ | $ ssh-keygen -f sync.rsa | ||
+ | Wenn man einen Passphrase verwendet (sinnvoll) ist die spätere Automatisierung via cronjob umständlicher und das Backup sollte manuell gestartet werden. Wenn man keinen Passphrase verwendet, kann jeder, der an den Schlüssel kommt die Daten auf dem Webserver bei Belwue beliebig verändern, allerdings hat es den Vorteil das untenstehende Skripte einfach als Cronjob automatisiert gestartet werden können.((Wer beides haben will: Passphrase **und** keine Benutzerinteraktion, | ||
+ | |||
+ | $ mkdir .ssh | ||
+ | $ chmod 700 .ssh | ||
+ | $ cat sync.rsa >> .ssh/ | ||
+ | $ chmod 600 .ssh/ | ||
+ | $ echo "order allow, | ||
+ | $ echo "deny from all" >> .ssh/ | ||
+ | wobei ich mir bei Belwue nicht sicher bin, ob der Inhalt der .htaccess Datei nötig ist, das Vorhandensein aber schon. | ||
+ | |||
+ | Dieses Verzeichnis am einfachsten mit Filezilla oder direkt mit rsync auf den Webserver übertragen: | ||
+ | $ rsync -avP .ssh/ aYYXXXXX@moodle01.belwue.de:/ | ||
+ | wobei hier Benutzername aYYXXXXX, webserver (andere Beispiele: pubwwwX.belwue.de), | ||
+ | |||
+ | Für jeden Webspace, den man bei belwue hat muss man das separat machen (aber mit demselben Schlüssel möglich). | ||
+ | |||
+ | ===== Backup der Homepage ===== | ||
+ | Mit Schlüssel ist ein Backup der Homepage einfach möglich, z.B.: | ||
+ | <code bash backupwebspace.sh> | ||
+ | #!/bin/bash | ||
+ | |||
+ | privatekey="/ | ||
+ | moodlehome="/ | ||
+ | pubwwwhome="/ | ||
+ | |||
+ | mkdir -p moodle01.belwue.de/ | ||
+ | rsync --delete -e "ssh -i $privatekey" | ||
+ | rsync --delete -e "ssh -i $privatekey" | ||
+ | rsync --delete -e "ssh -i $privatekey" | ||
+ | rsync --delete -e "ssh -i $privatekey" | ||
+ | </ | ||
+ | oder je nachdem, was man alles im Webspace speichern will. | ||
+ | Der Einsatz von < | ||
+ | |||
+ | ===== Backup der Datenbanken ===== | ||
+ | Wer bei Belwue Webspace hat, benutzt vermutlich auch eine Datenbank, an die man aber nicht so ohne Weiteres rankommt. | ||
+ | Bei einem Moodle-Auftritt ist phpmyadmin bereits im Unterverzeichnis " | ||
+ | |||
+ | < | ||
+ | cd pubwww5.belwue.de | ||
+ | ../ | ||
+ | |||
+ | cd .. | ||
+ | cd moodle01.belwue.de | ||
+ | ../ | ||
+ | </ | ||
+ | |||
+ | Der Apache-user und das Apache-passwort sind auf dem Webserver unter htdoc/ | ||
+ | Der Phpmyadmin-user und das PhpMyAdmin-Passwort sind auch initial von Belwue vorgegeben worden. Der dbadm... Benutzer ist meines Wissens nicht nötig. | ||
+ | <note important> | ||
+ | Achtung, die Passwörter können Sonderzeichen enthalten. Die einfach Variante ist, das Passwort für einen Apacheuser zu erstellen / zu ändern. Für den PhpMyAdmin-user ist das, soviel ich weiß nicht möglich (vielleicht direkt in der Datenbank(!)). | ||
+ | Dann muss man Sonderzeichen in obigem Skript mit Hilfe von voranstellen eines " | ||
+ | Alternativ: Eine [[http:// | ||
+ | </ | ||
+ | |||
+ | ===== mysql-export.sh ===== | ||
+ | Dieses Skript wurde [[https:// | ||
+ | <code bash mysql-export.sh> | ||
+ | #!/bin/bash | ||
+ | |||
+ | #set -x | ||
+ | |||
+ | # This program is free software published under the terms of the GNU GPL. | ||
+ | # | ||
+ | # Forked: http:// | ||
+ | # (C) Institut TELECOM + Olivier Berger < | ||
+ | # $Id: curl-backup-phpmyadmin.sh 12 2011-12-12 16:02:44Z berger_o $ | ||
+ | |||
+ | # Clean up and add parameter handling by Artem Grebenkin | ||
+ | # < | ||
+ | # | ||
+ | # Script seemed to have been posix-sh, but partly in bash-style, converted to bash style completely | ||
+ | # Tobias Küchel < | ||
+ | # | ||
+ | # Optional: This saves dumps of your Database using CURL and connecting to | ||
+ | # phpMyAdmin (via HTTPS), keeping the 10 latest backups by default | ||
+ | # | ||
+ | # Tested on phpMyAdmin 3.4.5 | ||
+ | # | ||
+ | # For those interested in debugging/ | ||
+ | # add-on LiveHttpHeaders is a very interesting extension to debug HTTP | ||
+ | # transactions and guess what's needed to develop such a CURL-based | ||
+ | # script. | ||
+ | # | ||
+ | # Arguments: mysql-export.sh [-h|--help] [--stdout] [--tables=< | ||
+ | # -h, --help: Print help | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # | ||
+ | # Common uses: mysql-export.sh --tables=hotel_content_provider --add-drop --database=hs --stdout --use-keychain --apache-user=betatester --phpmyadmin-user=hs --apache-password=www.example.com\ \(me\) --phpmyadmin-password=phpmyadmin.example.com --host=https:// | ||
+ | # exports and imports on the fly in local db | ||
+ | |||
+ | # Please adapt these values : | ||
+ | |||
+ | MKTEMP=mktemp # ' | ||
+ | TMP_FOLDER=/ | ||
+ | COMPRESSION=bzip2 | ||
+ | USE_KEYCHAIN=0 | ||
+ | DEBUG=0 | ||
+ | |||
+ | ## following values will be overwritten by command line arguments | ||
+ | STDOUT= | ||
+ | DB_TABLES= | ||
+ | ADD_DROP=0 | ||
+ | APACHE_USER= | ||
+ | APACHE_PASSWD= | ||
+ | PHPMYADMIN_USER= | ||
+ | PHPMYADMIN_PASSWD= | ||
+ | DATABASE= | ||
+ | REMOTE_HOST= | ||
+ | # End of customisations | ||
+ | |||
+ | |||
+ | ## debugging function | ||
+ | function decho { | ||
+ | [ $DEBUG -eq 1 ] && echo $@ | ||
+ | } | ||
+ | |||
+ | function usage | ||
+ | { | ||
+ | cat << EOF | ||
+ | Arguments: mysql-export.sh [-h|--help] [--stdout] [--tables=< | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | -- [curl_options] | ||
+ | -h, --help: Print help | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | In that case --apache-password and --phpmyadmin-password will be used | ||
+ | as account name for search in Mac Os X keychain. | ||
+ | |||
+ | * You need to set at least those parameters on the command line or in the script | ||
+ | |||
+ | | ||
+ | |||
+ | This exports and imports on the fly into local db | ||
+ | EOF | ||
+ | } | ||
+ | |||
+ | curloptions=0 | ||
+ | curlopts="" | ||
+ | for arg in $@ | ||
+ | do | ||
+ | if [ $arg == " | ||
+ | then | ||
+ | curloptions=1 | ||
+ | elif [ $curloptions -eq 1 ] | ||
+ | then | ||
+ | curlopts+=" | ||
+ | elif [ $arg == ' | ||
+ | then | ||
+ | STDOUT=1 | ||
+ | elif [[ $arg =~ ' | ||
+ | then | ||
+ | DB_TABLES=$arg | ||
+ | elif [[ $arg =~ ' | ||
+ | then | ||
+ | COMPRESSION=${arg: | ||
+ | elif [ $arg == ' | ||
+ | then | ||
+ | ADD_DROP=1 | ||
+ | elif [[ $arg =~ ' | ||
+ | then | ||
+ | APACHE_USER=${arg: | ||
+ | elif [[ $arg =~ ' | ||
+ | then | ||
+ | APACHE_PASSWD=${arg: | ||
+ | elif [[ $arg =~ ' | ||
+ | then | ||
+ | PHPMYADMIN_USER=${arg: | ||
+ | elif [[ $arg =~ ' | ||
+ | then | ||
+ | PHPMYADMIN_PASSWD=${arg: | ||
+ | elif [[ $arg =~ ' | ||
+ | then | ||
+ | DATABASE=${arg: | ||
+ | elif [[ $arg =~ ' | ||
+ | then | ||
+ | REMOTE_HOST=${arg: | ||
+ | elif [ $arg == ' | ||
+ | then | ||
+ | USE_KEYCHAIN=1 | ||
+ | else | ||
+ | usage | ||
+ | exit 0 | ||
+ | fi | ||
+ | done | ||
+ | curlopts+=" | ||
+ | decho "Curl options: $curlopts" | ||
+ | |||
+ | # is APACHE auth really necessary? | ||
+ | #[ -z " | ||
+ | #if [ -z " | ||
+ | if [ -z " | ||
+ | then | ||
+ | usage | ||
+ | exit 1 | ||
+ | fi | ||
+ | |||
+ | ## not tested (01.03.13) | ||
+ | if [ $USE_KEYCHAIN -eq 1 ] | ||
+ | then | ||
+ | APACHE_PASSWD=`security 2>&1 >/ | ||
+ | PHPMYADMIN_PASSWD=`security 2>&1 >/ | ||
+ | fi | ||
+ | |||
+ | ## which mktemp to use | ||
+ | mkdir -p $TMP_FOLDER || exit 1 | ||
+ | if [ " | ||
+ | result=$(`which mktemp` " | ||
+ | decho TEMP: $result | ||
+ | fi | ||
+ | if [ " | ||
+ | result=$(`which tempfile` -d " | ||
+ | decho TEMP: $result | ||
+ | fi | ||
+ | |||
+ | |||
+ | ############################################################### | ||
+ | # | ||
+ | # First login and fetch the cookie which will be used later | ||
+ | # | ||
+ | ############################################################### | ||
+ | |||
+ | apache_auth_params=" | ||
+ | |||
+ | curl $curlopts -D $TMP_FOLDER/ | ||
+ | # token=$(grep ' | ||
+ | |||
+ | token=$(grep link $result | grep ' | ||
+ | cookie=$(cat $TMP_FOLDER/ | ||
+ | |||
+ | entry_params=" | ||
+ | decho Apache login: $apache_auth_params | ||
+ | decho PhpMyadmin login: $entry_params | ||
+ | decho Token: $token | ||
+ | decho Cookie: $cookie | ||
+ | ## Try to log in with PhpMyAdmin username and password showing errors if it fails | ||
+ | curl $curlopts -S -D $TMP_FOLDER/ | ||
+ | ## did it fail? | ||
+ | if [ $? -ne 0 ]; then | ||
+ | echo "Curl Error on : curl $curlopts -S -D $TMP_FOLDER/ | ||
+ | exit 1 | ||
+ | fi | ||
+ | ## Was the http-request unsuccessful? | ||
+ | grep -q " | ||
+ | if [ $? -ne 0 ]; then | ||
+ | echo -n "Error : couldn' | ||
+ | grep " | ||
+ | exit 1 | ||
+ | fi | ||
+ | |||
+ | |||
+ | |||
+ | ## prepare the post-parameters | ||
+ | post_params=" | ||
+ | ## later: post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | ## later: post_params+="& | ||
+ | post_params+="& | ||
+ | ## later: post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | ## later: post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | |||
+ | if [ $ADD_DROP -eq 1 ]; then | ||
+ | post_params+="& | ||
+ | fi | ||
+ | |||
+ | # | ||
+ | target=" | ||
+ | |||
+ | post_params+="& | ||
+ | case $COMPRESSION in | ||
+ | gzip) | ||
+ | target+=" | ||
+ | bzip2) | ||
+ | target+=" | ||
+ | zip) | ||
+ | target+=" | ||
+ | none) | ||
+ | ;; | ||
+ | *) | ||
+ | target+=" | ||
+ | esac | ||
+ | |||
+ | decho Database: $DATABASE | ||
+ | if [ -n " | ||
+ | DB_TABLES=${DB_TABLES/ | ||
+ | DB_TABLES=${DB_TABLES//,/& | ||
+ | DB_TABLES=${DB_TABLES: | ||
+ | decho Tables: $DB_TABLES | ||
+ | | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | |||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | else | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | fi | ||
+ | |||
+ | ## the important curl command, either output to stdout additionally | ||
+ | if [ -n " | ||
+ | decho " Exportcommand: | ||
+ | curl $curlopts -g -S -D $TMP_FOLDER/ | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | else | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | post_params+="& | ||
+ | fi | ||
+ | |||
+ | ## the important curl command, either output to stdout additionally | ||
+ | if [ -n " | ||
+ | decho " Exportcommand: | ||
+ | curl $curlopts -g -S -D $TMP_FOLDER/ | ||
+ | else | ||
+ | decho " Exportcommand: | ||
+ | curl $curlopts -g -S -O -D $TMP_FOLDER/ | ||
+ | |||
+ | ## check if there was an attachement | ||
+ | grep -q " | ||
+ | if [ $? -eq 0 ] ; then | ||
+ | mv export.php $target | ||
+ | echo " | ||
+ | else | ||
+ | echo " | ||
+ | exit 1 | ||
+ | fi | ||
+ | fi | ||
+ | |||
+ | # remove the old backups and keep the 10 younger ones. | ||
+ | #ls -1 backup_mysql_*${database}_*.gz | sort -u | head -n-10 | xargs -r rm -v | ||
+ | rm -f $result | ||
+ | rm -f $TMP_FOLDER/ | ||
+ | rm -f $TMP_FOLDER/ | ||
+ | </ | ||