-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit.
- Loading branch information
Showing
3 changed files
with
541 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#!/usr/bin/env bash | ||
|
||
|
||
## Author: Tommy Miland (@tmiland) - Copyright (c) 2019 | ||
|
||
###################################################################### | ||
## Source: https://wiki.postgresql.org/wiki/Automated_Backup_on_Linux | ||
###################################################################### | ||
#### Pg Backup.sh #### | ||
#### Automated PostgreSQL Backup on Linux #### | ||
#### Maintained by @tmiland #### | ||
###################################################################### | ||
|
||
version='1.0.0' | ||
|
||
#------------------------------------------------------------------------------# | ||
# | ||
# MIT License | ||
# | ||
# Copyright (c) 2019 Tommy Miland | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
# | ||
#------------------------------------------------------------------------------# | ||
############################## | ||
## POSTGRESQL BACKUP CONFIG ## | ||
############################## | ||
# Icons used for printing | ||
ARROW='➜' | ||
DONE='✔' | ||
ERROR='✗' | ||
WARNING='⚠' | ||
# Colors used for printing | ||
RED='\033[0;31m' | ||
BLUE='\033[0;34m' | ||
BBLUE='\033[1;34m' | ||
GREEN='\033[0;32m' | ||
ORANGE='\033[0;33m' | ||
DARKORANGE="\033[38;5;208m" | ||
CYAN='\033[0;36m' | ||
DARKGREY="\033[48;5;236m" | ||
NC='\033[0m' # No Color | ||
# Text formatting used for printing | ||
BOLD="\033[1m" | ||
DIM="\033[2m" | ||
UNDERLINED="\033[4m" | ||
INVERT="\033[7m" | ||
HIDDEN="\033[8m" | ||
# Optional system user to run backups as. If the user the script is running as doesn't match this | ||
# the script terminates. Leave blank to skip check. | ||
BACKUP_USER=root | ||
|
||
# Optional hostname to adhere to pg_hba policies. Will default to "localhost" if none specified. | ||
HOSTNAME=localhost | ||
|
||
# Optional username to connect to database as. Will default to "postgres" if none specified. | ||
USERNAME=postgres | ||
|
||
# This dir will be created if it doesn't exist. This must be writable by the user the script is | ||
# running as. | ||
BACKUP_DIR=/home/backup/database/postgresql/ | ||
|
||
# List of strings to match against in database name, separated by space or comma, for which we only | ||
# wish to keep a backup of the schema, not the data. Any database names which contain any of these | ||
# values will be considered candidates. (e.g. "system_log" will match "dev_system_log_2010-01") | ||
SCHEMA_ONLY_LIST="" | ||
|
||
# Will produce a custom-format backup if set to "yes" | ||
ENABLE_CUSTOM_BACKUPS=yes | ||
|
||
# Will produce a gzipped plain-format backup if set to "yes" | ||
ENABLE_PLAIN_BACKUPS=yes | ||
|
||
# Will produce gzipped sql file containing the cluster globals, like users and passwords, if set to "yes" | ||
ENABLE_GLOBALS_BACKUPS=yes | ||
|
||
|
||
#### SETTINGS FOR ROTATED BACKUPS #### | ||
|
||
# Which day to take the weekly backup from (1-7 = Monday-Sunday) | ||
DAY_OF_WEEK_TO_KEEP=5 | ||
|
||
# Number of days to keep daily backups | ||
DAYS_TO_KEEP=7 | ||
|
||
# How many weeks to keep weekly backups | ||
WEEKS_TO_KEEP=5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
#!/usr/bin/env bash | ||
|
||
|
||
## Author: Tommy Miland (@tmiland) - Copyright (c) 2019 | ||
|
||
###################################################################### | ||
## Source: https://wiki.postgresql.org/wiki/Automated_Backup_on_Linux | ||
###################################################################### | ||
#### Pg Backup.sh #### | ||
#### Automated PostgreSQL Backup on Linux #### | ||
#### Maintained by @tmiland #### | ||
###################################################################### | ||
|
||
version=$version | ||
|
||
#------------------------------------------------------------------------------# | ||
# | ||
# MIT License | ||
# | ||
# Copyright (c) 2019 Tommy Miland | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
# | ||
#------------------------------------------------------------------------------# | ||
########################### | ||
####### LOAD CONFIG ####### | ||
########################### | ||
|
||
while [ $# -gt 0 ]; do | ||
case $1 in | ||
-c) | ||
if [ -r "$2" ]; then | ||
source "$2" | ||
shift 2 | ||
else | ||
${ECHO} "Unreadable config file \"$2\"" 1>&2 | ||
exit 1 | ||
fi | ||
;; | ||
*) | ||
${ECHO} "Unknown Option \"$1\"" 1>&2 | ||
exit 2 | ||
;; | ||
esac | ||
done | ||
|
||
if [ $# = 0 ]; then | ||
SCRIPTPATH=$(cd ${0%/*} && pwd -P) | ||
source $SCRIPTPATH/pg_backup.conf | ||
fi; | ||
|
||
########################### | ||
#### PRE-BACKUP CHECKS #### | ||
########################### | ||
|
||
# Make sure we're running as the required backup user | ||
if [ "$BACKUP_USER" != "" -a "$(id -un)" != "$BACKUP_USER" ]; then | ||
echo "This script must be run as $BACKUP_USER. Exiting." 1>&2 | ||
exit 1; | ||
fi; | ||
|
||
|
||
########################### | ||
### INITIALISE DEFAULTS ### | ||
########################### | ||
|
||
if [ ! $HOSTNAME ]; then | ||
HOSTNAME="localhost" | ||
fi; | ||
|
||
if [ ! $USERNAME ]; then | ||
USERNAME="postgres" | ||
fi; | ||
|
||
|
||
########################### | ||
#### START THE BACKUPS #### | ||
########################### | ||
|
||
|
||
FINAL_BACKUP_DIR=$BACKUP_DIR"`date +\%Y-\%m-\%d`/" | ||
|
||
echo "Making backup directory in $FINAL_BACKUP_DIR" | ||
|
||
if ! mkdir -p $FINAL_BACKUP_DIR; then | ||
echo "Cannot create backup directory in $FINAL_BACKUP_DIR. Go and fix it!" 1>&2 | ||
exit 1; | ||
fi; | ||
|
||
|
||
####################### | ||
### GLOBALS BACKUPS ### | ||
####################### | ||
|
||
echo -e "\n\nPerforming globals backup" | ||
echo -e "--------------------------------------------\n" | ||
|
||
if [ $ENABLE_GLOBALS_BACKUPS = "yes" ] | ||
then | ||
echo "Globals backup" | ||
|
||
if ! pg_dumpall -g -U "$USERNAME" | gzip > $FINAL_BACKUP_DIR"globals".sql.gz.in_progress; then | ||
echo "[!!ERROR!!] Failed to produce globals backup" 1>&2 | ||
else | ||
mv $FINAL_BACKUP_DIR"globals".sql.gz.in_progress $FINAL_BACKUP_DIR"globals".sql.gz | ||
fi | ||
else | ||
echo "None" | ||
fi | ||
|
||
|
||
########################### | ||
### SCHEMA-ONLY BACKUPS ### | ||
########################### | ||
|
||
for SCHEMA_ONLY_DB in ${SCHEMA_ONLY_LIST//,/ } | ||
do | ||
SCHEMA_ONLY_CLAUSE="$SCHEMA_ONLY_CLAUSE or datname ~ '$SCHEMA_ONLY_DB'" | ||
done | ||
|
||
SCHEMA_ONLY_QUERY="select datname from pg_database where false $SCHEMA_ONLY_CLAUSE order by datname;" | ||
|
||
echo -e "\n\nPerforming schema-only backups" | ||
echo -e "--------------------------------------------\n" | ||
|
||
SCHEMA_ONLY_DB_LIST=`psql -U "$USERNAME" -At -c "$SCHEMA_ONLY_QUERY" postgres` | ||
|
||
echo -e "The following databases were matched for schema-only backup:\n${SCHEMA_ONLY_DB_LIST}\n" | ||
|
||
for DATABASE in $SCHEMA_ONLY_DB_LIST | ||
do | ||
echo "Schema-only backup of $DATABASE" | ||
|
||
if ! pg_dump -Fp -s -U "$USERNAME" "$DATABASE" | gzip > $FINAL_BACKUP_DIR"$DATABASE"_SCHEMA.sql.gz.in_progress; then | ||
echo "[!!ERROR!!] Failed to backup database schema of $DATABASE" 1>&2 | ||
else | ||
mv $FINAL_BACKUP_DIR"$DATABASE"_SCHEMA.sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE"_SCHEMA.sql.gz | ||
fi | ||
done | ||
|
||
|
||
########################### | ||
###### FULL BACKUPS ####### | ||
########################### | ||
|
||
for SCHEMA_ONLY_DB in ${SCHEMA_ONLY_LIST//,/ } | ||
do | ||
EXCLUDE_SCHEMA_ONLY_CLAUSE="$EXCLUDE_SCHEMA_ONLY_CLAUSE and datname !~ '$SCHEMA_ONLY_DB'" | ||
done | ||
|
||
FULL_BACKUP_QUERY="select datname from pg_database where not datistemplate and datallowconn $EXCLUDE_SCHEMA_ONLY_CLAUSE order by datname;" | ||
|
||
echo -e "\n\nPerforming full backups" | ||
echo -e "--------------------------------------------\n" | ||
|
||
for DATABASE in `psql -U "$USERNAME" -At -c "$FULL_BACKUP_QUERY" postgres` | ||
do | ||
if [ $ENABLE_PLAIN_BACKUPS = "yes" ] | ||
then | ||
echo "Plain backup of $DATABASE" | ||
|
||
if ! pg_dump -Fp -U "$USERNAME" "$DATABASE" | gzip > $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress; then | ||
echo "[!!ERROR!!] Failed to produce plain backup database $DATABASE" 1>&2 | ||
else | ||
mv $FINAL_BACKUP_DIR"$DATABASE".sql.gz.in_progress $FINAL_BACKUP_DIR"$DATABASE".sql.gz | ||
fi | ||
fi | ||
|
||
if [ $ENABLE_CUSTOM_BACKUPS = "yes" ] | ||
then | ||
echo "Custom backup of $DATABASE" | ||
|
||
if ! pg_dump -Fc -U "$USERNAME" "$DATABASE" -f $FINAL_BACKUP_DIR"$DATABASE".custom.in_progress; then | ||
echo "[!!ERROR!!] Failed to produce custom backup database $DATABASE" 1>&2 | ||
else | ||
mv $FINAL_BACKUP_DIR"$DATABASE".custom.in_progress $FINAL_BACKUP_DIR"$DATABASE".custom | ||
fi | ||
fi | ||
|
||
done | ||
|
||
echo -e "\nAll database backups complete!" | ||
|
||
$SCRIPTPATH/pg_backup_rotated.sh |
Oops, something went wrong.