1 Answer

answered by

1. Adjust Permissions on /var/log/postgresql_backup.log

If you want to keep the log in /var/log/, you can grant the postgres user permission to write to this file:

su -
sudo touch /var/log/postgresql_backup.log
sudo chown postgres:postgres /var/log/postgresql_backup.log
sudo chmod 644 /var/log/postgresql_backup.log

This will create the log file, change its ownership to the postgres user, and allow it to write logs.

2. Fixing the Backup Directory Permission Error

Ensure that the postgres user has write permissions on the /mnt/backup/ directory:

sudo chown -R postgres:postgres /mnt/backup
sudo chmod 700 /mnt/backup

This will set the appropriate ownership and permissions so the postgres user can write to this directory.

3. Run Script

After fixing the permissions, re-run the script:

su - postgres

./backup.sh


backup script.

#!/bin/bash

# Define Variables
BACKUP_DIR="/mnt/backup"     # Directory where the backups will be stored
LOGFILE="/var/log/postgresql_backup.log"   # Log file location
PGUSER="postgres"                          # PostgreSQL user with backup privileges
PGPASSWORD="admin@123"                     # Password for PostgreSQL user
DATABASE="db_chirags"              	   # Name of the database you want to backup
RETENTION=7                                # Number of days to retain backups

# Date format for the backup file
DATE=$(date +"%Y-%m-%d_%H-%M-%S")

# Backup file name
BACKUP_FILE="${BACKUP_DIR}/${DATABASE}_${DATE}.sql"

# Function to write to log
log() {
    echo "$(date +"%Y-%m-%d %H:%M:%S") - $1" >> $LOGFILE
}

# Create Backup Directory if it doesn't exist
mkdir -p $BACKUP_DIR

# Backup Database
log "Starting backup for database: $DATABASE"
export PGPASSWORD=$PGPASSWORD
pg_dump -U $PGUSER -F c -b -v -f "$BACKUP_FILE" $DATABASE

# Check if the backup was successful
if [ $? -eq 0 ]; then
    log "Backup successful: $BACKUP_FILE"
    
    # Compress the backup file
    tar -czf "${BACKUP_FILE}.tar.gz" -C "$BACKUP_DIR" "$(basename $BACKUP_FILE)"
    rm -f "$BACKUP_FILE"
    log "Backup compressed to: ${BACKUP_FILE}.tar.gz"
else
    log "Backup failed for database: $DATABASE"
    exit 1
fi

# Remove old backups (older than retention period)
log "Cleaning up old backups..."
find $BACKUP_DIR -name "*.tar.gz" -mtime +$RETENTION -exec rm -f {} \;
log "Backup cleanup completed."

log "PostgreSQL backup completed for database: $DATABASE"

Most popular tags

laravel postgresql laravel-10 replication ha postgresql mongodb laravel-11 mongodb database mongodb tutorial ubuntu 24.04 lts streaming-replication mysql database laravel postgresql backup laravel login register logout database mysql php laravel 11 - login with otp valid for 10 minutes. user and admin registration user and admin login multiauth technlogy asp.net asp.net c# mysql master slave replication centos linux laravel sql server schedule backup autobackup postgresql django python haproxy load balancer install self sign ssl laravel 11 gaurds zabbix 7 how to install graylog on ubuntu 24.04 lts | step-by-step asp.net core mvc .net mvc network upload c# ssl integration sql server on ubuntu 22.04 lts mssql server ms sql server sql server user access in postgres mysql password change cent os linux configure replica laravel 11 socialite login with google account google login kubernetes (k8s) install nginx load balancer install install and configure .net 8.0 in ubuntu 24.04 lts php in iis php with iis php tutorial chirags php tutorials chirags php tutorial chirags tutorial laravel 11 guards mongodb sharding metabase business analytics metabase postgresql 16 to postgresql 17 postgresql migration letsencrypt mongodb crud rocky linux laravel custom captcha laravel 11 captcha laravel captcha mongo dll php.ini debian 12 nginx apache nextcloud gitea in ubuntu git gitea npm error node js mysql ndb cluster mysql cluster ssl oracle login register logout in python debian windows shell batch file bat file time stamp date time shopping cart in laravel centos rhel swap memeory rhel 5.5
...