resistance is obsolete ™ ;-)

Backup

As posted by Stephan in the users-de list ...

ich habe mal eine ganz prinzipielle Frage zur Datensicherung euerer ogo Daten.
Macht ihr per cron ein Backup bestimmter Files oder macht ihr snapshots vom
System? Ich bin gerade dabei, mich mit diesem Thema ein wenig
auseinanderzusetzen, weiß aber nicht so recht, wie ich ansetzen soll.
======================================
Ich habe dafuer ein shell script geschrieben (pg_backup.sh) und
fuehre es per cron als user postgres aus. Ich benutze Debian Woody,
sollte aber auch auf anderen Distributionen laufen. Wahrscheinlich
ist es sinnvoll das Script soweit zu erweitern, dass es auch einige
Verzeichnisse "tar't". Z.B.:
/usr/lib/opengroupware.org/documents/
/usr/lib/opengroupware.org/news/
Installations Hinweise fuer script:
als root folgende Verzeichnisse anlegen fuer backup und log files:
mkdir -p /var/backups/postgres
mkdir -p /var/log/postgres
chown postgres:root /var/backups/postgres
chown postgres:root /var/log/postgres
Gebrauch auf eigene Gefahr ;-)
###snip pg_backup.sh###
#!/bin/bash
#-------- header ------------------------------------------------------
# file name : "pg_backup.sh"
# directory : "/usr/lib/postgresql/bin"
# version : "0.0.1"
# author : "Stephan Schlott"
# contact : "support@ssnd.de"
# date : "Wed May 26 18:04:19 CEST 2004"
# last change :
#-------- variable declaration-----------------------------------------
# vailed options / option string for getopts
# => needs to be adapted
OPTS="hlf:m:"
# name of new logfile
NEW_FILE=`date +%y%m%d%H%M%S`
# default rotate function
D_ROTATE='print_help "no database specified"'
# default rotate function
D_EXEC=""
# default max number of backupfile
MAX_BACKUP=14
# logfile path
LOGFILE=/var/log/postgres/pg_backup.log
# backupfile path
BACKUPDIR=/var/backups/postgres
#---------function declaration-----------------------------------------
function print_help() {
new_echo "$1";
cat << USAGE
synopsis: backup and rotating postgresql database
syntax : pg_backup [options] -f <database name>
example : pg_backup -l -m 7 -f example-db
backup example-db and keep only the last 7 backups
options :
-h print this message
-l stdin and stderror are logged in default log
file
-m max number of backup file saved chronological
(default 14 days)
-f <database name> (mandatory)
USAGE
exit 0;
} # end function print_help
function option_check() {
while getopts $OPTS OPTION
do
case $OPTION in
h) print_help;
;;
l) LOG_EXEC=log_exec;
;;
m) MAX_BACKUP="$OPTARG";
;;
f) BACKUP_DB="$OPTARG";
D_ROTATE=backup_rotate;
;;
?) print_help;
;;
esac
done
} # end function option_check
function log_exec() {
exec 1>>$LOGFILE
exec 2>>$LOGFILE
} # end function log_exec
function new_echo() {
MESSAGE=$1
echo ""
echo "$MESSAGE"
echo ""
} # end function new_echo
function clear_backup() {
cd $BACKUPDIR
COUNTER=0
ls -t [0-9]*$BACKUP_DB.sql |
while read FILE
do
COUNTER=`expr $COUNTER + 1`
if test "$COUNTER" -gt "$MAX_BACKUP" && test -w $FILE
then
echo "deleted backupfile: $FILE"
rm "$FILE"
fi
done
} # end function clear_backup
function backup_rotate() {
BACKUP_FILE="$NEW_FILE$BACKUP_DB.sql"
cd $BACKUPDIR
new_echo "---------- `date` start rotate backup file: $BACKUP_FILE
----------"
echo "generated backup file: $BACKUP_FILE"
pg_dump -o "$BACKUP_DB" > $BACKUP_FILE
clear_backup
new_echo "---------- `date` end rotate backup file: $BACKUP_FILE
----------"
} # end function backup_rotate
#-------- main programm -----------------------------------------------
function main() {
PARA="$@"
option_check $PARA;
# don't execute log_exec if no database has been specified ;-)
test "$D_ROTATE" = "backup_rotate" && eval $LOG_EXEC;
eval $D_ROTATE;
exit 0;
} # end function main
main "$@"
###snap pg_backup.sh###
====================================================
Recovern kannst Du uebrigens mit folgenden Befehlen,
wenn auch die Art mit dropdb etwas drastisch ist
(loescht die aktuelle ogo Datenbank) fuer Hinweise
auf eine elgantere Loesung waere ich dankbar:
### begin ogo database recovery ###
***vorsicht, vorsicht vorsicht***
***vorsicht, vorsicht vorsicht***
***vorsicht, vorsicht vorsicht***
postgres@woody:~$ dropdb ogo
postgres@woody:~$ createdb ogo
postgres@woody:~$ psql ogo
Welcome to psql, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help on internal slash commands
\g or terminate with semicolon to execute query
\q to quit
ogo=# \i /var/backups/postgres/040525115002ogo.sql
# 040525115002ogo.sql ein backup file erstellt durch das script
### end ogo database recovery ###
Also bei mir konnte ich auf diese Art und Weise verschiedene Datenbanken
laden. Allerdings fehlten die Dokumente unter news und documents. Natuerlich
auch noch Standardeinstellungen die im /usr/lib/opengroupware.org Verzeichnis
geaendert wurden. Wahrscheinlich ist es am Besten, das ganze Verzeichnis einmal
zu sichern.
We welcome your feedback!
Trademarks.  
This site is sponsored by
SKYRIX Software AG
ZideOne GmbH
MDlink