WebsiteBaker Support (2.12.x) > Modules
Plugin zur Überwachung
Martin Hecht:
Hallo,
--- Quote from: dbs on October 10, 2019, 10:38:39 AM ---Mir scheint Martins Variante die "einfachste".
Das würde ich gern mal in Aktion sehen.
--- End quote ---
Ehrlich gesagt habe ich in meinem Post zwei Ansätze, die ich für unterschiedliche Dinge einsetze, kombiniert.
In der Kombination habe ich es aber nicht live am Laufen, aber ein Skript, das aus der crontab heraus einmal am Tag aufgerufen wird, könnte in etwa so aussehen:
--- Code: ---#!/bin/bash
# example for a cronjob to dump database and inform admin about relevant changes
#
# to make the cronjob run correctly and inform you about the output,
# make sure to set up the values for $SHELL, $MAILTO, and $PATH in the crontab
#
# make sure you have mysqldump installed and passwordless access configured
# for example by placing a .my.cnf file in the $HOME of the user who runs this script,
# adjust the values accordingly
#
# cat .my.cnf
# [client]
# host = localhost
# user = root
# password = y0ur_5up3r_secur3_passw0rd
#
# define a location for the dumps, protect this directory, e.g. via .htaccess
# or adjust the unix permissions such that the web server user can't go in
BACKUPDIR=$HOME/backups
# the database name
DATABASE_NAME=cms
TODAY=$(date +%Y-%m-%d)
cd $BACKUPDIR
mysqldump --databases $DATABASE_NAME > current.sql
# copy the current dump
cp -a current.sql ${DATABASE_NAME}-${TODAY}.sql
gzip ${DATABASE_NAME}-${TODAY}.sql
# in case the last.sql was not there, create an empty file
touch last.sql
# create the output and filter it - you might want to grep out some more noise.
# Maybe the IP address from where a user is logged in causes some trouble
# the sed is to make the output more human readable if you like...
diff -u0 last.sql current.sql \
| grep -v dbsessions \
| sed 's/^+/ADDED CONTENT:\n/;s/^-/REMOVED CONTENT:\n/;s/,/,\n/g'
# now move the current one to the last one
mv current.sql last.sql
# if you want to remove old snapshots you might want to keep a copy per month
# for the past year and one per year for the time even more back. You could do
# something like:
#
# MONTH=$(date +%Y-%m)
# cp -a ${DATABASE_NAME}-${TODAY}.sql.gz ${DATABASE_NAME}-${MONTH}.sql.gz
# find -maxdepth 1 -name ${DATABASE_NAME}-'*-*-*.sql.gz' -mtime +60 -delete
#
# cp -a ${DATABASE_NAME}-${TODAY}.sql.gz ${DATABASE_NAME}-${YEAR}.sql.gz
# YEAR=$(date +%Y)
# find -maxdepth 1 -name ${DATABASE_NAME}-'*-*.sql.gz' -mtime +400 -delete
#
--- End code ---
der cronjob muss nicht unbedingt auf dem Host laufen, auf dem das Skript ausgeführt wird. Der Cronjob kann ja auch eine passwortfreie ssh-Verbindung zu dem Host aufmachen, auf dem das Skript läuft und dieses dort aufrufen. Der Output wird dann zurück geschickt und vom Cron-Daemon per Mail weitergeleitet.
Martin
Martin Hecht:
ein wichtiger Hinweis noch zum passwortfreien Datenbankzugriff: Das my.cnf File stellt eine Möglichkeit dafür dar, aber es ist wichtig, dieses File für den Webserver nicht lesbar zu machen. Wenn das in dem Webspace nicht geht, dann muss man das Passwort aus dem cronjob heraus wie "interaktiv" eingeben. "expect" bietet sich dazu an, die Eingaben zu simulieren, als würden sie interaktiv erfolgen, auch innerhalb des cronjobs.
dbs:
Da ich schon einen htaccess geschützten Ordner habe um mit PHP dumps und File-Backups zu machen, wäre eine PHP Variante zum Vergleichen auch interessant.Dein Script versuch ich aber erstmal.
Navigation
[0] Message Index
[*] Previous page
Go to full version