Browse Source

Sync with a closer release of automysqlbackup (Debian patch 01_sync_latest_automysqlbackup)

Emmanuel Bouthenot 5 years ago
parent
commit
6bb8595862
1 changed files with 130 additions and 38 deletions
  1. 130 38
      autopostgresqlbackup

+ 130 - 38
autopostgresqlbackup

@@ -45,21 +45,21 @@ DBHOST=localhost
 DBNAMES="all"
 
 # Backup directory location e.g /backups
-BACKUPDIR="/home/backups/pgsql_backups"
+BACKUPDIR="/backups"
 
 # Mail setup
 # What would you like to be mailed to you?
 # - log   : send only log file
 # - files : send log file and sql files as attachments (see docs)
 # - stdout : will simply output the log to the screen if run manually.
-MAILCONTENT="log"
+# - quiet : Only send logs if an error occurs to the MAILADDR.
+MAILCONTENT="stdout"
 
 # Set the maximum allowed email size in k. (4000 = approx 5MB email [see docs])
 MAXATTSIZE="4000"
 
 # Email Address to send mail to? (user@domain.com)
-MAILADDR="root@localhost"
-
+MAILADDR="user@domain.com"
 
 # ============================================================
 # === ADVANCED OPTIONS ( Read the doc's below for details )===
@@ -83,12 +83,18 @@ DOWEEKLY=6
 # Choose Compression type. (gzip or bzip2)
 COMP=gzip
 
+# Compress communications between backup server and PostgreSQL server?
+# set compression level from 0 to 9 (0 means no compression)
+COMMCOMP=0
+
+# Additionally keep a copy of the most recent backup in a seperate directory.
+LATEST=no
+
 # Command to run before backups (uncomment to use)
-#PREBACKUP="/etc/pgsql-backup-pre"
+#PREBACKUP="/etc/postgresql-backup-pre"
 
 # Command run after backups (uncomment to use)
-POSTBACKUP="bash /home/backups/scripts/ftp_pgsql"
-
+#POSTBACKUP="/etc/postgresql-backup-post"
 
 #=====================================================================
 # Options documentation
@@ -117,21 +123,22 @@ POSTBACKUP="bash /home/backups/scripts/ftp_pgsql"
 # email addresses in a space seperated list.
 # (If you set mail content to "log" you will require access to the "mail" program
 # on your server. If you set this to "files" you will have to have mutt installed
-# on your server. If you set it sto stdout it will log to the screen if run from 
-# the console or to the cron job owner if run through cron)
+# on your server. If you set it to "stdout" it will log to the screen if run from
+# the console or to the cron job owner if run through cron. If you set it to "quiet"
+# logs will only be mailed if there are errors reported. )
 #
 # MAXATTSIZE sets the largest allowed email attachments total (all backup files) you
 # want the script to send. This is the size before it is encoded to be sent as an email
 # so if your mail server will allow a maximum mail size of 5MB I would suggest setting
 # MAXATTSIZE to be 25% smaller than that so a setting of 4000 would probably be fine.
 #
-# Finally copy automysqlbackup.sh to anywhere on your server and make sure
+# Finally copy autopostgresqlbackup.sh to anywhere on your server and make sure
 # to set executable permission. You can also copy the script to
 # /etc/cron.daily to have it execute automatically every night or simply
 # place a symlink in /etc/cron.daily to the file if you wish to keep it 
 # somwhere else.
 # NOTE:On Debian copy the file with no extention for it to be run
-# by cron e.g just name the file "automysqlbackup"
+# by cron e.g just name the file "autopostgresqlbackup"
 #
 # Thats it..
 #
@@ -139,10 +146,8 @@ POSTBACKUP="bash /home/backups/scripts/ftp_pgsql"
 # === Advanced options doc's ===
 #
 # The list of MDBNAMES is the DB's to be backed up only monthly. You should
-# always include "mysql" in this list to backup your user/password
-# information along with any other DBs that you only feel need to
-# be backed up monthly. (if using a hosted server then you should
-# probably remove "mysql" as your provider will be backing this up)
+# always include "template1" in this list to backup the default database
+# template used to create new databases.
 # NOTE: If DBNAMES="all" then MDBNAMES has no effect as all DBs will be backed
 # up anyway.
 #
