Is it possible to implement the functionality of this script using resticprofile? #430
-
I love resticprofile, and ideally would like to have all my backups controlled using just one mechanism. Here is a script I had to write because I couldn't figure out how to back up postgresql databases individually, dynamically, using resticprofile alone. The reason I break up the database backup files is that I have worked with them on restore before and a single file is almost completely unmanageable, as it's far too large to view/edit. I may in future break this script up further into schema and data. #!/usr/bin/zsh
if ! [[ $USER = "restic" ]]; then
echo Must be run as user 'restic'
exit 1
fi
# dump all databases into seperate files using the postgresql custom format
# that can be written by pg_dump and read by pg_restore - to stdin and to restic
EXT="pgdump" # pgdump custom extension (own)
PGDUMP=$(command -v pg_dump)
PGDUMPALL=$(command -v pg_dumpall)
PSQL=$(command -v psql)
RESTIC=$(command -v restic)
GET_PASSWORD="/usr/local/lib/resticprofile/get_restic_password.zsh"
COMPRESSION=none
REPO_BASE="rest:http://restic.example.com/postgresql_"
DB_REPO_GLOBALS=${REPO_BASE}globals
DB_REPO_DATABASES=${REPO_BASE}databases
export RESTIC_REST_USERNAME=admin
export RESTIC_REST_PASSWORD=admin
# create a directory called dump in the working directory
echo "Backing up globals..."
export RESTIC_REPOSITORY=$DB_REPO_GLOBALS
export RESTIC_PASSWORD=$(${GET_PASSWORD} postgresql_globals)
$PGDUMPALL --globals-only | $RESTIC backup --stdin-filename "globals.sql" --stdin --verbose
# get a list of all databases from psql
echo "Backing up databases..."
DBLIST=$($PSQL -l | command sed -E -e '/^( |-|\().+$/d' -e '/^$/d' -e '/^\s+template(0|1).+$/d' | command awk '{ print $1; }')
export RESTIC_REPOSITORY=$DB_REPO_DATABASES
export RESTIC_PASSWORD=$(${GET_PASSWORD} postgresql_databases)
while IFS=$'\n' read -n DB; do
echo "Backing up '$DB'...";
$PGDUMP -d "$DB" --format=custom --compress=$COMPRESSION | $RESTIC backup --stdin-filename "$DB.dump" --stdin --verbose
echo $DB
done <<< $DBLIST
echo "Done"
N.B. Compression is set to none because it allows restic to handle deduplication and compression most effectively, and also reduces load on the server (win/win!). |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Roughly yes. You need a profile per DB dump. I think I should put it in the documentation 👍🏻 |
Beta Was this translation helpful? Give feedback.
Roughly yes. You need a profile per DB dump.
You can use a trick to make dynamic configuration profiles, explained in this discussion:
#414 (reply in thread)
I think I should put it in the documentation 👍🏻