postgresql - Postgres 9.4 恢复不工作

标签 postgresql postgresql-9.4

我根据 WAL 归档的 postgres 文档备份了 postgresql 9.4 数据库。

备份后我在数据库中创建了 2 条记录。

现在,当我尝试恢复数据库时,我在上面创建的最后 2 条记录没有出现。

WAL归档步骤:

cd /etc/postgresql/9.4/
mkdir archives
mkdir backups
chown postgres:postgres archives
chown postgres:postgres backups
cd /etc/postgresql/9.4/main/
echo 'max_wal_senders=1' >> postgresql.conf
echo 'wal_level=hot_standby' >> postgresql.conf
echo 'archive_mode=on' >> postgresql.conf
echo "archive_command='test ! -f /etc/postgresql/9.4/archives/%f && cp %p /etc/postgresql/9.4/archives/%f'" >> postgresql.conf
echo 'local replication postgres trust' >> pg_hba.conf
service postgresql restart

备份步骤:

cd /etc/postgresql/9.4/backups
rm -rf *
pg_basebackup --xlog -U postgres --format=t -D /etc/postgresql/9.4/backups/

还原步骤:

 service postgresql stop
 cd /var/lib/postgresql/9.4/
 if [ ! -d "/var/lib/postgresql/9.4/tmp/" ]
 then
    mkdir tmp
  else
    rm -rf tmp
  fi
  mkdir tmp
   mv /var/lib/postgresql/9.4/main/* /var/lib/postgresql/9.4/tmp/
  cd /var/lib/postgresql/9.4/main/
  rm -rf *
   cd /etc/postgresql/9.4/backups
  tar -xf base.tar -C /var/lib/postgresql/9.4/main/
 cd /var/lib/postgresql/9.4/main/

 FROMDIR="/etc/postgresql/9.4/archives/"

 TODIR="/var/lib/postgresql/9.4/tmp/pg_xlog/"

 if [ ! -d "$FROMDIR" ]
 then
      echo "Directory $FROMDIR does not exist!!"
 exit
 fi


 if [ ! -d "$TODIR" ]
 then
        echo "Directory $TODIR does not exist!!"
        exit
  fi

  cd $FROMDIR

  for i in `find . -type f`
      do
         if [ ! -f $TODIR/$i ]
      then
      echo "copying file $i"
      cp $i /var/lib/postgresql/9.4/main/pg_xlog/$i
      fi

     done

   cd /var/lib/postgresql/9.4/main/pg_xlog/

   chown -R postgres:postgres *

   cd /var/lib/postgresql/9.4/main/

   FILE="recovery.done"

   if [ -f $FILE ]
   then
          mv $FILE recovery.conf
   else
         echo "restore_command = 'cp /etc/postgresql/9.4/archives/%f %p'" >> recovery.conf
   fi

   su postgres service postgresql start

   exit

最佳答案

当当前 WAL 段(通常为 16 Mb)被填满时,更改会出现在存档中(在您的情况下为/etc/postgresql/9.4/archives/)。让我引用 documentation :

The archive_command is only invoked for completed WAL segments. Hence, if your server generates little WAL traffic (or has slack periods where it does so), there could be a long delay between the completion of a transaction and its safe recording in archive storage. To limit how old unarchived data can be, you can set archive_timeout to force the server to switch to a new WAL segment file periodically. When this parameter is greater than zero, the server will switch to a new segment file whenever this many seconds have elapsed since the last segment file switch, and there has been any database activity, including a single checkpoint. (Increasing checkpoint_timeout will reduce unnecessary checkpoints on an idle system.) Note that archived files that are closed early due to a forced switch are still the same length as completely full files. Therefore, it is unwise to use a very short archive_timeout — it will bloat your archive storage.

如果您只想测试恢复过程,您可以在创建一些记录以强制切换到新的 WAL 段之后简单地执行 select pg_switch_xlog();。然后验证存档目录中是否出现了一个新文件。


此外,您不需要将文件从存档目录复制到 pg_xlog/。 Restore_command 将为您完成。

关于postgresql - Postgres 9.4 恢复不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33795975/

相关文章:

mysql - 在处理数据库时,当我们可以使用简单的哈希时添加不同的表是一件好事吗?

postgresql - 在 PostgreSQL 中找不到函数

postgresql - PreparedStatement 不起作用时如何在 JDBC 中转义 SQL 参数?

Postgresql:获取jsonb列的所有最外键

json - 如何在 PostgreSQL (9.4+) 中选择不区分大小写的 JSONB 键

java - Postgresql 枚举在 springboot 1.5->2.0 迁移期间中断

database - 使用 Postgres 复制整个表

postgresql - postgres,go-gorm-无法预加载数据

mysql - PostgreSQL 9.4 : Use calculated column in the same query and group partial results by month

sql - 获取时间跨度内的 session 持续时间平均值