|
|
<!DOCTYPE html>
|
|
|
<html lang="en-us">
|
|
|
<head><meta charset="utf-8">
|
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
|
<script type="application/ld+json">
|
|
|
{
|
|
|
"@context" : "http://schema.org",
|
|
|
"@type" : "BlogPosting",
|
|
|
"mainEntityOfPage": {
|
|
|
"@type": "WebPage",
|
|
|
"@id": "https:\/\/h.cowbay.org"
|
|
|
},
|
|
|
"articleSection" : "post",
|
|
|
"name" : "[筆記] 用pbackrest 備份還原 postgresql \/ Backup Restore Postgresql With Pgbackrest",
|
|
|
"headline" : "[筆記] 用pbackrest 備份還原 postgresql \/ Backup Restore Postgresql With Pgbackrest",
|
|
|
"description" : "\x3cp\x3e這兩天在測試pgbackrest ,簡單筆記一下測試狀況\x3c\/p\x3e",
|
|
|
"inLanguage" : "en",
|
|
|
"author" : "Eric Chang",
|
|
|
"creator" : "Eric Chang",
|
|
|
"publisher": "Eric Chang",
|
|
|
"accountablePerson" : "Eric Chang",
|
|
|
"copyrightHolder" : "Eric Chang",
|
|
|
"copyrightYear" : "2019",
|
|
|
"datePublished": "2019-09-05 11:42:28 \x2b0800 CST",
|
|
|
"dateModified" : "2019-09-05 11:42:28 \x2b0800 CST",
|
|
|
"url" : "https:\/\/h.cowbay.org\/post\/backup-restore-postgresql-with-pgbackrest\/",
|
|
|
"wordCount" : "818",
|
|
|
"image" : "https://h.cowbay.orghttps://h.cowbay.org/images/post-default-15.jpg"",
|
|
|
"keywords" : [ ""postgresql"","Blog" ]
|
|
|
}
|
|
|
</script>
|
|
|
|
|
|
|
|
|
<title>[筆記] 用pbackrest 備份還原 postgresql / Backup Restore Postgresql With Pgbackrest </title>
|
|
|
|
|
|
|
|
|
<meta name="description" content="some articles about job,food,passion sisters" />
|
|
|
|
|
|
|
|
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
|
<meta name="robots" content="all,follow">
|
|
|
<meta name="googlebot" content="index,follow,snippet,archive">
|
|
|
<link rel="stylesheet" id="ct-tracks-google-fonts-css" href="https://fonts.googleapis.com/css?family=Raleway%3A400%2C700&subset=latin%2Clatin-ext&ver=4.7.2" type="text/css" media="all">
|
|
|
|
|
|
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous">
|
|
|
|
|
|
<link href="https://h.cowbay.org/css/style.css?v=1624607940" rel="stylesheet" id="theme-stylesheet" type='text/css' media='all'>
|
|
|
|
|
|
<link href="https://h.cowbay.org/css/custom.css?v=1624607940" rel="stylesheet" type='text/css' media='all'>
|
|
|
<link rel="shortcut icon" href="https://h.cowbay.org/img/favicon.ico" type="image/x-icon">
|
|
|
<link rel="icon" href="https://h.cowbay.org/img/favicon.ico" type="image/x-icon">
|
|
|
|
|
|
|
|
|
<script type="application/javascript">
|
|
|
var doNotTrack = false;
|
|
|
if (!doNotTrack) {
|
|
|
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
|
|
|
ga('create', 'UA-138954876-1', 'auto');
|
|
|
|
|
|
ga('send', 'pageview');
|
|
|
}
|
|
|
</script>
|
|
|
<script async src='https://www.google-analytics.com/analytics.js'></script>
|
|
|
|
|
|
</head>
|
|
|
|
|
|
|
|
|
<body class="post-template-default single single-post single-format-standard ct-body singular singular-post not-front standard">
|
|
|
|
|
|
<div id="overflow-container" class="overflow-container">
|
|
|
<a class="skip-content" href="#main">Skip to content</a>
|
|
|
<header id="site-header" class="site-header" role="banner">
|
|
|
<div class='top-navigation'>
|
|
|
<div class='container'>
|
|
|
|
|
|
<div id="menu-secondary" class="menu-container menu-secondary" role="navigation">
|
|
|
<button id="toggle-secondary-navigation" class="toggle-secondary-navigation"><i class="fas fa-plus"></i></button>
|
|
|
|
|
|
<div class="menu">
|
|
|
|
|
|
<ul id="menu-secondary-items" class="menu-secondary-items">
|
|
|
|
|
|
<li class="menu-item menu-item-type-taxonomy menu-item-object-category">
|
|
|
<a href="/categories/"></a>
|
|
|
</li>
|
|
|
|
|
|
<li class="menu-item menu-item-type-taxonomy menu-item-object-category">
|
|
|
<a href="/categories/ansible">ansible</a>
|
|
|
</li>
|
|
|
|
|
|
<li class="menu-item menu-item-type-taxonomy menu-item-object-category">
|
|
|
<a href="/categories/linux">linux</a>
|
|
|
</li>
|
|
|
|
|
|
<li class="menu-item menu-item-type-taxonomy menu-item-object-category">
|
|
|
<a href="/categories/proxmox">proxmox</a>
|
|
|
</li>
|
|
|
|
|
|
<li class="menu-item menu-item-type-taxonomy menu-item-object-category">
|
|
|
<a href="/categories/ps">ps</a>
|
|
|
</li>
|
|
|
|
|
|
<li class="menu-item menu-item-type-taxonomy menu-item-object-category">
|
|
|
<a href="/categories/%E7%A2%8E%E5%BF%B5">碎念</a>
|
|
|
</li>
|
|
|
|
|
|
<li class="menu-item menu-item-type-taxonomy menu-item-object-category">
|
|
|
<a href="/categories/%E7%AD%86%E8%A8%98">筆記</a>
|
|
|
</li>
|
|
|
|
|
|
<li class="menu-item menu-item-type-taxonomy menu-item-object-category">
|
|
|
<a href="/categories/%E7%BE%A4%E6%9A%89">群暉</a>
|
|
|
</li>
|
|
|
|
|
|
<li class="menu-item menu-item-type-taxonomy menu-item-object-category">
|
|
|
<a href="/categories/%E9%9B%9C%E5%BF%B5">雜念</a>
|
|
|
</li>
|
|
|
|
|
|
|
|
|
</ul>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
<ul class="social-media-icons">
|
|
|
|
|
|
|
|
|
|
|
|
<li>
|
|
|
<a href="full%20Social%20profile%20url%20in%20facebook" data-animate-hover="pulse" class="facebook" target="_blank">
|
|
|
<i class="fab fa-facebook-square" title="facebook"></i>
|
|
|
<span class="screen-reader-text">facebook</span>
|
|
|
</a>
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
<li>
|
|
|
<a href="full%20profile%20url%20in%20googleplus" data-animate-hover="pulse" class="gplus" target="_blank">
|
|
|
<i class="fab fa-google-plus-g" title="googleplus"></i>
|
|
|
<span class="screen-reader-text">googleplus</span>
|
|
|
</a>
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
<li>
|
|
|
<a href="chang0206" data-animate-hover="pulse" class="twitter" target="_blank">
|
|
|
<i class="fab fa-twitter-square" title="twitter"></i>
|
|
|
<span class="screen-reader-text">twitter</span>
|
|
|
</a>
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
<li>
|
|
|
<a href="chang0206" data-animate-hover="pulse" class="instagram" target="_blank">
|
|
|
<i class="fab fa-instagram" title="instagram"></i>
|
|
|
<span class="screen-reader-text">instagram</span>
|
|
|
</a>
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
<li>
|
|
|
<a href="mailto:mc@hotshraingmy.info" data-animate-hover="pulse" class="email">
|
|
|
<i class="fas fa-envelope" title="email"></i>
|
|
|
<span class="screen-reader-text">email</span>
|
|
|
</a>
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
<li>
|
|
|
<a href="full%20profile%20url%20in%20linkedin" data-animate-hover="pulse" class="linkedin" target="_blank">
|
|
|
<i class="fab fa-linkedin-in" title="linkedin"></i>
|
|
|
<span class="screen-reader-text">linkedin</span>
|
|
|
</a>
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
<li>
|
|
|
<a href="full%20profile%20url%20in%20stackoverflow" data-animate-hover="pulse" class="stackoverflow" target="_blank">
|
|
|
<i class="fab fa-stack-overflow" title="stackoverflow"></i>
|
|
|
<span class="screen-reader-text">stackoverflow</span>
|
|
|
</a>
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li>
|
|
|
<a href="changchichung" data-animate-hover="pulse" class="github" target="_blank">
|
|
|
<i class="fab fa-github" title="github"></i>
|
|
|
<span class="screen-reader-text">github</span>
|
|
|
</a>
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li>
|
|
|
<a href="full%20profile%20url%20in%20pinterest" data-animate-hover="pulse" class="pinterest" target="_blank">
|
|
|
<i class="fab fa-pinterest" title="pinterest"></i>
|
|
|
<span class="screen-reader-text">pinterest</span>
|
|
|
</a>
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
<li>
|
|
|
<a href="https://h.cowbay.org/index.xml" data-animate-hover="pulse" class="rss" target="_blank">
|
|
|
<i class="fas fa-rss" title="rss"></i>
|
|
|
<span class="screen-reader-text">rss</span>
|
|
|
</a>
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
</ul></div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="container">
|
|
|
<div id="title-info" class="title-info">
|
|
|
<div id='site-title' class='site-title'>
|
|
|
|
|
|
<a href="/"> MC部落 </a>
|
|
|
</div>
|
|
|
</div>
|
|
|
<button id="toggle-navigation" class="toggle-navigation">
|
|
|
<i class="fas fa-bars"></i>
|
|
|
</button>
|
|
|
|
|
|
<div id="menu-primary-tracks" class="menu-primary-tracks"></div>
|
|
|
<div id="menu-primary" class="menu-container menu-primary" role="navigation">
|
|
|
|
|
|
<p class="site-description">What’s the Worst That Could Happen?</p>
|
|
|
|
|
|
|
|
|
<div class="menu">
|
|
|
<ul id="menu-primary-items" class="menu-primary-items">
|
|
|
|
|
|
|
|
|
<li class='menu-item menu-item-type-custom menu-item-object-custom '>
|
|
|
<a href="https://h.cowbay.org/">Home</a>
|
|
|
|
|
|
</li>
|
|
|
|
|
|
<li class='menu-item menu-item-type-post_type menu-item-object-page '>
|
|
|
<a href="https://h.cowbay.org/about/">About</a>
|
|
|
|
|
|
</li>
|
|
|
|
|
|
<li class='menu-item menu-item-type-post_type menu-item-object-page '>
|
|
|
<a href="https://h.cowbay.org/contact/">Get in touch</a>
|
|
|
|
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
</header>
|
|
|
|
|
|
<div id="main" class="main" role="main">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<div id="loop-container" class="loop-container">
|
|
|
|
|
|
<div class="post type-post status-publish format-standard has-post-thumbnail hentry category-design tag-design tag-standard-2 tag-tagalicious tag-travel entry full-without-featured odd excerpt-1">
|
|
|
|
|
|
<div class='featured-image lazy lazy-bg-image' data-background="https://h.cowbay.org/images/post-default-15.jpg">
|
|
|
</div>
|
|
|
|
|
|
<div class="entry-meta">
|
|
|
<span class="date">05 September</span> <span> / </span>
|
|
|
|
|
|
<span class="author">
|
|
|
<a href="https://github.com/changchichung" title="Posts by Eric Chang" rel="author">Eric Chang</a>
|
|
|
</span>
|
|
|
|
|
|
|
|
|
|
|
|
<span class="category">
|
|
|
<span> / </span>
|
|
|
|
|
|
<a href="/categories/%E7%AD%86%E8%A8%98">筆記</a>
|
|
|
</span>
|
|
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
<div class='entry-header'>
|
|
|
<h1 class='entry-title'> [筆記] 用pbackrest 備份還原 postgresql / Backup Restore Postgresql With Pgbackrest</h1>
|
|
|
</div>
|
|
|
<div class="entry-container">
|
|
|
<div class="entry-content">
|
|
|
<article>
|
|
|
<p>這兩天在測試pgbackrest ,簡單筆記一下測試狀況</p>
|
|
|
|
|
|
<h4 id="install">install</h4>
|
|
|
|
|
|
<p>在ubuntu 18.04 安裝pgbackrest 很簡單,內建在apt裡面,所以可以直接用</p>
|
|
|
|
|
|
<pre><code>sudo apt install pgbackrest
|
|
|
</code></pre>
|
|
|
|
|
|
<p>進行安裝</p>
|
|
|
|
|
|
<h4 id="config">config</h4>
|
|
|
|
|
|
<p>pgbackrest 的設定檔在 /etc/pgbackrest/pgbackrest.conf
|
|
|
如果是用apt 安裝,預設會建立一個 /etc/pgbackrest.conf
|
|
|
但是這個路徑是錯誤的
|
|
|
執行 pgbackrest的時候,預設還是會去讀取 /etc/pgbackrest/pgbackrest.conf
|
|
|
要特別注意,要不就是每次都指定config路徑,要不就是把那個錯誤的設定檔幹掉</p>
|
|
|
|
|
|
<h4 id="config-內容">config 內容</h4>
|
|
|
|
|
|
<p>內容其實很簡單</p>
|
|
|
|
|
|
<pre><code>postgres@hqdc039:~$ cat /etc/pgbackrest/pgbackrest.conf
|
|
|
[demo]
|
|
|
pg1-path=/database/11/main
|
|
|
|
|
|
[global]
|
|
|
repo1-cipher-pass=zWaf6XtpjIVZC5444yXB+cgFDFl7MxGlgkZSaoPvTGirhPygu4jOKOXf9LO4vjfO
|
|
|
repo1-cipher-type=aes-256-cbc
|
|
|
repo1-path=/var/lib/pgbackrest
|
|
|
repo1-retention-full=2
|
|
|
|
|
|
[global:archive-push]
|
|
|
compress-level=3
|
|
|
process-max=4
|
|
|
</code></pre>
|
|
|
|
|
|
<p><strong>[demo]</strong> 用來指定這個 “stanza” 的名稱
|
|
|
* pg1-path 是資料庫存放的路徑</p>
|
|
|
|
|
|
<p><strong>[global]</strong> 中的兩個cipher 用途我不清楚,不設定也沒關係</p>
|
|
|
|
|
|
<ul>
|
|
|
<li>“repo1-path” 則是用來存放備份的路徑</li>
|
|
|
<li>“repo1-retention-full” 定義要保留幾次 full backup</li>
|
|
|
</ul>
|
|
|
|
|
|
<p><strong>[global:archive-push]</strong> 似乎是用來定義在備份/還原時的選項
|
|
|
* process-max 指定要用多少process (平行處理)</p>
|
|
|
|
|
|
<h4 id="簡單流程">簡單流程</h4>
|
|
|
|
|
|
<ul>
|
|
|
<li>config (pgbackrest & postgresql )</li>
|
|
|
<li>建立 stanza</li>
|
|
|
<li>建立備份</li>
|
|
|
<li>(還原,如果有需要的話)</li>
|
|
|
</ul>
|
|
|
|
|
|
<h4 id="其他部份">其他部份</h4>
|
|
|
|
|
|
<ul>
|
|
|
<li>清除 stanza (刪除備份)</li>
|
|
|
<li>踩到的地雷</li>
|
|
|
</ul>
|
|
|
|
|
|
<hr />
|
|
|
|
|
|
<h4 id="設定-pgbackrest-postgresql">設定 pgbackrest & postgresql</h4>
|
|
|
|
|
|
<p>pgbackrest 基本上設定很簡單,內容就跟上面的一樣就可以跑了。
|
|
|
當然要新增其他進階的,就要自己研究了,內容有夠長
|
|
|
<a href="https://pgbackrest.org/configuration.html">https://pgbackrest.org/configuration.html</a></p>
|
|
|
|
|
|
<p>在postgresql 中,要新增一些設定,主要是 archive_command</p>
|
|
|
|
|
|
<pre><code>postgres@hqdc039:~$ grep -A 100 "for pgbackrest" /etc/postgresql/11/main/postgresql.conf
|
|
|
#for pgbackrest
|
|
|
archive_command = 'pgbackrest --stanza=demo archive-push %p'
|
|
|
archive_mode = on
|
|
|
listen_addresses = '*'
|
|
|
log_line_prefix = ''
|
|
|
max_wal_senders = 3
|
|
|
wal_level = replica
|
|
|
postgres@hqdc039:~$
|
|
|
</code></pre>
|
|
|
|
|
|
<p><strong>archive_command</strong>
|
|
|
這個因為要帶stanza 名稱進來,所以<strong>需要跟pgbackrest.conf 裡面定義的名稱一致</strong></p>
|
|
|
|
|
|
<p><strong>max_wal_senders</strong>
|
|
|
簡單說就是定義在抄寫wal 的時候,可以同時抄給幾台
|
|
|
這是postgresql 的說明</p>
|
|
|
|
|
|
<pre><code>the maximum number of simultaneously running WAL sender processes
|
|
|
</code></pre>
|
|
|
|
|
|
<p><strong>wal_level</strong>
|
|
|
有三種等級,參考 <a href="https://blog.csdn.net/pg_hgdb/article/details/78666719">https://blog.csdn.net/pg_hgdb/article/details/78666719</a></p>
|
|
|
|
|
|
<ul>
|
|
|
<li>minimal –不能通过基础备份和wal日志恢复数据库。</li>
|
|
|
<li>replica = 9.6版本以前的archive和hot_standby –该级别支持wal归档和复制。</li>
|
|
|
<li>logical –在replica级别的基础上添加了支持逻辑解码所需的信息。</li>
|
|
|
</ul>
|
|
|
|
|
|
<p>設定完成後,重起 postgresql 就可以了</p>
|
|
|
|
|
|
<h4 id="建立-stanza">建立 stanza</h4>
|
|
|
|
|
|
<p>stanza 這名詞我是第一次聽到,直接翻譯就是 <strong>”“(詩的)節,段”</strong></p>
|
|
|
|
|
|
<p><a href="https://dictionary.cambridge.org/zht/%E8%A9%9E%E5%85%B8/%E8%8B%B1%E8%AA%9E-%E6%BC%A2%E8%AA%9E-%E7%B9%81%E9%AB%94/stanza">https://dictionary.cambridge.org/zht/%E8%A9%9E%E5%85%B8/%E8%8B%B1%E8%AA%9E-%E6%BC%A2%E8%AA%9E-%E7%B9%81%E9%AB%94/stanza</a></p>
|
|
|
|
|
|
<p>在pgbackrest中,可以一次定義多個 stanza,用來備份不同的DB
|
|
|
這次環境很簡單,所以就只有設定一個
|
|
|
依照上面的pgbackrest.conf 內容設定好了,需要先建立這個stanza
|
|
|
指令:</p>
|
|
|
|
|
|
<pre><code>postgres@hqdc039:~$ pgbackrest --stanza=demo stanza-create --log-level-console=detail
|
|
|
2019-09-04 16:21:40.700 P00 INFO: stanza-create command begin 2.16: --log-level-console=detail --pg1-path=/database/11/main --repo1-cipher-pass=<redacted> --repo1-cipher-type=aes-256-cbc --repo1-path=/var/lib/pgbackrest --stanza=demo
|
|
|
2019-09-04 16:21:41.525 P00 INFO: stanza-create command end: completed successfully (825ms)
|
|
|
postgres@hqdc039:~$
|
|
|
</code></pre>
|
|
|
|
|
|
<p>接著就可以執行備份了</p>
|
|
|
|
|
|
<h4 id="backup">backup</h4>
|
|
|
|
|
|
<p>備份指令也很簡單,要注意的是,如果不帶參數,pgbackrest 會自行決定要用incremental或者是 full backup</p>
|
|
|
|
|
|
<pre><code>postgres@hqdc039:~$ pgbackrest --stanza=demo --log-level-console=detail backup
|
|
|
2019-09-04 16:41:17.458 P00 INFO: backup command begin 2.16: --log-level-console=detail --pg1-path=/database/11/main --repo1-cipher-pass=<redacted> --repo1-cipher-type=aes-256-cbc --repo1-path=/var/lib/pgbackrest --repo1-retention-full=2 --stanza=demo
|
|
|
2019-09-04 16:41:17.697 P00 INFO: last backup label = 20190904-134245F, version = 2.16
|
|
|
2019-09-04 16:41:18.607 P00 INFO: execute non-exclusive pg_start_backup() with label "pgBackRest backup started at 2019-09-04 16:41:17": backup begins after the next regular checkpoint completes
|
|
|
2019-09-04 16:41:19.008 P00 INFO: backup start archive = 000000100000000E0000004A, lsn = E/4A000028
|
|
|
WARN: a timeline switch has occurred since the last backup, enabling delta checksum
|
|
|
2019-09-04 16:41:21.213 P01 DETAIL: match file from prior backup /database/11/main/base/51435/51488 (546.3MB, 20%) checksum 5eb4f73d9b1c535ebfdfb622d930dade87e23786
|
|
|
2019-09-04 16:41:21.821 P01 DETAIL: match file from prior backup /database/11/main/base/51435/51460 (455.3MB, 37%) checksum aa74bba2bea8823789ad4194e4574b44a020271a
|
|
|
...
|
|
|
...
|
|
|
...
|
|
|
|
|
|
2019-09-04 16:41:24.827 P01 DETAIL: match file from prior backup /database/11/main/PG_VERSION (3B, 100%) checksum dd71038f3463f511ee7403dbcbc87195302d891c
|
|
|
2019-09-04 16:41:24.835 P01 INFO: backup file /database/11/main/base/13125/51569 (0B, 100%)
|
|
|
2019-09-04 16:41:24.860 P00 INFO: incr backup size = 2.5GB
|
|
|
2019-09-04 16:41:24.860 P00 INFO: execute non-exclusive pg_stop_backup() and wait for all WAL segments to archive
|
|
|
2019-09-04 16:41:24.961 P00 INFO: backup stop archive = 000000100000000E0000004A, lsn = E/4A000130
|
|
|
2019-09-04 16:41:24.964 P00 DETAIL: wrote 'pg_data/backup_label' file returned from pg_stop_backup()
|
|
|
2019-09-04 16:41:25.155 P00 INFO: new backup label = 20190904-134245F_20190904-164117I
|
|
|
2019-09-04 16:41:25.194 P00 INFO: backup command end: completed successfully (7736ms)
|
|
|
2019-09-04 16:41:25.194 P00 INFO: expire command begin
|
|
|
2019-09-04 16:41:25.214 P00 INFO: full backup total < 2 - using oldest full backup for 11-1 archive retention
|
|
|
2019-09-04 16:41:25.214 P00 DETAIL: archive retention on backup 20190904-134245F, archiveId = 11-1, start = 0000000D0000000E00000048
|
|
|
2019-09-04 16:41:25.215 P00 DETAIL: no archive to remove, archiveId = 11-1
|
|
|
2019-09-04 16:41:25.215 P00 INFO: expire command end: completed successfully (21ms)
|
|
|
postgres@hqdc039:~$
|
|
|
</code></pre>
|
|
|
|
|
|
<p>執行後,可以檢查一下</p>
|
|
|
|
|
|
<pre><code>postgres@hqdc039:~$ pgbackrest --stanza=demo --log-level-console=info info
|
|
|
stanza: demo
|
|
|
status: ok
|
|
|
cipher: aes-256-cbc
|
|
|
|
|
|
db (current)
|
|
|
wal archive min/max (11-1): 0000000D0000000E00000048/000000100000000E0000004A
|
|
|
|
|
|
full backup: 20190904-134245F
|
|
|
timestamp start/stop: 2019-09-04 13:42:45 / 2019-09-04 13:48:54
|
|
|
wal start/stop: 0000000D0000000E00000048 / 0000000D0000000E00000048
|
|
|
database size: 2.6GB, backup size: 2.6GB
|
|
|
repository size: 547MB, repository backup size: 547MB
|
|
|
|
|
|
incr backup: 20190904-134245F_20190904-164117I
|
|
|
timestamp start/stop: 2019-09-04 16:41:17 / 2019-09-04 16:41:25
|
|
|
wal start/stop: 000000100000000E0000004A / 000000100000000E0000004A
|
|
|
database size: 2.6GB, backup size: 2.2MB
|
|
|
repository size: 547MB, repository backup size: 220.4KB
|
|
|
backup reference list: 20190904-134245F
|
|
|
postgres@hqdc039:~$
|
|
|
</code></pre>
|
|
|
|
|
|
<h4 id="還原測試">還原測試</h4>
|
|
|
|
|
|
<p>pgbackrest 的還原因為透過WAL ,所以有一些觀念需要先釐清
|
|
|
本來我的測試是先做好備份,然後直接砍掉 DB,接著再用restore還原
|
|
|
卻發現砍掉的DB居然沒有回來..
|
|
|
後來看了這一篇,然後又研究了一下 <strong>Point-in-Time Recovery</strong>
|
|
|
<a href="https://github.com/pgbackrest/pgbackrest/issues/800">https://github.com/pgbackrest/pgbackrest/issues/800</a>
|
|
|
才找到正確的用法</p>
|
|
|
|
|
|
<p>先把DB整個砍掉</p>
|
|
|
|
|
|
<pre><code>postgres@hqdc034:/zp/database$ time dropdb demo
|
|
|
</code></pre>
|
|
|
|
|
|
<p>然後停止postgresql 服務</p>
|
|
|
|
|
|
<pre><code>postgres@hqdc034:/zp/database$ pg_ctlcluster 10 main stop
|
|
|
sudo systemctl stop postgresql@10-main
|
|
|
</code></pre>
|
|
|
|
|
|
<p>接著就可以來下指令還原了</p>
|
|
|
|
|
|
<pre><code>postgres@hqdc034:/zp/database$ time pgbackrest --stanza=demo --log-level-console=info --delta --type=time "--target=2019-09-05 11:00:00.268248+08" --target-action=promote restore
|
|
|
2019-09-05 11:15:57.480 P00 INFO: restore command begin 2.13: --delta --log-level-console=info --pg1-path=/zp/database/10/main --process-max=4 --repo1-path=/var/lib/pgbackrest --stanza=demo --target="2019-09-05 11:00:00.268248+08" --target-action=promote --type=time
|
|
|
2019-09-05 11:15:57.624 P00 INFO: restore backup set 20190905-111109F
|
|
|
2019-09-05 11:15:57.947 P00 INFO: remove invalid files/paths/links from /zp/database/10/main
|
|
|
2019-09-05 11:16:00.440 P01 INFO: restore file /zp/database/10/main/global/pg_control.pgbackrest.tmp (8KB, 99%) checksum e253f9d706ac59e1ec0408ba477d1d5bac41b20f
|
|
|
2019-09-05 11:16:00.791 P00 INFO: write /zp/database/10/main/recovery.conf
|
|
|
2019-09-05 11:16:00.797 P00 INFO: restore global/pg_control (performed last to ensure aborted restores cannot be started)
|
|
|
2019-09-05 11:16:00.800 P00 INFO: restore command end: completed successfully (3321ms)
|
|
|
|
|
|
real 0m3.328s
|
|
|
user 0m6.896s
|
|
|
sys 0m0.832s
|
|
|
</code></pre>
|
|
|
|
|
|
<p>在DB目錄中,會產生一個recovery.conf,這是用來給 postgresql 看的檔案,裡面會紀錄怎麼還原、以及還原的類型、時間點,這邊指定要恢復到 <strong><em>2019-09-05 11:00:00.268248+08</em></strong>
|
|
|
這個時間格式要看資料庫的設定,可以藉由以下指令得到</p>
|
|
|
|
|
|
<pre><code>postgres@hqdc034:/zp/database$ psql -Atc "select current_timestamp"
|
|
|
2019-09-05 11:14:27.268248+08
|
|
|
</code></pre>
|
|
|
|
|
|
<p>檢查一下recovery.conf</p>
|
|
|
|
|
|
<pre><code>postgres@hqdc034:/zp/database$ cat /zp/database/10/main/recovery.conf
|
|
|
restore_command = 'pgbackrest --log-level-console=info --stanza=demo archive-get %f "%p"'
|
|
|
recovery_target_time = '2019-09-05 11:00:00.268248+08'
|
|
|
recovery_target_action = 'promote'
|
|
|
</code></pre>
|
|
|
|
|
|
<p>重新啟動postgresql 然後看看被砍掉的demo DB有沒有回來</p>
|
|
|
|
|
|
<pre><code>postgres@hqdc034:/zp/database$ pg_ctlcluster 10 main start
|
|
|
Warning: the cluster will not be running as a systemd service. Consider using systemctl:
|
|
|
sudo systemctl start postgresql@10-main
|
|
|
postgres@hqdc034:/zp/database$ psql
|
|
|
psql (10.8 (Ubuntu 10.8-1.pgdg14.04+1))
|
|
|
Type "help" for help.
|
|
|
|
|
|
postgres=# \l
|
|
|
List of databases
|
|
|
Name | Owner | Encoding | Collate | Ctype | Access privileges
|
|
|
-----------+----------+----------+-------------+-------------+-----------------------
|
|
|
demo | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
|
|
|
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
|
|
|
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
|
|
|
| | | | | postgres=CTc/postgres
|
|
|
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
|
|
|
| | | | | postgres=CTc/postgres
|
|
|
(4 rows)
|
|
|
|
|
|
postgres=# \q
|
|
|
postgres@hqdc034:/zp/database$
|
|
|
</code></pre>
|
|
|
|
|
|
<p>很好, demo DB 有順利的還原回來了,先暫時測試到這邊。接下來要來玩postgresql on zfs</p>
|
|
|
|
|
|
<p>後續如果想更深入測試 pgbackrest,可以試試看異機備份還原。</p>
|
|
|
</article>
|
|
|
</div>
|
|
|
|
|
|
<div class='entry-meta-bottom'>
|
|
|
|
|
|
|
|
|
<div class="entry-categories"><p><span>Categories</span>
|
|
|
|
|
|
<a href="/categories/%E7%AD%86%E8%A8%98" title="View all posts in 筆記">筆記</a>
|
|
|
</p>
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div class="entry-tags"><p><span>Tags</span>
|
|
|
|
|
|
<a href="/tags/postgresql" title="View all posts tagged postgresql">postgresql</a>
|
|
|
|
|
|
|
|
|
</p></div> </div>
|
|
|
|
|
|
|
|
|
<div class="author-meta">
|
|
|
|
|
|
<div class="author">
|
|
|
|
|
|
<img alt='Eric Chang' src="https://www.gravatar.com/avatar/23f8ed94e007297499ac8df1641b3ff5?s=100&d=identicon" class='avatar avatar-72 photo' height='72' width='72'>
|
|
|
|
|
|
<span>
|
|
|
Written by:<a href="https://github.com/changchichung" title="Posts by Eric Chang" rel="author">Eric Chang</a> </span>
|
|
|
</div>
|
|
|
<div class="bio">
|
|
|
|
|
|
|
|
|
<p>塵世裡一個迷途小書僮</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<a class="facebook" target="_blank"
|
|
|
href="full%20Social%20profile%20url%20in%20facebook">
|
|
|
<i class="fab fa-facebook-f"
|
|
|
title="facebook icon"></i>
|
|
|
</a>
|
|
|
|
|
|
|
|
|
|
|
|
<a class="googleplus" target="_blank"
|
|
|
href="full%20profile%20url%20in%20googleplus">
|
|
|
<i class="fab fa-google-plus-g"
|
|
|
title="googleplus icon"></i>
|
|
|
</a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<a class="twitter" target="_blank"
|
|
|
href="chang0206">
|
|
|
<i class="fab fa-twitter-square"
|
|
|
title="twitter icon"></i>
|
|
|
</a>
|
|
|
|
|
|
|
|
|
<a class="linkedin" target="_blank"
|
|
|
href="full%20profile%20url%20in%20linkedin">
|
|
|
<i class="fab fa-linkedin"
|
|
|
title="linkedin icon"></i>
|
|
|
</a>
|
|
|
|
|
|
|
|
|
|
|
|
<a class="email" target="_blank"
|
|
|
href="mailto:mc@hotshraingmy.info">
|
|
|
<i class="fas fa-envelope"
|
|
|
title="email icon"></i>
|
|
|
</a>
|
|
|
|
|
|
|
|
|
|
|
|
<a class="instagram" target="_blank"
|
|
|
href="chang0206">
|
|
|
<i class="fab fa-instagram"
|
|
|
title="instagram icon"></i>
|
|
|
</a>
|
|
|
|
|
|
|
|
|
|
|
|
<a class="stackoverflow" target="_blank"
|
|
|
href="full%20profile%20url%20in%20stackoverflow">
|
|
|
<i class="fab fa-stack-overflow"
|
|
|
title="stackoverflow icon"></i>
|
|
|
</a>
|
|
|
|
|
|
|
|
|
|
|
|
<a class="github" target="_blank"
|
|
|
href="changchichung">
|
|
|
<i class="fab fa-github"
|
|
|
title="github icon"></i>
|
|
|
</a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<a class="pinterest" target="_blank"
|
|
|
href="full%20profile%20url%20in%20pinterest">
|
|
|
<i class="fab fa-pinterest"
|
|
|
title="pinterest icon"></i>
|
|
|
</a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
<section id="comments" class="comments">
|
|
|
<div id="disqus_thread"></div>
|
|
|
<script type="application/javascript">
|
|
|
var disqus_config = function () {
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
(function() {
|
|
|
if (["localhost", "127.0.0.1"].indexOf(window.location.hostname) != -1) {
|
|
|
document.getElementById('disqus_thread').innerHTML = 'Disqus comments not available by default when the website is previewed locally.';
|
|
|
return;
|
|
|
}
|
|
|
var d = document, s = d.createElement('script'); s.async = true;
|
|
|
s.src = '//' + "h-cowbay-org-1" + '.disqus.com/embed.js';
|
|
|
s.setAttribute('data-timestamp', +new Date());
|
|
|
(d.head || d.body).appendChild(s);
|
|
|
})();
|
|
|
</script>
|
|
|
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
|
|
|
<a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<footer id="site-footer" class="site-footer" role="contentinfo">
|
|
|
<h1>
|
|
|
|
|
|
<a href=""> MC部落 </a>
|
|
|
|
|
|
</h1>
|
|
|
|
|
|
|
|
|
<p class="site-description">What’s the Worst That Could Happen?</p>
|
|
|
|
|
|
|
|
|
<div id="menu-footer" class="menu-container menu-footer" role="navigation">
|
|
|
<div class="menu">
|
|
|
|
|
|
<ul id="menu-footer-items" class="menu-footer-items">
|
|
|
|
|
|
</ul>
|
|
|
|
|
|
</div> </div>
|
|
|
|
|
|
<ul class="social-media-icons">
|
|
|
|
|
|
|
|
|
<li>
|
|
|
<a class="facebook" target="_blank"
|
|
|
href="full%20Social%20profile%20url%20in%20facebook" >
|
|
|
<i class="fab fa-facebook-f" title="facebook"></i>
|
|
|
<span class="screen-reader-text">facebook</span>
|
|
|
</a>
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
<li>
|
|
|
<a class="googleplus" target="_blank"
|
|
|
href="full%20profile%20url%20in%20googleplus" >
|
|
|
<i class="fab fa-google-plus-g" title="googleplus"></i>
|
|
|
<span class="screen-reader-text">googleplus</span>
|
|
|
</a>
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li>
|
|
|
<a href="chang0206" class="twitter" target="_blank">
|
|
|
<i class="fab fa-twitter-square" title="twitter"></i>
|
|
|
<span class="screen-reader-text">twitter</span>
|
|
|
</a>
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
<li>
|
|
|
<a href="chang0206" class="instagram" target="_blank">
|
|
|
<i class="fab fa-instagram" title="instagram"></i>
|
|
|
<span class="screen-reader-text">instagram</span>
|
|
|
</a>
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
<li>
|
|
|
<a href="mailto:mc@hotshraingmy.info" class="email">
|
|
|
<i class="fas fa-envelope" title="email"></i>
|
|
|
<span class="screen-reader-text">email</span>
|
|
|
</a>
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
<li>
|
|
|
<a href="full%20profile%20url%20in%20linkedin" class="linkedin" target="_blank">
|
|
|
<i class="fab fa-linkedin-in" title="linkedin"></i>
|
|
|
<span class="screen-reader-text">linkedin</span>
|
|
|
</a>
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
<li>
|
|
|
<a href="full%20profile%20url%20in%20stackoverflow" class="stackoverflow" target="_blank">
|
|
|
<i class="fab fa-stack-overflow" title="stackoverflow"></i>
|
|
|
<span class="screen-reader-text">stackoverflow</span>
|
|
|
</a>
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li>
|
|
|
<a href="changchichung" class="github" target="_blank">
|
|
|
<i class="fab fa-github" title="github"></i>
|
|
|
<span class="screen-reader-text">github</span>
|
|
|
</a>
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<li>
|
|
|
<a href="full%20profile%20url%20in%20pinterest" class="pinterest" target="_blank">
|
|
|
<i class="fab fa-pinterest" title="pinterest"></i>
|
|
|
<span class="screen-reader-text">pinterest</span>
|
|
|
</a>
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
|
|
<li>
|
|
|
<a href="https://h.cowbay.org/index.xml" data-animate-hover="pulse" class="rss" target="_blank">
|
|
|
<i class="fas fa-rss" title="rss"></i>
|
|
|
<span class="screen-reader-text">rss</span>
|
|
|
</a>
|
|
|
</li>
|
|
|
|
|
|
|
|
|
</ul> <div class="design-credit">
|
|
|
|
|
|
<p>© 2018 Göran Svensson</p>
|
|
|
|
|
|
<p>Nederburg Hugo Theme by <a href="https://appernetic.io">Appernetic</a>.</p>
|
|
|
|
|
|
<p>A port of Tracks by Compete Themes.</p>
|
|
|
|
|
|
</div>
|
|
|
</footer>
|
|
|
|
|
|
</div>
|
|
|
<script src="https://h.cowbay.org/js/jquery.min.js"></script>
|
|
|
<script src="https://h.cowbay.org/js/jquerymigrate.js"></script>
|
|
|
<script src="https://h.cowbay.org/js/production.min.js?v=1624607940"></script>
|
|
|
|
|
|
</body>
|
|
|
</html>
|