2019-03-21 15:48:41 -04:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
|
2020-05-08 13:04:28 +00:00
|
|
|
# Capture the user's command line arguments
|
2019-03-21 15:48:41 -04:00
|
|
|
ARGS=("$@")
|
2019-04-23 13:36:07 -05:00
|
|
|
|
2020-05-08 13:04:28 +00:00
|
|
|
# This is needed to get the postgresql admin password
|
|
|
|
# Note: xtracing should be off so it doesn't print the pw
|
|
|
|
export PGPASSWORD=$(cat /etc/postgresql/admin_user.conf \
|
|
|
|
| grep postgres | awk -F: '{print $5}')
|
2019-04-23 13:36:07 -05:00
|
|
|
|
2020-05-08 13:04:28 +00:00
|
|
|
source /tmp/restore_main.sh
|
2019-04-23 13:36:07 -05:00
|
|
|
|
2020-05-08 13:04:28 +00:00
|
|
|
# Export the variables needed by the framework
|
|
|
|
export DB_NAME="postgres"
|
|
|
|
export DB_NAMESPACE=${POSTGRESQL_POD_NAMESPACE}
|
|
|
|
export ARCHIVE_DIR=${POSTGRESQL_BACKUP_BASE_DIR}/db/${DB_NAMESPACE}/${DB_NAME}/archive
|
2019-04-23 13:36:07 -05:00
|
|
|
|
2020-05-08 13:04:28 +00:00
|
|
|
# Define variables needed in this file
|
|
|
|
POSTGRESQL_HOST=$(cat /etc/postgresql/admin_user.conf | cut -d: -f 1)
|
|
|
|
export PSQL="psql -U $POSTGRESQL_ADMIN_USER -h $POSTGRESQL_HOST"
|
2019-03-21 15:48:41 -04:00
|
|
|
|
2020-05-08 13:04:28 +00:00
|
|
|
# Extract all databases from an archive and put them in the requested
|
|
|
|
# file.
|
2019-03-21 15:48:41 -04:00
|
|
|
get_databases() {
|
2020-05-08 13:04:28 +00:00
|
|
|
TMP_DIR=$1
|
|
|
|
DB_FILE=$2
|
2019-04-23 13:36:07 -05:00
|
|
|
|
|
|
|
SQL_FILE=postgres.$POSTGRESQL_POD_NAMESPACE.all.sql
|
2020-05-08 13:04:28 +00:00
|
|
|
if [[ -e $TMP_DIR/$SQL_FILE ]]; then
|
|
|
|
grep 'CREATE DATABASE' $TMP_DIR/$SQL_FILE | awk '{ print $3 }' > $DB_FILE
|
2019-03-21 15:48:41 -04:00
|
|
|
else
|
2020-05-08 13:04:28 +00:00
|
|
|
# no databases - just touch the file
|
|
|
|
touch $DB_FILE
|
2019-03-21 15:48:41 -04:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-05-08 13:04:28 +00:00
|
|
|
# Extract Single Database SQL Dump from pg_dumpall dump file
|
|
|
|
extract_single_db_dump() {
|
|
|
|
sed "/connect.*$2/,\$!d" $1 | sed "/PostgreSQL database dump complete/,\$d" > ${3}/$2.sql
|
2019-04-09 12:55:44 -04:00
|
|
|
}
|
|
|
|
|
2020-05-08 13:04:28 +00:00
|
|
|
# Restore a single database dump from pg_dumpall sql dumpfile.
|
2019-03-21 15:48:41 -04:00
|
|
|
restore_single_db() {
|
2019-04-23 13:36:07 -05:00
|
|
|
SINGLE_DB_NAME=$1
|
2020-05-08 13:04:28 +00:00
|
|
|
TMP_DIR=$2
|
2019-04-23 13:36:07 -05:00
|
|
|
|
|
|
|
SQL_FILE=postgres.$POSTGRESQL_POD_NAMESPACE.all.sql
|
2020-05-08 13:04:28 +00:00
|
|
|
if [[ -f $TMP_DIR/$SQL_FILE ]]; then
|
|
|
|
extract_single_db_dump $TMP_DIR/$SQL_FILE $SINGLE_DB_NAME $TMP_DIR
|
|
|
|
if [[ -f $TMP_DIR/$SINGLE_DB_NAME.sql && -s $TMP_DIR/$SINGLE_DB_NAME.sql ]]; then
|
|
|
|
# Postgresql does not have the concept of creating database if condition.
|
|
|
|
# This next command creates the database in case it does not exist.
|
|
|
|
$PSQL -tc "SELECT 1 FROM pg_database WHERE datname = '$SINGLE_DB_NAME'" | grep -q 1 || \
|
|
|
|
$PSQL -c "CREATE DATABASE $SINGLE_DB_NAME"
|
|
|
|
if [[ "$?" -ne 0 ]]; then
|
|
|
|
echo "Could not create the single database being restored: ${SINGLE_DB_NAME}."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
$PSQL -d $SINGLE_DB_NAME -f ${TMP_DIR}/${SINGLE_DB_NAME}.sql 2>>$LOG_FILE >> $LOG_FILE
|
|
|
|
if [[ "$?" -eq 0 ]]; then
|
|
|
|
echo "Database restore Successful."
|
2019-03-21 15:48:41 -04:00
|
|
|
else
|
2020-05-08 13:04:28 +00:00
|
|
|
echo "Database restore Failed."
|
|
|
|
return 1
|
2019-03-21 15:48:41 -04:00
|
|
|
fi
|
|
|
|
else
|
2020-05-08 13:04:28 +00:00
|
|
|
echo "Database dump For $SINGLE_DB_NAME is empty or not available."
|
|
|
|
return 1
|
2019-03-21 15:48:41 -04:00
|
|
|
fi
|
|
|
|
else
|
2020-05-08 13:04:28 +00:00
|
|
|
echo "No database file available to restore from."
|
|
|
|
return 1
|
2019-03-21 15:48:41 -04:00
|
|
|
fi
|
2020-05-08 13:04:28 +00:00
|
|
|
return 0
|
2019-03-21 15:48:41 -04:00
|
|
|
}
|
|
|
|
|
2020-05-08 13:04:28 +00:00
|
|
|
# Restore all the databases from the pg_dumpall sql file.
|
2019-03-21 15:48:41 -04:00
|
|
|
restore_all_dbs() {
|
2020-05-08 13:04:28 +00:00
|
|
|
TMP_DIR=$1
|
|
|
|
|
2019-04-23 13:36:07 -05:00
|
|
|
SQL_FILE=postgres.$POSTGRESQL_POD_NAMESPACE.all.sql
|
2020-05-08 13:04:28 +00:00
|
|
|
if [[ -f $TMP_DIR/$SQL_FILE ]]; then
|
|
|
|
$PSQL postgres -f $TMP_DIR/$SQL_FILE 2>>$LOG_FILE >> $LOG_FILE
|
|
|
|
if [[ "$?" -eq 0 ]]; then
|
2019-04-23 13:36:07 -05:00
|
|
|
echo "Database Restore successful."
|
2019-03-21 15:48:41 -04:00
|
|
|
else
|
2020-05-08 13:04:28 +00:00
|
|
|
echo "Database Restore failed."
|
|
|
|
return 1
|
2019-03-21 15:48:41 -04:00
|
|
|
fi
|
|
|
|
else
|
2020-05-08 13:04:28 +00:00
|
|
|
echo "There is no database file available to restore from."
|
|
|
|
return 1
|
2019-04-23 13:36:07 -05:00
|
|
|
fi
|
2020-05-08 13:04:28 +00:00
|
|
|
return 0
|
2019-03-21 15:48:41 -04:00
|
|
|
}
|
|
|
|
|
2020-05-08 13:04:28 +00:00
|
|
|
# Call the CLI interpreter, providing the archive directory path and the
|
|
|
|
# user arguments passed in
|
|
|
|
cli_main ${ARGS[@]}
|