热衷于分享各种干货知识,大家有想看或者想学的可以评论区留言,秉承着“开源知识来源于互联网,回归于互联网”的理念,分享一些日常工作中能用到或者比较重要的内容,希望大家能够喜欢,不足之处请大家多提宝贵地意见,我们一起提升,守住自己的饭碗。
一、备份脚本
#!/bin/bash
# 定义一些变量
BACKUP_DIR="/pgdb/pg_backup"
PG_USER="postgres"
PG_HOST="localhost"
PG_PORT="5785"
BACKUP_LABEL="base_backup"
DATE=$(date +"%Y%m%d%H%M%S")
LOG_FILE="${BACKUP_DIR}/pg_backup_${DATE}.log"
# 检查并创建备份目录
if[!-d "$BACKUP_DIR"];then
mkdir -p "$BACKUP_DIR"
fi
# 执行 pg_basebackup 命令
echo "Starting PostgreSQL physical backup..."
/pgdb/pgsql/bin/pg_basebackup -h $PG_HOST -p $PG_PORT -U $PG_USER -D "${BACKUP_DIR}/${BACKUP_LABEL}_${DATE}"-F tar -v -P >>"$LOG_FILE"2>&1
# 检查备份是否成功
if[ $?-eq 0];then
echo "Backup completed successfully."
echo "Backup is available at: ${BACKUP_DIR}/${BACKUP_LABEL}_${DATE}"
else
echo "Backup failed. Check the log file for details: $LOG_FILE"
exit1
fi
echo "Backup operation finished. Log file: $LOG_FILE"
二、 恢复脚本
#!/bin/bash
# Define variables
BACKUP_DIR="/pgdb/pg_backup"
RESTORE_LABEL="base_backup"
PG_DATA_DIR="/pgdb/data"
DATE=$(date +"%Y%m%d%H%M%S")
LOG_FILE="${BACKUP_DIR}/pg_restore_${DATE}.log"
# Check if backup directory exists
if[!-d "$BACKUP_DIR"];then
echo "Backup directory does not exist: $BACKUP_DIR"
exit1
fi
# Stop PostgreSQL service
echo "Stopping PostgreSQL service..."
systemctl stop postgres.service
# Ensure you're in a directory that postgres can access
cd /tmp
# Ensure PostgreSQL is stopped
echo "Checking if PostgreSQL server has stopped..."
sudo -u postgres /pgdb/pgsql/bin/pg_ctl status -D $PG_DATA_DIR | grep -q "server is running"
if[ $?-eq 0];then
echo "PostgreSQL server did not stop successfully. Exiting."
exit1
fi
# Check if the data directory is empty or improperly initialized
if["$(ls -A $PG_DATA_DIR)"];then
echo "Clearing existing data directory: $PG_DATA_DIR"
rm -rf $PG_DATA_DIR/*
if [ $? -ne 0 ]; then
echo "Failed to clear data directory. Check permissions and try again."
exit 1
fi
else
echo "Data directory is already empty or incorrectly initialized."
fi
# Restore data
LATEST_BACKUP=$(find $BACKUP_DIR -name "${RESTORE_LABEL}_*.tar" | sort | tail -n 1)
if [ -z "$LATEST_BACKUP" ]; then
echo "No backup files found in $BACKUP_DIR. Please ensure backups are available."
exit 1
fi
echo "Restoring from backup: $LATEST_BACKUP"
tar -xf "$LATEST_BACKUP" -C $PG_DATA_DIR >> "$LOG_FILE" 2>&1
# Check if restore was successful
if [ $? -eq 0 ]; then
echo "Backup restored successfully from: $LATEST_BACKUP"
else
echo "Failed to restore backup. See log for details: $LOG_FILE"
exit 1
fi
# Adjust permissions on data directory
echo "Adjusting permissions on data directory..."
chown -R postgres:postgres $PG_DATA_DIR
chmod 700 $PG_DATA_DIR
# Start PostgreSQL service
echo "Starting PostgreSQL service..."
systemctl start postgres.service
if systemctl is-active --quiet postgres.service; then
echo "PostgreSQL service successfully restarted."
else
echo "PostgreSQL service failed to start. Check service status for issues."
exit 1
fi
echo "Restore operation completed. Log file: $LOG_FILE"
三、说明
脚本赋予执行权限,一键执行即可。大家可以根据自己的实际情况修改备份和恢复脚本的内容,恢复脚本需谨慎使用。
标签:PostgreSQL,backup,Postgresql14,备份,一键,echo,PG,BACKUP,DIR From: https://www.cnblogs.com/cheyunhua/p/18454838