@@ -155,7 +160,7 @@ POSTBACKUP="bash /home/backups/scripts/ftp_pgsql"
 # a database with the same name as the original database when restoring.
 # Saying "no" here will allow your to specify the database name you want to
 # restore your dump into, making a copy of the database by using the dump
-# created with automysqlbackup.
+# created with autopostgresqlbackup.
 # NOTE: Not used if SEPDIR=no
 #
 # The SEPDIR option allows you to choose to have all DBs backed up to
@@ -171,6 +176,13 @@ POSTBACKUP="bash /home/backups/scripts/ftp_pgsql"
 # bzip2 will produce slightly smaller files but is more processor intensive so
 # may take longer to complete.
 #
+# COMMCOMP is used to set the compression level (from 0 to 9, 0 means no compression)
+# between the client and the server, so it is useful to save bandwidth when backing up
+# a remote PostgresSQL server over the network.
+#
+# LATEST is to store an additional copy of the latest backup to a standard
+# location so it can be downloaded bt thrid party scripts.
+#
 # Use PREBACKUP and POSTBACKUP to specify Per and Post backup commands
 # or scripts to perform tasks either before or after the backup process.
 #
@@ -201,6 +213,23 @@ POSTBACKUP="bash /home/backups/scripts/ftp_pgsql"
 # Happy backing up...
 #
 #=====================================================================
+# Restoring
+#=====================================================================
+# Firstly you will need to uncompress the backup file.
+# eg.
+# gunzip file.gz (or bunzip2 file.bz2)
+#
+# Next you will need to use the postgresql client to restore the DB from the
+# sql file.
+# eg.
+# psql --host dbserver --dbname database < /path/file.sql
+#
+# NOTE: Make sure you use "<" and not ">" in the above command because
+# you are piping the file.sql to psql and not the other way around.
+#
+# Lets hope you never have to use this.. :)
+#
+#=====================================================================
 # Change Log
 #=====================================================================
 #
@@ -209,22 +238,31 @@ POSTBACKUP="bash /home/backups/scripts/ftp_pgsql"
 #
 #=====================================================================
 #=====================================================================
+#=====================================================================
 #
 # Should not need to be modified from here down!!
 #
 #=====================================================================
 #=====================================================================
+#=====================================================================
 PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/postgres/bin:/usr/local/pgsql/bin
-DATE=`date +%Y-%m-%d`				# Datestamp e.g 2002-09-21
+DATE=`date +%Y-%m-%d_%Hh%Mm`	# Datestamp e.g 2002-09-21
 DOW=`date +%A`					# Day of the week e.g. Monday
 DNOW=`date +%u`					# Day number of the week 1 to 7 where 1 represents Monday
 DOM=`date +%d`					# Date of the Month e.g. 27
 M=`date +%B`					# Month e.g January
 W=`date +%V`					# Week Number e.g 37
-VER=1.0						# Version Number
-LOGFILE=$BACKUPDIR/$DBHOST-`date +%N`.log	# Logfile Name
-OPT=""						# OPT string for use with mysqldump ( see man mysqldump )
-BACKUPFILES=""					# thh: added for later mailing
+VER=1.0							# Version Number
+LOGFILE=$BACKUPDIR/$DBHOST-`date +%N`.log			# Logfile Name
+LOGERR=$BACKUPDIR/ERRORS_$DBHOST-`date +%N`.log		# Logfile Name
+BACKUPFILES=""
+OPT=""						# OPT string for use with pg_dump ( see man pg_dump )
+
+# Add --compress pg_dump option to $OPT
+if [ "$COMMCOMP" -gt 0 ];
+	then
+	OPT="$OPT --compress=$COMMCOMP"
+fi
 
 # Create required directories
 if [ ! -e "$BACKUPDIR" ]		# Check Backup Directory exists.
