From d1d11c46680ce1316231f0fc568728b60e203418 Mon Sep 17 00:00:00 2001 From: skobkin Date: Wed, 6 Nov 2024 17:55:49 +0000 Subject: [PATCH 01/17] Use .env file for backup configuration --- postgresql_backup/pg_backup.sh | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/postgresql_backup/pg_backup.sh b/postgresql_backup/pg_backup.sh index e77646c..2c55f29 100644 --- a/postgresql_backup/pg_backup.sh +++ b/postgresql_backup/pg_backup.sh @@ -1,14 +1,15 @@ #!/bin/bash -# Configuration -HOST="localhost" -PORT="5432" -USER="your_postgres_user" -BACKUP_PATH="/path/to/backup/directory" -DUMP_FORMAT="custom" # Set to "custom" for .dump or "sql" for plaintext SQL -EXTENSION="dump" # Default extension +# Load environment variables from .env file +if [ -f .env ]; then + export $(grep -v '^#' .env | xargs) +else + echo ".env file not found! Please create it with the necessary configuration." + exit 1 +fi -# Adjust file extension based on dump format +# Set file extension based on DUMP_FORMAT +EXTENSION="dump" # Default extension for custom format if [[ "$DUMP_FORMAT" == "sql" ]]; then EXTENSION="sql.gz" fi -- 2.43.5 From b26c680e52d20b6f509a246343cc2d843e1d137a Mon Sep 17 00:00:00 2001 From: skobkin Date: Wed, 6 Nov 2024 17:57:19 +0000 Subject: [PATCH 02/17] .env.dist added --- postgresql_backup/.env.dist | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 postgresql_backup/.env.dist diff --git a/postgresql_backup/.env.dist b/postgresql_backup/.env.dist new file mode 100644 index 0000000..351d4a6 --- /dev/null +++ b/postgresql_backup/.env.dist @@ -0,0 +1,6 @@ +# Database configuration +HOST="localhost" +PORT="5432" +USER="your_postgres_user" +BACKUP_PATH="/path/to/backup/directory" +DUMP_FORMAT="custom" # Use "custom" for .dump or "sql" for SQL compressed with Gzip \ No newline at end of file -- 2.43.5 From a2b01d06858334f733b6a75501301fe587fa4b6a Mon Sep 17 00:00:00 2001 From: skobkin Date: Wed, 6 Nov 2024 17:59:12 +0000 Subject: [PATCH 03/17] Updating README.md on .env configurations --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ac502d4..af66fec 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,13 @@ # scripts -Random scripts for different tasks \ No newline at end of file +Random scripts for different tasks + +## Configuration + +If the script provides any configuration, make a copy of `.env.dist` file to `.env` and edit configuration there. + +```shell +cd some_script +cp .env.dist .env +nano .env +``` \ No newline at end of file -- 2.43.5 From aed0c982a8639a301b852528ad5dfdef3a30d0a2 Mon Sep 17 00:00:00 2001 From: skobkin Date: Wed, 6 Nov 2024 18:31:04 +0000 Subject: [PATCH 04/17] postgresql_backup. Allow multiple servers. --- postgresql_backup/pg_backup.sh | 74 +++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 23 deletions(-) diff --git a/postgresql_backup/pg_backup.sh b/postgresql_backup/pg_backup.sh index 2c55f29..fae5843 100644 --- a/postgresql_backup/pg_backup.sh +++ b/postgresql_backup/pg_backup.sh @@ -8,32 +8,60 @@ else exit 1 fi -# Set file extension based on DUMP_FORMAT -EXTENSION="dump" # Default extension for custom format -if [[ "$DUMP_FORMAT" == "sql" ]]; then - EXTENSION="sql.gz" +# Ensure BACKUP_PATH is set +if [ -z "$BACKUP_PATH" ]; then + echo "BACKUP_PATH not set in .env file!" + exit 1 fi -# Create backup directory if it doesn't exist -mkdir -p "$BACKUP_PATH" +# Extract unique database prefixes (DB1, DB2, ...) +DATABASE_PREFIXES=$(env | grep -o 'DB[0-9]*_' | sort -u) -# Get a list of all databases, excluding template databases -DATABASES=$(psql -h "$HOST" -p "$PORT" -U "$USER" -d postgres -t -c "SELECT datname FROM pg_database WHERE datistemplate = false;") +# Loop through each database configuration prefix +for PREFIX in $DATABASE_PREFIXES; do + HOST_VAR="${PREFIX}HOST" + PORT_VAR="${PREFIX}PORT" + USER_VAR="${PREFIX}USER" + FORMAT_VAR="${PREFIX}DUMP_FORMAT" -# Loop over each database and create a backup -for DB in $DATABASES; do - BACKUP_FILE="$BACKUP_PATH/$DB.$EXTENSION" - - if [[ "$DUMP_FORMAT" == "custom" ]]; then - # Create a compressed custom-format backup - pg_dump -h "$HOST" -p "$PORT" -U "$USER" -Fc "$DB" -f "$BACKUP_FILE" - elif [[ "$DUMP_FORMAT" == "sql" ]]; then - # Create a plain-text SQL backup and compress it with gzip - pg_dump -h "$HOST" -p "$PORT" -U "$USER" "$DB" | gzip > "$BACKUP_FILE" - else - echo "Invalid DUMP_FORMAT specified: $DUMP_FORMAT" - exit 1 + HOST=${!HOST_VAR} + PORT=${!PORT_VAR} + USER=${!USER_VAR} + DUMP_FORMAT=${!FORMAT_VAR:-"custom"} # Default to "custom" if not set + + if [ -z "$HOST" ] || [ -z "$PORT" ] || [ -z "$USER" ]; then + echo "Incomplete configuration for $PREFIX. Skipping..." + continue fi - echo "Backup for database '$DB' created at '$BACKUP_FILE'" -done \ No newline at end of file + # Create a directory for each server using host and port + SERVER_DIR="$BACKUP_PATH/${HOST}_${PORT}" + mkdir -p "$SERVER_DIR" + + # Get a list of all databases for this server, excluding template databases + DATABASES=$(psql -h "$HOST" -p "$PORT" -U "$USER" -d postgres -t -c "SELECT datname FROM pg_database WHERE datistemplate = false;") + + # Loop over each database and create a backup + for DB_NAME in $DATABASES; do + # Set file extension based on DUMP_FORMAT + EXTENSION="dump" + if [[ "$DUMP_FORMAT" == "sql" ]]; then + EXTENSION="sql.gz" + fi + + # Set backup file path within the server-specific directory + BACKUP_FILE="$SERVER_DIR/$DB_NAME.$EXTENSION" + + # Perform the backup based on the specified format + if [[ "$DUMP_FORMAT" == "custom" ]]; then + pg_dump -h "$HOST" -p "$PORT" -U "$USER" -Fc "$DB_NAME" -f "$BACKUP_FILE" + elif [[ "$DUMP_FORMAT" == "sql" ]]; then + pg_dump -h "$HOST" -p "$PORT" -U "$USER" "$DB_NAME" | gzip > "$BACKUP_FILE" + else + echo "Invalid DUMP_FORMAT for $PREFIX: $DUMP_FORMAT. Skipping..." + continue + fi + + echo "Backup for database '$DB_NAME' created at '$BACKUP_FILE'" + done +done -- 2.43.5 From eee99952480af4386b890e38411c8d2fb4c6ffd8 Mon Sep 17 00:00:00 2001 From: skobkin Date: Wed, 6 Nov 2024 18:40:00 +0000 Subject: [PATCH 05/17] postgresql_backup. Using highest available level of compression. --- postgresql_backup/pg_backup.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/postgresql_backup/pg_backup.sh b/postgresql_backup/pg_backup.sh index fae5843..221c0c2 100644 --- a/postgresql_backup/pg_backup.sh +++ b/postgresql_backup/pg_backup.sh @@ -54,9 +54,11 @@ for PREFIX in $DATABASE_PREFIXES; do # Perform the backup based on the specified format if [[ "$DUMP_FORMAT" == "custom" ]]; then - pg_dump -h "$HOST" -p "$PORT" -U "$USER" -Fc "$DB_NAME" -f "$BACKUP_FILE" + # Custom format and highest ZSTD compression level + pg_dump -h "$HOST" -p "$PORT" -U "$USER" -Fc --compress="zstd:21" "$DB_NAME" -f "$BACKUP_FILE" elif [[ "$DUMP_FORMAT" == "sql" ]]; then - pg_dump -h "$HOST" -p "$PORT" -U "$USER" "$DB_NAME" | gzip > "$BACKUP_FILE" + # Plain-text SQL backup compressed with gzip at high compression level + pg_dump -h "$HOST" -p "$PORT" -U "$USER" "$DB_NAME" | gzip -9 > "$BACKUP_FILE" else echo "Invalid DUMP_FORMAT for $PREFIX: $DUMP_FORMAT. Skipping..." continue -- 2.43.5 From 641ba068984c9f4d40831dd061b834e94647c6b7 Mon Sep 17 00:00:00 2001 From: skobkin Date: Wed, 6 Nov 2024 18:42:03 +0000 Subject: [PATCH 06/17] Adding .gitignore for .env files --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..58322a0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*/.env.dist \ No newline at end of file -- 2.43.5 From 06961c72b50ee4a2ae1b11b6c0aa63566bf18b6f Mon Sep 17 00:00:00 2001 From: skobkin Date: Wed, 6 Nov 2024 18:45:55 +0000 Subject: [PATCH 07/17] .env.dist update --- postgresql_backup/.env.dist | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/postgresql_backup/.env.dist b/postgresql_backup/.env.dist index 351d4a6..5a76b30 100644 --- a/postgresql_backup/.env.dist +++ b/postgresql_backup/.env.dist @@ -1,6 +1,19 @@ -# Database configuration -HOST="localhost" -PORT="5432" -USER="your_postgres_user" +# Backup path BACKUP_PATH="/path/to/backup/directory" -DUMP_FORMAT="custom" # Use "custom" for .dump or "sql" for SQL compressed with Gzip \ No newline at end of file + +# Servers configuration +# Add as many as needed + +# Database 1 configuration +DB1_HOST="localhost" +DB1_PORT="5432" +DB1_USER="user1" +DB1_NAME="database1" +DB1_DUMP_FORMAT="custom" # "custom" for .dump or "sql" for SQL compressed with Gzip + +# Database 2 configuration +#DB2_HOST="remote_host" +#DB2_PORT="5432" +#DB2_USER="user2" +#DB2_NAME="database2" +#DB2_DUMP_FORMAT="sql" -- 2.43.5 From 66667dc10341b6bbc8e41841249b4a5e867fdb3a Mon Sep 17 00:00:00 2001 From: skobkin Date: Wed, 6 Nov 2024 18:51:38 +0000 Subject: [PATCH 08/17] postgresql_backup. Removing unnecessary variable from .env.dist --- postgresql_backup/.env.dist | 2 -- 1 file changed, 2 deletions(-) diff --git a/postgresql_backup/.env.dist b/postgresql_backup/.env.dist index 5a76b30..e5ec258 100644 --- a/postgresql_backup/.env.dist +++ b/postgresql_backup/.env.dist @@ -8,12 +8,10 @@ BACKUP_PATH="/path/to/backup/directory" DB1_HOST="localhost" DB1_PORT="5432" DB1_USER="user1" -DB1_NAME="database1" DB1_DUMP_FORMAT="custom" # "custom" for .dump or "sql" for SQL compressed with Gzip # Database 2 configuration #DB2_HOST="remote_host" #DB2_PORT="5432" #DB2_USER="user2" -#DB2_NAME="database2" #DB2_DUMP_FORMAT="sql" -- 2.43.5 From 89b50cc40f3dca8a396bd81f09587093591af510 Mon Sep 17 00:00:00 2001 From: skobkin Date: Wed, 6 Nov 2024 19:02:44 +0000 Subject: [PATCH 09/17] postgresql_backup. Adding README to mention .pgpass --- postgresql_backup/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 postgresql_backup/README.md diff --git a/postgresql_backup/README.md b/postgresql_backup/README.md new file mode 100644 index 0000000..e070ec2 --- /dev/null +++ b/postgresql_backup/README.md @@ -0,0 +1,17 @@ +# Usage + +## Authentication + +Do not forget to create and/or edit `~/.pgpass` file providing all needed passwords for each host: + +The format is the following: +```csv +hostname:port:database:username:password +``` + +Example + +```csv +localhost:5432:*:postgres:pg_password_1 +localhost:5433:*:postgres:pg_password_2 +``` \ No newline at end of file -- 2.43.5 From 6498a3ab1d1f9bd1b4302ce911db4f6790781399 Mon Sep 17 00:00:00 2001 From: skobkin Date: Wed, 6 Nov 2024 19:09:08 +0000 Subject: [PATCH 10/17] postgresql_backup. code highlight in README.md --- postgresql_backup/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/postgresql_backup/README.md b/postgresql_backup/README.md index e070ec2..ddd3497 100644 --- a/postgresql_backup/README.md +++ b/postgresql_backup/README.md @@ -5,13 +5,13 @@ Do not forget to create and/or edit `~/.pgpass` file providing all needed passwords for each host: The format is the following: -```csv +```properties hostname:port:database:username:password ``` Example -```csv +```properties localhost:5432:*:postgres:pg_password_1 localhost:5433:*:postgres:pg_password_2 ``` \ No newline at end of file -- 2.43.5 From e5731a3529ae39dace87c6d506a00728cfd65d2d Mon Sep 17 00:00:00 2001 From: skobkin Date: Wed, 6 Nov 2024 19:20:13 +0000 Subject: [PATCH 11/17] postgresql_backup. Progress output and smart directory creation. --- postgresql_backup/pg_backup.sh | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/postgresql_backup/pg_backup.sh b/postgresql_backup/pg_backup.sh index 221c0c2..2fdc607 100644 --- a/postgresql_backup/pg_backup.sh +++ b/postgresql_backup/pg_backup.sh @@ -34,15 +34,23 @@ for PREFIX in $DATABASE_PREFIXES; do continue fi - # Create a directory for each server using host and port + # Create a directory for each server using host and port, only if it doesn't exist SERVER_DIR="$BACKUP_PATH/${HOST}_${PORT}" - mkdir -p "$SERVER_DIR" + if [ ! -d "$SERVER_DIR" ]; then + echo "Creating backup directory: $SERVER_DIR" + mkdir -p "$SERVER_DIR" + else + echo "Backup directory $SERVER_DIR already exists, skipping creation." + fi # Get a list of all databases for this server, excluding template databases + echo "Fetching databases for $HOST:$PORT ..." DATABASES=$(psql -h "$HOST" -p "$PORT" -U "$USER" -d postgres -t -c "SELECT datname FROM pg_database WHERE datistemplate = false;") # Loop over each database and create a backup for DB_NAME in $DATABASES; do + echo "Starting backup for database '$DB_NAME' on $HOST:$PORT..." + # Set file extension based on DUMP_FORMAT EXTENSION="dump" if [[ "$DUMP_FORMAT" == "sql" ]]; then @@ -51,19 +59,22 @@ for PREFIX in $DATABASE_PREFIXES; do # Set backup file path within the server-specific directory BACKUP_FILE="$SERVER_DIR/$DB_NAME.$EXTENSION" + echo "Backup file: $BACKUP_FILE" # Perform the backup based on the specified format if [[ "$DUMP_FORMAT" == "custom" ]]; then - # Custom format and highest ZSTD compression level + echo "Performing custom format backup with zstd compression level 21..." pg_dump -h "$HOST" -p "$PORT" -U "$USER" -Fc --compress="zstd:21" "$DB_NAME" -f "$BACKUP_FILE" elif [[ "$DUMP_FORMAT" == "sql" ]]; then - # Plain-text SQL backup compressed with gzip at high compression level + echo "Performing SQL format backup with gzip compression..." pg_dump -h "$HOST" -p "$PORT" -U "$USER" "$DB_NAME" | gzip -9 > "$BACKUP_FILE" else echo "Invalid DUMP_FORMAT for $PREFIX: $DUMP_FORMAT. Skipping..." continue fi - echo "Backup for database '$DB_NAME' created at '$BACKUP_FILE'" + echo "Backup for database '$DB_NAME' on $HOST:$PORT completed successfully." done done + +echo "Backup process completed." -- 2.43.5 From f40839b9aea64022b3499d6b2a14df25b5595a1c Mon Sep 17 00:00:00 2001 From: skobkin Date: Wed, 6 Nov 2024 19:25:45 +0000 Subject: [PATCH 12/17] postgresql_backup. Moving comment to a new line in .env file for better import. --- postgresql_backup/.env.dist | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/postgresql_backup/.env.dist b/postgresql_backup/.env.dist index e5ec258..d7a1aef 100644 --- a/postgresql_backup/.env.dist +++ b/postgresql_backup/.env.dist @@ -8,7 +8,8 @@ BACKUP_PATH="/path/to/backup/directory" DB1_HOST="localhost" DB1_PORT="5432" DB1_USER="user1" -DB1_DUMP_FORMAT="custom" # "custom" for .dump or "sql" for SQL compressed with Gzip +# "custom" for .dump or "sql" for SQL compressed with Gzip +DB1_DUMP_FORMAT="custom" # Database 2 configuration #DB2_HOST="remote_host" -- 2.43.5 From 198285910af59b2f78a5ab361970836f6e2690f2 Mon Sep 17 00:00:00 2001 From: skobkin Date: Wed, 6 Nov 2024 19:32:00 +0000 Subject: [PATCH 13/17] postgresql_backup. Removing unnecessary output. --- postgresql_backup/pg_backup.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/postgresql_backup/pg_backup.sh b/postgresql_backup/pg_backup.sh index 2fdc607..5e6f668 100644 --- a/postgresql_backup/pg_backup.sh +++ b/postgresql_backup/pg_backup.sh @@ -63,10 +63,8 @@ for PREFIX in $DATABASE_PREFIXES; do # Perform the backup based on the specified format if [[ "$DUMP_FORMAT" == "custom" ]]; then - echo "Performing custom format backup with zstd compression level 21..." pg_dump -h "$HOST" -p "$PORT" -U "$USER" -Fc --compress="zstd:21" "$DB_NAME" -f "$BACKUP_FILE" elif [[ "$DUMP_FORMAT" == "sql" ]]; then - echo "Performing SQL format backup with gzip compression..." pg_dump -h "$HOST" -p "$PORT" -U "$USER" "$DB_NAME" | gzip -9 > "$BACKUP_FILE" else echo "Invalid DUMP_FORMAT for $PREFIX: $DUMP_FORMAT. Skipping..." -- 2.43.5 From a2f25f51a8a4a6092f1850132f091d741539e20b Mon Sep 17 00:00:00 2001 From: skobkin Date: Wed, 6 Nov 2024 19:46:33 +0000 Subject: [PATCH 14/17] postgresql_backup. Optimizing output. --- postgresql_backup/pg_backup.sh | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/postgresql_backup/pg_backup.sh b/postgresql_backup/pg_backup.sh index 5e6f668..05e8a48 100644 --- a/postgresql_backup/pg_backup.sh +++ b/postgresql_backup/pg_backup.sh @@ -49,8 +49,6 @@ for PREFIX in $DATABASE_PREFIXES; do # Loop over each database and create a backup for DB_NAME in $DATABASES; do - echo "Starting backup for database '$DB_NAME' on $HOST:$PORT..." - # Set file extension based on DUMP_FORMAT EXTENSION="dump" if [[ "$DUMP_FORMAT" == "sql" ]]; then @@ -59,7 +57,8 @@ for PREFIX in $DATABASE_PREFIXES; do # Set backup file path within the server-specific directory BACKUP_FILE="$SERVER_DIR/$DB_NAME.$EXTENSION" - echo "Backup file: $BACKUP_FILE" + + echo "Dumping '$DB_NAME' on $HOST:$PORT to $BACKUP_FILE" # Perform the backup based on the specified format if [[ "$DUMP_FORMAT" == "custom" ]]; then @@ -71,7 +70,7 @@ for PREFIX in $DATABASE_PREFIXES; do continue fi - echo "Backup for database '$DB_NAME' on $HOST:$PORT completed successfully." + du -sh $BACKUP_FILE done done -- 2.43.5 From 34c2a222a3dc3c729f388a7cdd9513626979c2ce Mon Sep 17 00:00:00 2001 From: skobkin Date: Wed, 6 Nov 2024 20:04:19 +0000 Subject: [PATCH 15/17] postgresql_backup. Configurable ZSTD and GZip levels. --- postgresql_backup/pg_backup.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/postgresql_backup/pg_backup.sh b/postgresql_backup/pg_backup.sh index 05e8a48..8c2e6d7 100644 --- a/postgresql_backup/pg_backup.sh +++ b/postgresql_backup/pg_backup.sh @@ -57,14 +57,14 @@ for PREFIX in $DATABASE_PREFIXES; do # Set backup file path within the server-specific directory BACKUP_FILE="$SERVER_DIR/$DB_NAME.$EXTENSION" - + echo "Dumping '$DB_NAME' on $HOST:$PORT to $BACKUP_FILE" # Perform the backup based on the specified format if [[ "$DUMP_FORMAT" == "custom" ]]; then - pg_dump -h "$HOST" -p "$PORT" -U "$USER" -Fc --compress="zstd:21" "$DB_NAME" -f "$BACKUP_FILE" + pg_dump -h "$HOST" -p "$PORT" -U "$USER" -Fc --compress="zstd:$ZSTD_LEVEL" "$DB_NAME" -f "$BACKUP_FILE" elif [[ "$DUMP_FORMAT" == "sql" ]]; then - pg_dump -h "$HOST" -p "$PORT" -U "$USER" "$DB_NAME" | gzip -9 > "$BACKUP_FILE" + pg_dump -h "$HOST" -p "$PORT" -U "$USER" "$DB_NAME" | gzip -$GZIP_LEVEL > "$BACKUP_FILE" else echo "Invalid DUMP_FORMAT for $PREFIX: $DUMP_FORMAT. Skipping..." continue -- 2.43.5 From f2dff8632f586b4cca18280aa787ac9ec1f88ec6 Mon Sep 17 00:00:00 2001 From: skobkin Date: Wed, 6 Nov 2024 20:05:21 +0000 Subject: [PATCH 16/17] postgresql_backup. Adding compression configuration to .env.dist --- postgresql_backup/.env.dist | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/postgresql_backup/.env.dist b/postgresql_backup/.env.dist index d7a1aef..1d170f8 100644 --- a/postgresql_backup/.env.dist +++ b/postgresql_backup/.env.dist @@ -1,6 +1,10 @@ # Backup path BACKUP_PATH="/path/to/backup/directory" +# Compression levels +ZSTD_LEVEL=5 +GZIP_LEVEL=7 + # Servers configuration # Add as many as needed -- 2.43.5 From c4ed18059252585ccdcd7aa21f911765f2f56c39 Mon Sep 17 00:00:00 2001 From: skobkin Date: Wed, 6 Nov 2024 20:33:37 +0000 Subject: [PATCH 17/17] postgresql_backup. Systemd configuration. --- postgresql_backup/README.md | 40 +++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/postgresql_backup/README.md b/postgresql_backup/README.md index ddd3497..72d4e66 100644 --- a/postgresql_backup/README.md +++ b/postgresql_backup/README.md @@ -14,4 +14,44 @@ Example ```properties localhost:5432:*:postgres:pg_password_1 localhost:5433:*:postgres:pg_password_2 +``` + +## Scheduling + +### Systemd + +#### Service + +`/etc/systemd/system/postgresql_backup.service` + +```ini +[Unit] +Description=PostgreSQL backup Script + +[Service] +Type=oneshot +WorkingDirectory=/root/scripts/postgresql_backup +ExecStart=/root/scripts/postgresql_backup/pg_backup.sh +``` + +#### Timer + +`/etc/systemd/system/postgresql_backup.timer` + +```ini +[Unit] +Description=Run Backup Script Every Night + +[Timer] +OnCalendar=*-*-* 22:00:00 +Unit=postgresql_backup.service + +[Install] +WantedBy=timers.target +``` + +Then: + +```shell +systemctl enable postgresql_backup.timer --now ``` \ No newline at end of file -- 2.43.5