Add first step of postgresql dump procedure
This commit is contained in:
parent
090ea867ed
commit
8a14457aef
6 changed files with 113 additions and 0 deletions
69
roles/backups/files/backup_script.sh
Normal file
69
roles/backups/files/backup_script.sh
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
IFS=$'\n\t'
|
||||
# Inspiration of script from: https://borgbackup.readthedocs.io/en/stable/quickstart.html#automating-backups
|
||||
|
||||
logInfo() { printf '%(%Y-%m-%d %H:%m:%S)T [INFO]: %s\n' -1 "$*" >&2; }
|
||||
|
||||
configurationFileLocation=$1
|
||||
|
||||
workDirPath=/root/backup_work_dir
|
||||
logInfo "Creating working directory at: $workDirPath..."
|
||||
mkdir -p "$workDirPath"
|
||||
|
||||
cleanup() {
|
||||
logInfo "Backup interrupted"
|
||||
# rm -f "$workDirPath" # TODO: Enable
|
||||
exit 2
|
||||
}
|
||||
trap 'cleanup' INT TERM
|
||||
|
||||
# region: ---------------------------------- DBBACKUPS ----------------------------------
|
||||
createDatabaseBackup() {
|
||||
host=$1
|
||||
dbname=$2
|
||||
username=$3
|
||||
password=$4
|
||||
targetFolderPath=$5
|
||||
|
||||
logInfo "Dumping database: $dbname to $targetFolderPath..."
|
||||
|
||||
# Getting the correct version tools installed on the host proofed to be a very frustrating experience.
|
||||
# So instead we'll do the dumping on the container.
|
||||
postgresContainerName='postgres-postgres-1'
|
||||
containerDumpPath=/dump.sql
|
||||
|
||||
logInfo "Dumping database $dbname on container: $postgresContainerName..."
|
||||
docker exec "$postgresContainerName" bash -c "(export PGPASSWORD='$password'; pg_dump $dbname \
|
||||
--file $containerDumpPath \
|
||||
--host $host \
|
||||
--username $username)"
|
||||
|
||||
logInfo "Extracting the archive from the container..."
|
||||
docker cp "$postgresContainerName:$containerDumpPath" "$targetFolderPath/$dbname.sql" 2>/dev/null
|
||||
|
||||
logInfo "Removing the file from the docker container..."
|
||||
docker exec "$postgresContainerName" rm "$containerDumpPath"
|
||||
}
|
||||
|
||||
createAllDatabaseBackups() {
|
||||
nrOfConfigurations="$(yq '.database_backups | length' <"$configurationFileLocation")"
|
||||
logInfo "Backing up from $nrOfConfigurations database configurations..."
|
||||
|
||||
postgresBackupDirectory="$workDirPath/postgres"
|
||||
mkdir -p "$postgresBackupDirectory"
|
||||
|
||||
for ((i = 0 ; i < "$nrOfConfigurations" ; i++)); do
|
||||
dbConfiguration="$(yq ".database_backups[$i]" <"$configurationFileLocation")"
|
||||
host="$(echo "$dbConfiguration" | jq -r '.host')"
|
||||
dbname="$(echo "$dbConfiguration" | jq -r '.dbname')"
|
||||
username="$(echo "$dbConfiguration" | jq -r '.username')"
|
||||
password="$(echo "$dbConfiguration" | jq -r '.password')"
|
||||
targetFolderPath="$postgresBackupDirectory"
|
||||
|
||||
createDatabaseBackup "$host" "$dbname" "$username" "$password" "$targetFolderPath"
|
||||
done
|
||||
}
|
||||
# endregion: ------------------------------- DBBACKUPS ----------------------------------
|
||||
|
||||
createAllDatabaseBackups
|
||||
Loading…
Add table
Add a link
Reference in a new issue