postgresql automatically adds pitr script

 Postgresql provides pitr as an additional plan. On the basis of full physical backup, all data modification records are provided through the backup of the wal file. When the restoration is required, the data is restored through the wal log playback. The restoration will be discussed later. The post mainly explains that regular automatic full backups and pitr incremental backups of the database are performed through scripts and crontab scheduled tasks, so as to avoid a basic backup + long-term backup log which will cause a lot of time for rollback or restoration.

 

1. Write base_backup.sh to provide regular full base backups.

  During the execution of the script, the pg_start_backup() function will be executed first, then the entire $PGDATA directory will be compressed, and then the pg_stop_backup() function will be executed to back up and exit. Clean up old full backup files after all is done.

 

#!/bin/bash
base_dir="/home/lyf/pgback/back_9.6"
pirt_dir="/home/lyf/pgback/back_9.6"
pg_home="/home/lyf/9.6/pg9.6"
pg_port=5555
pg_data="/home/lyf/9.6/pg9.6/data"

# wal dir is not null
if test -z "$pirt_dir"
then
	echo "pirl_dir is null"
	exit
else
	if [ ! -d "$pirt_dir" ]
	then
		mkdir "$pirt_dir"
	be
be

# info file
_date=$(date +%y%m%d)
echo $_date>"${pirt_dir}/info"

if test -z "$base_dir"
then
	echo "base dir is null"
	exit
else
	echo "base dir is : ${base_dir}"
be

if test -z "$pg_home"
then
	echo "pg_home is null"
	exit
else
	echo "pg_home is : ${base_dir}"
be

if test -z "$pg_port"
then
	echo "pg_port is null"
	 pg_port=5432
 
be

if test -z "$pg_data"
then
	echo "pg_data is null"
	 pg_data="${pg_home}/data"
 
be

echo "pg_home:${pg_home},pg_port=:${pg_port},pg_data:${pg_data}"



#new dir


#back base data
${pg_home}/bin/psql -U postgres -p ${pg_port} -h 127.0.0.1 -c " select pg_start_backup('${_date}') "

#start back up

zip -r "$base_dir"/"$_date".zip "$pg_data"/*


#back end

${pg_home}/bin/psql -U postgres -p ${pg_port} -h 127.0.0.1 -c " select pg_stop_backup() "

#clear old dir wal

for p_dir in $(ls ${base_dir})
do
	if  test -d ${base_dir}/${p_dir} && [ ${p_dir} \< $_date ]
	then
		echo "clear data ${p_dir}"
		rm -rf $ {base_dir} / $ {p_dir}

	be


done

# clear old base back zip
for file in $(ls ${base_dir}/*.zip )
do
	fname=${file##*/}
	fdate=${fname%%.*}
	if ! test -z $fdate  && [ $fdate \< $_date ]
	then
		echo "rm file:${base_dir}/${fname}"
		rm ${base_dir}/${fname}
	be

done

# new wal back dir
# export pitr_dir = $ {curr_dir}








  You need to modify each configuration to be the installation directory and configuration of your current database. After modification, add the script to the crontab task list. The current defect of this script is: the full backup interval must be greater than 1 day, because the backup file name of the full backup is created in units of days.

 

#10 0 * * * sh /home/lyf/pgback/base_backup.sh > /home/lyf/pgback/back.log 2>&1

 

  All paths need to be changed to the correct path.

 

 

2. Write the pitr log backup script pitr.sh.

 

   This script configures the archive_command in postgresql.conf to use, and accurately puts the wal log into the wal log directory made after the latest full backup through the configuration file.

#!/bin/bash
# pirt_dir=${WAL_DIR}
pirt_dir="/home/lyf/pgback/back_9.6"

_p=$1
_f=$2

if [ x$_p = x ]
then
	# has %p
	exit
be
if [ x$_f = x ]
then
	exit
be


if test -z "$pirt_dir"
then
	echo "wal dir is null"
	exit
be


info_file="${pirt_dir}"/info

if [ ! -f "$info_file" ]
then
	_date=$(date +%y%m%d)
	echo $_date>"$info_file"
be

info_context=$(cat "$info_file")
echo "info context is ${info_context}"
# echo "${info_context}"

log_dir="$pirt_dir"/"$info_context"

# echo "log_dir:${log_dir}"

if [ ! -d "$log_dir" ]
then
	mkdir "$log_dir"
be

# echo "test cp "
if [ ! -f "${log_dir}/%f" ]
then
	echo "p is $_p"
	echo "f is $_f"
	#	echo "%p $log_dir/%f"
	cp $_p ${log_dir}/$_f
be

# test ! -f ${log_dir}/%f && cp %p ${log_dir}/%f

 

3. Modify the postgresql configuration file postgresql.conf.

  

wal_level = replica			# minimal, replica, or logical
archive_mode = on		
archive_command = 'sh /home/lyf/pgback/pitr.sh (path to pitr.sh) %p %f >> /home/lyf/pgback/pitr.log (logging file) '	

 

4. Restart the database.

  At this point, the incremental backup will work normally. A full basic backup will be made at the time point configured in crontab, and then the wal backup directory will be switched.

 

5. Restore from backup

5.1, stop the warehouse. 

5.2. Back up the current $PGDATA directory first , and then use the basic backup and wal log to restore after completion . (There is not much to say about the purpose of this backup. It is best to have a backup before all operations on the data, even if the current data may be damaged or unwanted)

5.3. Decompress the base backup .zip file from the backup directory ($ base_dir in the configuration file) , and after decompression, modify the point of $PGDATA to the decompressed path.

5.4. Move the recovery.conf file from the share directory to the new $PGDATA, edit the file to modify: restore_command=' cp / home/lyf/pgback/back_9.6 /20170531/ %f %p' 

    Note: The red part should be replaced with the real pirt backup directory. Refer to the directory configured in pitr.sh to select the folder with the same file name as the base backup.

5.5. Start the database meeting, and you can see that the restoration starts. 

5.6. If you want to restore to a specific targetlabel or time to modify the configuration 

 

#recovery_target_name = ''     # e.g. 'daily backup 2011-01-26'
#
#recovery_target_time = ''       # e.g. '2004-07-14 22:39:00 EST'
#
#recovery_target_xid = ''
#
#recovery_target_inclusive = true
 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326243903&siteId=291194637