@@ -247,23 +285,37 @@ if [ ! -e "$BACKUPDIR/monthly" ]	# Check Monthly Directory exists.
 	mkdir -p "$BACKUPDIR/monthly"
 fi
 
+if [ "$LATEST" = "yes" ]
+then
+	if [ ! -e "$BACKUPDIR/latest" ]	# Check Latest Directory exists.
+	then
+		mkdir -p "$BACKUPDIR/latest"
+	fi
+eval rm -f "$BACKUPDIR/latest/*"
+fi
 
 # IO redirection for logging.
 touch $LOGFILE
 exec 6>&1           # Link file descriptor #6 with stdout.
                     # Saves stdout.
 exec > $LOGFILE     # stdout replaced with file $LOGFILE.
+touch $LOGERR
+exec 7>&2           # Link file descriptor #7 with stderr.
+                    # Saves stderr.
+exec 2> $LOGERR     # stderr replaced with file $LOGERR.
 
 
 # Functions
 
 # Database dump function
 dbdump () {
-pg_dump --username=$USERNAME $HOST $OPT $1 > $2
-return 0
+	touch $2
+	chmod 600 $2
+	pg_dump --username=$USERNAME $PGHOST $OPT $1 > $2
+	return 0
 }
 
-# Compression function
+# Compression function plus latest copy
 SUFFIX=""
 compression () {
 if [ "$COMP" = "gzip" ]; then
@@ -279,6 +331,9 @@ elif [ "$COMP" = "bzip2" ]; then
 else
 	echo "No compression option set, check advanced settings"
 fi
+if [ "$LATEST" = "yes" ]; then
+	cp $1$SUFFIX "$BACKUPDIR/latest/"
+fi
 return 0
 }
 
@@ -308,15 +363,16 @@ fi
 
 # Hostname for LOG information
 if [ "$DBHOST" = "localhost" ]; then
-	DBHOST="`hostname -f`"
-	HOST=""
+	HOST=`hostname`
+	PGHOST=""
 else
-	HOST="-h $DBHOST"
+	HOST=$DBHOST
+	PGHOST="-h $DBHOST"
 fi
 
 # If backing up all DBs on the server
 if [ "$DBNAMES" = "all" ]; then
