乐趣区

关于postgresql:pgbackrest-实践

装置 postgres,理论环境已装置可跳过, 参考官网装置

sudo sh -c 'echo"deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main"> /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
# 抉择装置版本,这里默认 14.5
sudo apt-get -y install postgresql-14 

装置 pgbackRest

mkdir -p /build
wget -q -O - https://ghproxy.com/https://github.com/pgbackrest/pgbackrest/archive/release/2.41.tar.gz | tar zx -C /build
sudo apt-get -y install make gcc libpq-dev libssl-dev libxml2-dev pkg-config liblz4-dev libzstd-dev libbz2-dev libz-dev libyaml-dev
cd /build/pgbackrest-release-2.41/src && ./configure && make
sudo cp pgbackrest /usr/bin
sudo chmod 755 /usr/bin/pgbackrest

创立配置文件

sudo mkdir -p -m 770 /var/log/pgbackrest
sudo chown postgres:postgres /var/log/pgbackrest
sudo mkdir -p /etc/pgbackrest
sudo mkdir -p /etc/pgbackrest/conf.d
sudo touch /etc/pgbackrest/pgbackrest.conf
sudo chmod 640 /etc/pgbackrest/pgbackrest.conf
sudo chown postgres:postgres /etc/pgbackrest/pgbackrest.conf
sudo mkdir -p /var/lib/pgbackrest
sudo chmod 750 /var/lib/pgbackrest
sudo chown postgres:postgres /var/lib/pgbackrest

# 写入配置文件
cat << EOF > /etc/pgbackrest/pgbackrest.conf
[postgres]
pg1-path=/var/lib/postgresql/14/main
[global]
repo1-path=/var/lib/pgbackrest
repo1-retention-full=2
repo1-cipher-pass=Dpy3iBVyfoAsDH+OQyApvpNT5ijs3jPI3fuVgTRKKbckN9HZe74ObGtsdkrhaa7T
repo1-cipher-type=aes-256-cbc
process-max=3

[global:archive-push]
compress-level=3
EOF

批改 postgres 配置文件

sed -i "s/#archive_mode = off/archive_mode = on/g" /etc/postgresql/14/main/postgresql.conf
sed -i "s/#archive_command =''/archive_command = 'pgbackrest --stanza=postgres archive-push %p'/g" /etc/postgresql/14/main/postgresql.conf
sed -i "s/#listen_addresses ='localhost'/listen_addresses ='*'/g" /etc/postgresql/14/main/postgresql.conf
sed -i "s/#max_wal_senders = 10/max_wal_senders = 3/g" /etc/postgresql/14/main/postgresql.conf

容许近程登录(可选)

echo "host    all             all             0.0.0.0/0               scram-sha-256" >> /etc/postgresql/14/main/pg_hba.conf 

批改明码 (可选)

sudo -u postgres psql << EOF 
ALTER USER postgres WITH PASSWORD 'postgres';
EOF

创立 stanza

sudo -u postgres pgbackrest --stanza=postgres --log-level-console=info stanza-create

查看 stanza

sudo -u postgres pgbackrest --stanza=postgres --log-level-console=info check

重启 postgres

sudo -u postgres /usr/lib/postgresql/14/bin/pg_ctl -D /var/lib/postgresql/14/main/ restart

以上步骤就实现 pgbackrest 的根本设置了


全量备份
sudo -u postgres pgbackrest --stanza=postgres --log-level-console=info backup
增量备份
sudo -u postgres pgbackrest --stanza=postgres type=incr --log-level-console=info backup
差别备份
sudo -u postgres pgbackrest --stanza=postgres type=diff --log-level-console=info backup
执行还原
执行还原前须要先停掉 postgres
sudo -u postgres /usr/lib/postgresql/14/bin/pg_ctl -D /var/lib/postgresql/14/main/ stop
pgbackrest --stanza=postgres --log-level-console=info --delta restore
退出移动版