PGSQL scheduled remote full database backup strategy

PGSQL scheduled remote full database backup strategy

1. Prepare the container

Prepare a postgres container image to connect to the remote pg library

image-20230331113350649

Sample startup script

docker run -d --restart always --name postgres -p 5432:5432 -v /etc/localtime:/etc/localtime:ro -e POSTGRES_PASSWORD=123456-v /data/pg:/var/lib/postgresql/data mdillon/postgis:latest

Here you can see that we mounted the /data/pg directory to the data directory space of pgsql

2. Prepare profile environment variables

Example:

The previous ones are all default. The key point is to fill in the password of the pg library that needs to be logged in remotely in PGPASSWORD.

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "`id -u`" -eq 0 ]; then
  PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
else
  PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
fi
export PATH

if [ "${PS1-}" ]; then
  if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi



export PGPASSWORD='123456'


Create a profile file in the server's /data/pgfile directory and paste the content of the above file

insert image description here

Execute the docker cp command to copy the profile environment configuration to the container

Example: docker cp local directory container name: container directory

docker cp /data/pgfile/profile postgres:/etc

After the environment variable is copied to the container directory, you can enter the container to view

Here you can see that the /etc/profile of the container has been replaced by us

insert image description here

Then use source /etc/profile in the container

Excuting an order

 psql 172.19.52.5 -U postgres -p 5432 -d postgres

You can see that the remote connection to the database is successful

image-20230331120641051

3. Prepare backup.sh backup script

Example:

#!/bin/bash

# 数据库信息
DB_HOST=172.19.52.5
DB_PORT=5432
DB_USER=postgres
DB_NAME=ry-cloud
#文备份文件夹目录
BACKUP_DIR=/var/lib/postgresql/data/backup

#备份文件名称
BACKUP_FILE=$BACKUP_DIR/ry-cloud-$(date +%Y%m%d%H%M%S).sql.gz


pg_dump -h $DB_HOST -p $DB_PORT -U $DB_USER -d  $DB_NAME   | gzip >$BACKUP_FILE 


#查找1天前的数据 删除
find $BACKUP_DIR -type f -name "*.gz" -mtime +1 -exec rm {
    
    } \;






Create a mysh folder under the /data/pg directory Create a backup.sh script file under the mysh folder

Create a backup folder under the /data/pg directory. This folder is used to store backup data

image-20230331121110169

Because the container mapping directory corresponding to /data/pg is /var/lib/postgresql/data

So /mysh/backup.sh will appear in the /var/lib/postgresql/data directory of the container

image-20230331121251388

4. Execute the script test

exit container

Execute the command to check whether backup.sh can be executed normally

docker exec  postgres bash -c "source /etc/profile && /var/lib/postgresql/data/mysh/backup.sh"

image-20230331121703564

After executing the command, you can see that there is a backup file in the /data/pg/backup directory, which is the entire database compressed package from pg_dump

5.linux crontab set task timing execution

Use crontab -e to edit the scheduled task and add the backup command below and it's over

Use the command to edit crontab timing tasks

crontab -e 

Execute the backup command at two o'clock in the morning every day

0 2 * * *  sudo docker exec  postgres bash -c "source /etc/profile && /var/lib/postgresql/data/mysh/backup.sh" >> /data/pg/mysh/my.log 2>&1

image-20230331122041465

6. Summary

1. Pay attention to the relevant mapping path of the container

2. The pgsql version of the docker container should be consistent with the version of the remote server

3. Note that the backup directory address in backup.sh is the directory address of the container because the last script is executed in the container

4. Other commands are dead. Pay attention to the parameter configuration of backup.sh and the pg library password configuration of profile

Guess you like

Origin blog.csdn.net/qq_37349379/article/details/130088847