-	DBNAMES="`psql -U $USERNAME $HOST -l -A -F: | sed -ne "/:/ { /Name:Owner/d; /template0/d; s/:.*$//; p }"`"
+	DBNAMES="`psql -U $USERNAME $PGHOST -l -A -F: | sed -ne "/:/ { /Name:Owner/d; /template0/d; s/:.*$//; p }"`"
 	
 	# If DBs are excluded
 	for exclude in $DBEXCLUDE
@@ -331,7 +387,7 @@ echo ======================================================================
 echo AutoPostgreSQLBackup VER $VER
 echo http://autopgsqlbackup.frozenpc.net/
 echo 
-echo Backup of Database Server - $DBHOST
+echo Backup of Database Server - $HOST
 echo ======================================================================
 
 # Test is seperate DB backups are required
@@ -339,7 +395,7 @@ if [ "$SEPDIR" = "yes" ]; then
 echo Backup Start Time `date`
 echo ======================================================================
 	# Monthly Full Backup of all Databases
-	if [ $DOM = "01" ]; then
+	if [ "$DOM" = "01" ]; then
 		for MDB in $MDBNAMES
 		do
  
@@ -375,7 +431,7 @@ echo ======================================================================
 	fi
 	
 	# Weekly Backup
-	if [ $DNOW = $DOWEEKLY ]; then
+	if [ "$DNOW" = "$DOWEEKLY" ]; then
 		echo Weekly Backup of Database \( $DB \)
 		echo Rotating 5 weeks Backups...
 			if [ "$W" -le 05 ];then
@@ -385,7 +441,7 @@ echo ======================================================================
 			else
 				REMW=`expr $W - 5`
 			fi
-		eval rm -fv "$BACKUPDIR/weekly/$DB/week.$REMW.*" 
+		eval rm -fv "$BACKUPDIR/weekly/$DB/${DB}_week.$REMW.*"
 		echo
 			dbdump "$DB" "$BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql"
 			compression "$BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql"
@@ -412,7 +468,7 @@ else # One backup file for all DBs
 echo Backup Start `date`
 echo ======================================================================
 	# Monthly Full Backup of all Databases
-	if [ $DOM = "01" ]; then
+	if [ "$DOM" = "01" ]; then
 		echo Monthly full Backup of \( $MDBNAMES \)...
 			dbdump "$MDBNAMES" "$BACKUPDIR/monthly/$DATE.$M.all-databases.sql"
 			compression "$BACKUPDIR/monthly/$DATE.$M.all-databases.sql"
@@ -421,7 +477,7 @@ echo ======================================================================
 	fi
 
 	# Weekly Backup
-	if [ $DNOW = $DOWEEKLY ]; then
+	if [ "$DNOW" = "$DOWEEKLY" ]; then
 		echo Weekly Backup of Databases \( $DBNAMES \)
 		echo
 		echo Rotating 5 weeks Backups...
@@ -473,26 +529,62 @@ fi
 
 #Clean up IO redirection
 exec 1>&6 6>&-      # Restore stdout and close file descriptor #6.
+exec 1>&7 7>&-      # Restore stdout and close file descriptor #7.
 
 if [ "$MAILCONTENT" = "files" ]
 then
+	if [ -s "$LOGERR" ]
+	then
+		# Include error log if is larger than zero.
+		BACKUPFILES="$BACKUPFILES $LOGERR"
+		ERRORNOTE="WARNING: Error Reported - "
+	fi
 	#Get backup size
 	ATTSIZE=`du -c $BACKUPFILES | grep "[[:digit:][:space:]]total$" |sed s/\s*total//`
 	if [ $MAXATTSIZE -ge $ATTSIZE ]
 	then
 		BACKUPFILES=`echo "$BACKUPFILES" | sed -e "s# # -a #g"`	#enable multiple attachments
-		mutt -s "PostgreSQL Backup Log and SQL Files for $DBHOST - $DATE" $BACKUPFILES $MAILADDR < $LOGFILE		#send via mutt
+		mutt -s "PostgreSQL Backup Log and SQL Files for $HOST - $DATE" $BACKUPFILES $MAILADDR < $LOGFILE		#send via mutt
 	else
 		cat "$LOGFILE" | mail -s "WARNING! - PostgreSQL Backup exceeds set maximum attachment size on $HOST - $DATE" $MAILADDR
 	fi
 elif [ "$MAILCONTENT" = "log" ]
 then
-	cat "$LOGFILE" | mail -s "PostgreSQL Backup Log for $DBHOST - $DATE" $MAILADDR
+	cat "$LOGFILE" | mail -s "PostgreSQL Backup Log for $HOST - $DATE" $MAILADDR
+	if [ -s "$LOGERR" ]
+		then
+			cat "$LOGERR" | mail -s "ERRORS REPORTED: PostgreSQL Backup error Log for $HOST - $DATE" $MAILADDR
+	fi
+elif [ "$MAILCONTENT" = "quiet" ]
+then
+	if [ -s "$LOGERR" ]
+		then
+			cat "$LOGERR" | mail -s "ERRORS REPORTED: PostgreSQL Backup error Log for $HOST - $DATE" $MAILADDR
+			cat "$LOGFILE" | mail -s "PostgreSQL Backup Log for $HOST - $DATE" $MAILADDR
+	fi
 else
-	cat "$LOGFILE"
+	if [ -s "$LOGERR" ]
+		then
+			cat "$LOGFILE"
+			echo
+			echo "###### WARNING ######"
+			echo "Errors reported during AutoPostgreSQLBackup execution.. Backup failed"
+			echo "Error log below.."
+			cat "$LOGERR"
+	else
+		cat "$LOGFILE"
+	fi
+fi
+
+if [ -s "$LOGERR" ]
+	then
+		STATUS=1
+	else
+		STATUS=0
 fi
 
 # Clean up Logfile
 eval rm -f "$LOGFILE"
+eval rm -f "$LOGERR"
 
-exit 0
+exit $STATUS