search
HomeDatabaseMysql Tutorial 【原创】PostgreSQL 增量备份详解以及相关示例

PostgreSQL没有类似MySQL的二进制日志,但是有和MySQL类似的REDOLOG,并且有MySQL没有的REDO归档功能。当然REDO的归档已经MariaDB和Percona-MySQL已经实现。那我

PostgreSQL 没有类似MySQL 的二进制日志, 但是有和MySQL 类似的REDO LOG,并且有MySQL 没有的REDO 归档功能。当然REDO 的归档已经MariaDB 和Percona-MySQL 已经实现。

那我们就利用PGSQL的REDO 归档,也叫WAL来实现增量备份。


假设我们已经做了一个物理的全备,并且相应的参数都已经设置完毕,比如以下:


/home/full_backup 保存全备的目录。 /home/increment_log 保存增备的目录。 进入postgresql.conf 开启WAL. wal_level = archive wal_keep_segments=1 开启WAL 的归档模式. archive_mode = on

archive_command = 'test ! -f /home/increment_log/%f && cp %p /home/increment_log/%f' (归档,也就是增量)


那么关于WAL归档日志要注意的一点是,如果CRASH 掉了,那么没有归档的XLOG也可能没有提交到数据文件,,所以有可能这部分数据会丢失掉。所以在之后恢复的时候我们要把这部分XLOG拷贝到全备的对应目录。



恢复方法:

1. 确保没有任何业务对PG服务进行访问。(有可能这个时候PG已经CRASH掉了。) 2. 先停掉POSTGRESQL 服务。 3. cd /home/pgsql; 4. mv data data_old 6. cp -rfp /home/full_backup/* /home/pgsql (拷贝全备) 7. cp -rfp data_old/pg_xlog/000* data/pg_xlog/ (这里是拷贝自从最后一个增量和系统CRASH之间的XLOG到对应的目录。) 8. cd data 9. vi recovery.conf (添加recovery.conf文件,让PG认为这次是在恢复。) restore_command='test ! -f /home/increment_log/%f && cp /home/increment_log/%f %p' recovery_target_timeline = 'latest' 10. chown -R postgres.postgres recovery.conf 11. 启动postgresql 服务即可。



如果正确恢复了,那么recovery.conf 会自动更名为recovery.done.


以我的机器为例,我的版本是9.3,PG_XLOG中看到的BACKUP 信息:

[root@postgresql-instance pg_xlog]# cat 000000010000000000000003.00000028.backup START WAL LOCATION: 0/3000028 (file 000000010000000000000003) STOP WAL LOCATION: 0/30000B8 (file 000000010000000000000003) CHECKPOINT LOCATION: 0/3000028 BACKUP METHOD: pg_start_backup BACKUP FROM: master START TIME: 2014-01-07 03:35:02 EST LABEL: ytt STOP TIME: 2014-01-07 03:36:27 EST


日志中会看到恢复如下恢复信息:

LOG: 00000: starting archive recovery LOCATION: StartupXLOG, xlog.c:4983 LOG: 00000: redo starts at 0/3000028 LOCATION: StartupXLOG, xlog.c:5531 LOG: 00000: consistent recovery state reached at 0/303FC88


本文出自 “上帝,咱们不见不散!” 博客,请务必保留此出处

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does MySQL handle data replication?How does MySQL handle data replication?Apr 28, 2025 am 12:25 AM

MySQL processes data replication through three modes: asynchronous, semi-synchronous and group replication. 1) Asynchronous replication performance is high but data may be lost. 2) Semi-synchronous replication improves data security but increases latency. 3) Group replication supports multi-master replication and failover, suitable for high availability requirements.

How can you use the EXPLAIN statement to analyze query performance?How can you use the EXPLAIN statement to analyze query performance?Apr 28, 2025 am 12:24 AM

The EXPLAIN statement can be used to analyze and improve SQL query performance. 1. Execute the EXPLAIN statement to view the query plan. 2. Analyze the output results, pay attention to access type, index usage and JOIN order. 3. Create or adjust indexes based on the analysis results, optimize JOIN operations, and avoid full table scanning to improve query efficiency.

How do you back up and restore a MySQL database?How do you back up and restore a MySQL database?Apr 28, 2025 am 12:23 AM

Using mysqldump for logical backup and MySQLEnterpriseBackup for hot backup are effective ways to back up MySQL databases. 1. Use mysqldump to back up the database: mysqldump-uroot-pmydatabase>mydatabase_backup.sql. 2. Use MySQLEnterpriseBackup for hot backup: mysqlbackup--user=root-password=password--backup-dir=/path/to/backupbackup. When recovering, use the corresponding life

What are some common causes of slow queries in MySQL?What are some common causes of slow queries in MySQL?Apr 28, 2025 am 12:18 AM

The main reasons for slow MySQL query include missing or improper use of indexes, query complexity, excessive data volume and insufficient hardware resources. Optimization suggestions include: 1. Create appropriate indexes; 2. Optimize query statements; 3. Use table partitioning technology; 4. Appropriately upgrade hardware.

What are views in MySQL?What are views in MySQL?Apr 28, 2025 am 12:04 AM

MySQL view is a virtual table based on SQL query results and does not store data. 1) Views simplify complex queries, 2) Enhance data security, and 3) Maintain data consistency. Views are stored queries in databases that can be used like tables, but data is generated dynamically.

What are the differences in syntax between MySQL and other SQL dialects?What are the differences in syntax between MySQL and other SQL dialects?Apr 27, 2025 am 12:26 AM

MySQLdiffersfromotherSQLdialectsinsyntaxforLIMIT,auto-increment,stringcomparison,subqueries,andperformanceanalysis.1)MySQLusesLIMIT,whileSQLServerusesTOPandOracleusesROWNUM.2)MySQL'sAUTO_INCREMENTcontrastswithPostgreSQL'sSERIALandOracle'ssequenceandt

What is MySQL partitioning?What is MySQL partitioning?Apr 27, 2025 am 12:23 AM

MySQL partitioning improves performance and simplifies maintenance. 1) Divide large tables into small pieces by specific criteria (such as date ranges), 2) physically divide data into independent files, 3) MySQL can focus on related partitions when querying, 4) Query optimizer can skip unrelated partitions, 5) Choosing the right partition strategy and maintaining it regularly is key.

How do you grant and revoke privileges in MySQL?How do you grant and revoke privileges in MySQL?Apr 27, 2025 am 12:21 AM

How to grant and revoke permissions in MySQL? 1. Use the GRANT statement to grant permissions, such as GRANTALLPRIVILEGESONdatabase_name.TO'username'@'host'; 2. Use the REVOKE statement to revoke permissions, such as REVOKEALLPRIVILEGESONdatabase_name.FROM'username'@'host' to ensure timely communication of permission changes.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft