Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
home:crameleon:branches:home:lrupp
postgresql-backupscript
postgresql-backupscript
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File postgresql-backupscript of Package postgresql-backupscript
#!/bin/bash # Simple backupscript for PGSQL databases # Author: Lars Vogdt # BSD3 Clause License # PATH=/bin:/usr/bin # # Binaries # AWK='/usr/bin/awk' GREP='/usr/bin/grep' HOSTNAME_BIN='/usr/bin/hostname' MAILX='/usr/bin/mail' SEND_NSCA_BIN='/usr/bin/send_nsca' PG_DUMP='/usr/bin/pg_dump' PG_ISREADY='/usr/bin/pg_isready' PGREP='/usr/bin/pgrep' PGSQL='/usr/bin/psql' SED='/usr/bin/sed' TAIL='/usr/bin/tail' # # Options # DISTCONFIG='/etc/sysconfig/postgresql-backupscript' NAGIOSHOST='localhost' NAGIOS_SERVICE_NAME='PostgreSQL backup' PGSQL_OPTS='' SEND_NSCA_CONFIG='/etc/send_nsca.cfg' SEND_NSCA_HOSTNAME=$(hostname -s) USE_NSCA='no' USE_EMAIL='yes' ################################################## # Default/Fallback values # Don't change them here! Use $DISCONFIG instead ################################################## ABORT_ON_ERROR='no' BACKUPDIR='/root/backup/postgres' CONFIG_FILES="pg_hba.conf pg_ident.conf postgresql.auto.conf postgresql.conf" FQHOSTNAME=$($HOSTNAME_BIN -f) HOST=$($HOSTNAME_BIN -s 2>/dev/null) LOGNAME='pgsql-backup' LOGFILE="/var/log/${LOGNAME}.log" EMAIL='root@localhost' START_BACKUP='yes' EXCLUDE_DB='' FORCE_BACKUP='no' DEBUG='no' RETENTION=14 COMPRESS_EXE='/usr/bin/xz' COMPRESS_OPTS='' COMPRESS_AFTER_UNLOCK='yes' PGSQL_SCRIPT_BEFORE_DUMP='' PGSQL_SCRIPT_AFTER_DUMP='' PGSQL_SCRIPT_AFTER_COMPRESSION='' ################################################## umask 027 unset LANG; cleanup_and_exit(){ test -n "$TMPFILE" -a -f "$TMPFILE" && rm "$TMPFILE" exit $1 } LOG(){ local MESSAGE="$1" local LOG_DATE=$(date "+%b %d %H:%M:%S") if [ -z "$LOGFILE" ]; then HANDLE_MESSAGE "ERROR: LOGFILE is not defined" 1 cleanup_and_exit 1 fi if [ ! -d "$LOGDIR" ]; then mkdir -p "$LOGDIR" || exit 1 echo "$LOG_DATE $HOST $LOGNAME[$$]: function LOG created $LOGDIR" > "$LOGFILE" fi echo "$LOG_DATE $HOST $LOGNAME[$$]: $MESSAGE" >> $LOGFILE || DEBUG="yes" if [ "$DEBUG" = "yes" ]; then echo "DEBUG: $MESSAGE" fi } SEND_NSCA(){ local MESSAGE="$1" local EXIT_CODE="$2" echo -e "$SEND_NSCA_HOSTNAME\t$NAGIOS_SERVICE_NAME\t$EXIT_CODE\t$MESSAGE" | $SEND_NSCA_BIN -H $NAGIOSHOST -c $SEND_NSCA_CONFIG 1>/dev/null } SEND_EMAIL(){ local MESSAGE="$1" local EXIT_CODE="$2" case $EXIT_CODE in 0) MESSAGE="OK: $MESSAGE" ;; 1) MESSAGE="ERROR: $MESSAGE" ;; 2) MESSAGE="WARNING: $MESSAGE" ;; 3) MESSAGE="UNKNOWN: $MESSAGE" ;; esac echo "$MESSAGE" | $MAILX -s "[$LOGNAME] on $FQHOSTNAME" $EMAIL } HANDLE_MESSAGE(){ local MESSAGE="$1" local EXIT_CODE="$2" case "$USE_NSCA" in [Yy][Ee][Ss]) SEND_NSCA "$MESSAGE" "$EXIT_CODE" ;; esac case "$USE_EMAIL" in [Yy][Ee][Ss]) SEND_EMAIL "$MESSAGE" "$EXIT_CODE" ;; esac } test_postmaster_alive(){ local tmpfile="$1" $PG_ISREADY $PGSQL_OPTS 1>/dev/null 2>>"$tmpfile" if [ $? -ne 0 ]; then if [ -s "$tmpfile" ]; then HANDLE_MESSAGE "$(cat $tmpfile)" 1 LOG "$(cat "$tmpfile")" cleanup_and_exit 1 fi fi } print_help(){ echo "Usage: $(basename $0) [-c <configfile>]" echo echo " -d : print debug output" echo " -f : force backup (incl. remove backups in same directory)" echo " -h : print this message" echo " -c <configfile> : use the given config file instead of $DISTCONFIG" echo echo " The target of this script is to create backups of your PGSQL databases" echo " on a regular daily bases." echo " Please find the configuration details in $DISTCONFIG - if you want to" echo " test something, the option -f might be useful to use another file containing" echo " the configuration." echo echo " PLEASE NOTE: " echo " Please have a look at /usr/share/doc/packages/postgresql-backupscript/README.SUSE" echo " as this contains information for password protected databases and further" echo " details." exit 0 } get_datadir(){ DATADIR=$($PGREP -u postgres -fa -- -D | $TAIL -n1 | $AWK '" " { print $4 }') if [ -d "$DATADIR" ]; then echo "$DATADIR" else LOG "ERROR: Could not find DATADIR (got $DATADIR)" HANDLE_MESSAGE "Could not find DATADIR (got $DATADIR)" 1 cleanup_and_exit 1 fi } create_dir(){ local directory="$1" mkdir -p "$directory" 2>"$TMPFILE" || { HANDLE_MESSAGE "$(cat $TMPFILE)" 1 LOG "$(cat "$TMPFILE")" cleanup_and_exit 1 } } function compress_dump() { db_sql="$1" local FUNC_EXIT=0 if [ -x "$COMPRESS_EXE" ]; then LOG "Compressing $db_sql using '$COMPRESS_EXE $COMPRESS_OPTS $db_sql'" if ! $COMPRESS_EXE $COMPRESS_OPTS "$db_sql" then LOG "Compression of $db failed" if [ "$ABORT_ON_ERROR" = 'yes' ]; then cleanup_and_exit 1 fi FUNC_EXIT=1 fi else LOG "Not compressing $db.sql as $COMPRESS_EXE is not executable" fi return "$FUNC_EXIT" } trap cleanup_and_exit 0 1 2 3 7 13 15 while getopts 'dhfc:' OPTION ; do case $OPTION in d) DEBUG='yes' ;; f) START_BACKUP='yes' FORCE_BACKUP='yes' ;; h) print_help ;; c) DISTCONFIG="$OPTARG" ;; esac done shift $(( OPTIND - 1 )) for binary in "$AWK" "$COMPRESS_EXE" "$GREP" "$HOSTNAME_BIN" "$PG_DUMP" "$PG_ISREADY" "$PGREP" "$PGSQL" "$SED" "$TAIL" ; do if [ ! -x "$binary" ]; then LOG "ERROR: bnary $binary can not be executed" HANDLE_MESSAGE "Binary: $binary can not be executed" 1 cleanup_and_exit 1 fi done case $USE_NSCA in [Yy][Ee][Ss]) if [ ! -x "$SEND_NSCA_BIN" ]; then LOG "ERROR: $SEND_NSCA_BIN can not be executed" cleanup_and_exit 1 fi if [ ! -r "$SEND_NSCA_CONFIG" ]; then LOG "ERROR: can not read $SEND_NSCA_CONFIG" cleanup_and_exit 1 fi ;; esac case "$USE_EMAIL" in [Yy][Ee][Ss]) if [ ! -x "$MAILX" ]; then LOG "ERROR: $MAILX can not be executed" fi ;; esac # source our config if [ -f "$DISTCONFIG" ]; then . "$DISTCONFIG" LOGDIR=$(dirname "$LOGFILE") else echo "$DISTCONFIG not found - using defaults" >&2 LOGDIR=$(dirname "$LOGFILE") LOG "$DISTCONFIG not found - using defaults" fi case "$START_BACKUP" in [Yy]*) FINAL_EXIT=0 DATE=$(date "+%Y%m%d") BEGIN_SECONDS=$(date +%s) TMPFILE=$(mktemp /tmp/postgresql-backupscript-XXXXXX) if ! test -d "$BACKUPDIR" -a "$FORCE_BACKUP" != 'yes' ; then case "$CREATE_BACKUPDIR" in [Nn][Oo]) HANDLE_MESSAGE "$BACKUPDIR does not exist. Exiting as CREATE_BACKUPDIR is set to 'no' in $DISTCONFIG" 1 LOG "ERROR: $BACKUPDIR does not exist. Exiting as CREATE_BACKUPDIR is set to 'no' in $DISTCONFIG" cleanup_and_exit 1 ;; [Ss][Kk][Ii][Pp]) cleanup_and_exit 0 ;; *) create_dir "$BACKUPDIR" ;; esac fi if [ -d "$BACKUPDIR/$DATE" ]; then if [ "$FORCE_BACKUP" == 'yes' ]; then LOG "$BACKUPDIR/$DATE already exists - cleaning up, as option --force is used" rm -rf "$BACKUPDIR/$DATE" create_dir "$BACKUPDIR/$DATE" else HANDLE_MESSAGE "$BACKUPDIR/$DATE exists" LOG "$BACKUPDIR/$DATE already exists - aborting backup" cleanup_and_exit 1 fi else create_dir "$BACKUPDIR/$DATE" fi pushd "$BACKUPDIR/$DATE" 1>/dev/null PG_DATADIR=$(get_datadir) for config in $CONFIG_FILES; do if [ -r "$PG_DATADIR/$config" ]; then LOG "Creating backup of $PG_DATADIR/$config" cp -L "$PG_DATADIR/$config" "$BACKUPDIR/$DATE/" fi done test_postmaster_alive "$TMPFILE" if [ -n "$PGSQL_SCRIPT_BEFORE_DUMP" ]; then LOG "Executing $PGSQL_SCRIPT_BEFORE_DUMP" "$PGSQL_SCRIPT_BEFORE_DUMP" fi ALL_DATABASES=$(su - postgres -c "$PGSQL $PGSQL_OPTS -A -P format=unaligned -t -c 'SELECT datname from pg_database;'") # If DBs are excluded for exclude in $EXCLUDE_DB; do ALL_DATABASES=$(echo $ALL_DATABASES | $SED "s/\b$exclude\b//g") done # start the real backup for db in $ALL_DATABASES; do if [ -s "$TMPFILE" ]; then HANDLE_MESSAGE "PGSQL: $(cat $TMPFILE)" 1 LOG "$(cat "$TMPFILE")" cleanup_and_exit 1 fi LOG "Creating backup of $db" if ! su - postgres -c "$PG_DUMP $PGSQL_OPTS --clean --if-exists --create --format=plain --quote-all-identifiers --dbname=$db" >> "$db.sql" then LOG "Dump of $db failed" if [ "$ABORT_ON_ERROR" = 'yes' ]; then cleanup_and_exit 1 fi FINAL_EXIT=1 fi if [ "$COMPRESS_AFTER_UNLOCK" != "yes" ]; then if ! compress_dump "$db.sql" then FINAL_EXIT=1 fi fi chmod 640 $db.sql* done if [ -n "$PGSQL_SCRIPT_AFTER_DUMP" ]; then LOG "Executing $PGSQL_SCRIPT_AFTER_DUMP" "$PGSQL_SCRIPT_AFTER_DUMP" fi for db in $ALL_DATABASES ; do if [ "$COMPRESS_AFTER_UNLOCK" = "yes" ]; then if ! compress_dump "$db.sql" then FINAL_EXIT=1 fi fi chmod 640 $db.sql* done if [ -s "$TMPFILE" ]; then HANDLE_MESSAGE "PGSQL: $(cat $TMPFILE)" 1 LOG "$(cat "$TMPFILE")" cleanup_and_exit 1 fi popd 1>/dev/null chmod 750 "$BACKUPDIR/$DATE" if [ -n "$PGSQL_SCRIPT_AFTER_COMPRESSION" ]; then LOG "Executing $PGSQL_SCRIPT_AFTER_COMPRESSION" "$PGSQL_SCRIPT_AFTER_COMPRESSION" fi if [ -n "$RETENTION" -a "$RETENTION" -gt 0 ]; then LOG "removing backups older than $RETENTION days" find "$BACKUPDIR" -ctime +$RETENTION -print0 | xargs -0 rm -rf {} | grep -v "No such file or directory" fi END_SECONDS=$(date +%s) RUNTIME_RAW_SECONDS=$[$END_SECONDS-$BEGIN_SECONDS] RUNTIME_HOURS=$[$RUNTIME_RAW_SECONDS/3600] RUNTIME_MINUTES=$[$[$RUNTIME_RAW_SECONDS%3600]/60] RUNTIME_SECONDS=$[$[$RUNTIME_RAW_SECONDS%3600]%60] LOG "backup finished. Duration (hh:mm:ss): $RUNTIME_HOURS:$RUNTIME_MINUTES:$RUNTIME_SECONDS" case $USE_NSCA in [Yy][Ee][Ss]) SEND_NSCA "Finished: $(date); Duration (hh:mm:ss): $RUNTIME_HOURS:$RUNTIME_MINUTES:$RUNTIME_SECONDS | time=$RUNTIME_RAW_SECONDS;" 0 ;; esac cleanup_and_exit "$FINAL_EXIT" ;; *) cleanup_and_exit 0 ;; esac cleanup_and_exit 0
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor