|
|
<!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" : "[筆記] 用zfs的snapshot 快照功能來做 postgresql 的備份還原 \/ Postgresql Backup Restore Using Zfs Snapshot",
|
|
|
"headline" : "[筆記] 用zfs的snapshot 快照功能來做 postgresql 的備份還原 \/ Postgresql Backup Restore Using Zfs Snapshot",
|
|
|
"description" : "\x3cp\x3e前面測試了用pgbarman \/ pgbackrest 來備份 postgresql\x3c\/p\x3e\n\n\x3cp\x3e這次改從system file level 來下手\x3c\/p\x3e\n\n\x3cp\x3e採用zfs 的快照來備份、還原postgresql 資料庫\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-06 10:42:11 \x2b0800 CST",
|
|
|
"dateModified" : "2019-09-06 10:42:11 \x2b0800 CST",
|
|
|
"url" : "https:\/\/h.cowbay.org\/post\/postgresql-backup-restore-using-zfs-snapshot\/",
|
|
|
"wordCount" : "1339",
|
|
|
"image" : "https://h.cowbay.orghttps://h.cowbay.org/images/post-default-5.jpg"",
|
|
|
"keywords" : [ ""postgresql"",""zfs"",""backup"",""restore"","Blog" ]
|
|
|
}
|
|
|
</script>
|
|
|
|
|
|
|
|
|
<title>[筆記] 用zfs的snapshot 快照功能來做 postgresql 的備份還原 / Postgresql Backup Restore Using Zfs Snapshot </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=1586334318" rel="stylesheet" id="theme-stylesheet" type='text/css' media='all'>
|
|
|
|
|
|
<link href="https://h.cowbay.org/css/custom.css?v=1586334318" 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-5.jpg">
|
|
|
</div>
|
|
|
|
|
|
<div class="entry-meta">
|
|
|
<span class="date">06 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'> [筆記] 用zfs的snapshot 快照功能來做 postgresql 的備份還原 / Postgresql Backup Restore Using Zfs Snapshot</h1>
|
|
|
</div>
|
|
|
<div class="entry-container">
|
|
|
<div class="entry-content">
|
|
|
<article>
|
|
|
<p>前面測試了用pgbarman / pgbackrest 來備份 postgresql</p>
|
|
|
|
|
|
<p>這次改從system file level 來下手</p>
|
|
|
|
|
|
<p>採用zfs 的快照來備份、還原postgresql 資料庫</p>
|
|
|
|
|
|
<h3 id="建立測試資料庫-table-snapshot">建立測試資料庫、TABLE、snapshot</h3>
|
|
|
|
|
|
<h4 id="資料庫現況">資料庫現況</h4>
|
|
|
|
|
|
<p>只有系統預設的DB,沒有其他多的東西</p>
|
|
|
|
|
|
<pre><code>postgres@hqdc034:~$ psql -c '\l'
|
|
|
List of databases
|
|
|
Name | Owner | Encoding | Collate | Ctype | Access privileges
|
|
|
-----------+----------+----------+-------------+-------------+-----------------------
|
|
|
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
|
|
|
(3 rows)
|
|
|
|
|
|
postgres@hqdc034:~$ du -sh /zp/database/10/main/
|
|
|
232M /zp/database/10/main/
|
|
|
</code></pre>
|
|
|
|
|
|
<h4 id="建立第一次的快照">建立第一次的快照</h4>
|
|
|
|
|
|
<pre><code>2019-09-06 09:03:46 [changch@hqdc034 ~]$ sudo zfs list -t snapshot
|
|
|
no datasets available
|
|
|
2019-09-06 09:03:53 [changch@hqdc034 ~]$ sudo zfs snapshot zp/database@init_db_no_demo
|
|
|
2019-09-06 09:04:09 [changch@hqdc034 ~]$ sudo zfs list -t snapshot
|
|
|
NAME USED AVAIL REFER MOUNTPOINT
|
|
|
zp/database@init_db_no_demo 0 - 231M -
|
|
|
2019-09-06 09:04:15 [changch@hqdc034 ~]$
|
|
|
</code></pre>
|
|
|
|
|
|
<h4 id="建立-倒回測試資料庫-demo">建立、倒回測試資料庫 demo</h4>
|
|
|
|
|
|
<pre><code>postgres@hqdc034:~$ createdb demo
|
|
|
postgres@hqdc034:~$ psql demo < /home/changch/Downloads/demo.sql
|
|
|
SET
|
|
|
SET
|
|
|
略...
|
|
|
</code></pre>
|
|
|
|
|
|
<p>再檢查一次資料庫的狀況,看到 demo DB出現了,資料庫目錄也變大了</p>
|
|
|
|
|
|
<pre><code>postgres@hqdc034:~$ psql -c '\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@hqdc034:~$ du -sh /zp/database/10/main/
|
|
|
2.1G /zp/database/10/main/
|
|
|
postgres@hqdc034:~$
|
|
|
</code></pre>
|
|
|
|
|
|
<h4 id="建立第二次快照">建立第二次快照</h4>
|
|
|
|
|
|
<p>這次的快照,將包含剛剛倒回的 demo DB,但是不包含等下才要建立的測試 table</p>
|
|
|
|
|
|
<pre><code>2019-09-06 09:16:01 [changch@hqdc034 ~]$ sudo zfs list -t snapshot
|
|
|
NAME USED AVAIL REFER MOUNTPOINT
|
|
|
zp/database@init_db_no_demo 250K - 231M -
|
|
|
zp/database@demo_db_just_restore 0 - 2.08G -
|
|
|
2019-09-06 09:16:04 [changch@hqdc034 ~]$
|
|
|
</code></pre>
|
|
|
|
|
|
<h4 id="建立測試-table">建立測試 table</h4>
|
|
|
|
|
|
<pre><code>postgres@hqdc034:~$ psql -c 'create table test ( a int, b varchar(50) );'
|
|
|
CREATE TABLE
|
|
|
postgres@hqdc034:~$
|
|
|
</code></pre>
|
|
|
|
|
|
<h4 id="建立第三次快照">建立第三次快照</h4>
|
|
|
|
|
|
<p>這次快照,只有建立 test table ,但是裡面沒有資料</p>
|
|
|
|
|
|
<pre><code>2019-09-06 09:18:34 [changch@hqdc034 ~]$ sudo zfs list -t snapshot
|
|
|
NAME USED AVAIL REFER MOUNTPOINT
|
|
|
zp/database@init_db_no_demo 250K - 231M -
|
|
|
zp/database@demo_db_just_restore 138K - 2.08G -
|
|
|
zp/database@demo_db_create_test_table_but_no_data 0 - 2.08G -
|
|
|
2019-09-06 09:18:36 [changch@hqdc034 ~]$
|
|
|
</code></pre>
|
|
|
|
|
|
<h4 id="在test-table-插入100萬筆資料">在test table 插入100萬筆資料</h4>
|
|
|
|
|
|
<pre><code>postgres@hqdc034:~$ psql -c 'with aa as ( select * from generate_series (1,1000000) a ) insert into test select aa.a, md5(aa.a::varchar) from aa;'
|
|
|
INSERT 0 1000000
|
|
|
postgres@hqdc034:~$ psql -c 'select count(*) from test;'
|
|
|
count
|
|
|
---------
|
|
|
1000000
|
|
|
(1 row)
|
|
|
|
|
|
postgres@hqdc034:~$
|
|
|
</code></pre>
|
|
|
|
|
|
<h4 id="建立第四次快照">建立第四次快照</h4>
|
|
|
|
|
|
<p>test table 內有 1000000 筆資料</p>
|
|
|
|
|
|
<pre><code>2019-09-06 09:18:36 [changch@hqdc034 ~]$ sudo zfs snapshot zp/database@demo_db_test_table_with_1M_rows
|
|
|
2019-09-06 09:21:08 [changch@hqdc034 ~]$ sudo zfs list -t snapshot
|
|
|
NAME USED AVAIL REFER MOUNTPOINT
|
|
|
zp/database@init_db_no_demo 250K - 231M -
|
|
|
zp/database@demo_db_just_restore 138K - 2.08G -
|
|
|
zp/database@demo_db_create_test_table_but_no_data 116K - 2.08G -
|
|
|
zp/database@demo_db_test_table_with_1M_rows 0 - 2.15G -
|
|
|
2019-09-06 09:21:09 [changch@hqdc034 ~]$
|
|
|
</code></pre>
|
|
|
|
|
|
<h4 id="再次插入-100萬筆資料">再次插入 100萬筆資料</h4>
|
|
|
|
|
|
<pre><code>postgres@hqdc034:~$ time psql -c 'with aa as ( select * from generate_series (1,1000000) a ) insert into test select aa.a, md5(aa.a::varchar) from aa;'
|
|
|
INSERT 0 1000000
|
|
|
|
|
|
real 0m4.276s
|
|
|
user 0m0.020s
|
|
|
sys 0m0.012s
|
|
|
postgres@hqdc034:~$ psql -c 'select count(*) from test;'
|
|
|
count
|
|
|
---------
|
|
|
2000000
|
|
|
(1 row)
|
|
|
|
|
|
postgres@hqdc034:~$
|
|
|
</code></pre>
|
|
|
|
|
|
<h4 id="建立第五次快照">建立第五次快照</h4>
|
|
|
|
|
|
<p>現在 test table 有 200萬筆資料了</p>
|
|
|
|
|
|
<pre><code>2019-09-06 09:21:09 [changch@hqdc034 ~]$ sudo zfs snapshot zp/database@demo_db_test_table_with_2M_rows
|
|
|
2019-09-06 09:22:29 [changch@hqdc034 ~]$ sudo zfs list -t snapshot
|
|
|
NAME USED AVAIL REFER MOUNTPOINT
|
|
|
zp/database@init_db_no_demo 250K - 231M -
|
|
|
zp/database@demo_db_just_restore 138K - 2.08G -
|
|
|
zp/database@demo_db_create_test_table_but_no_data 116K - 2.08G -
|
|
|
zp/database@demo_db_test_table_with_1M_rows 218K - 2.15G -
|
|
|
zp/database@demo_db_test_table_with_2M_rows 0 - 2.23G -
|
|
|
2019-09-06 09:22:30 [changch@hqdc034 ~]$
|
|
|
</code></pre>
|
|
|
|
|
|
<h4 id="玩大點-直接湊滿1000萬筆資料好了">玩大點,直接湊滿1000萬筆資料好了</h4>
|
|
|
|
|
|
<pre><code>postgres@hqdc034:~$ time psql -c 'with aa as ( select * from generate_series (1,8000000) a ) insert into test select aa.a, md5(aa.a::varchar) from aa;'
|
|
|
INSERT 0 8000000
|
|
|
|
|
|
real 0m32.172s
|
|
|
user 0m0.024s
|
|
|
sys 0m0.008s
|
|
|
postgres@hqdc034:~$ psql -c 'select count(*) from test;'
|
|
|
count
|
|
|
----------
|
|
|
10000000
|
|
|
(1 row)
|
|
|
postgres@hqdc034:~$
|
|
|
</code></pre>
|
|
|
|
|
|
<h4 id="建立第六次快照">建立第六次快照</h4>
|
|
|
|
|
|
<p>10M rows in test table</p>
|
|
|
|
|
|
<pre><code>2019-09-06 09:22:30 [changch@hqdc034 ~]$ sudo zfs snapshot zp/database@demo_db_test_table_with_10M_rows
|
|
|
2019-09-06 09:25:18 [changch@hqdc034 ~]$ sudo zfs list -t snapshot
|
|
|
NAME USED AVAIL REFER MOUNTPOINT
|
|
|
zp/database@init_db_no_demo 250K - 231M -
|
|
|
zp/database@demo_db_just_restore 138K - 2.08G -
|
|
|
zp/database@demo_db_create_test_table_but_no_data 116K - 2.08G -
|
|
|
zp/database@demo_db_test_table_with_1M_rows 218K - 2.15G -
|
|
|
zp/database@demo_db_test_table_with_2M_rows 530K - 2.23G -
|
|
|
zp/database@demo_db_test_table_with_10M_rows 163K - 2.97G -
|
|
|
2019-09-06 09:25:21 [changch@hqdc034 ~]$
|
|
|
</code></pre>
|
|
|
|
|
|
<p>到1000萬筆資料為止,現在資料庫大小是這樣</p>
|
|
|
|
|
|
<pre><code>postgres@hqdc034:~$ du -sh /zp/database/10/main/
|
|
|
3.0G /zp/database/10/main/
|
|
|
postgres@hqdc034:~$
|
|
|
</code></pre>
|
|
|
|
|
|
<hr />
|
|
|
|
|
|
<h3 id="還原測試">還原測試</h3>
|
|
|
|
|
|
<p>最後一次做快照的時候,demo DB 裡面有一千萬筆資料,現在來砍掉500萬筆</p>
|
|
|
|
|
|
<pre><code>postgres@hqdc034:~$ time psql -c 'delete from test where a > 5000000;'
|
|
|
DELETE 3000000
|
|
|
|
|
|
real 0m7.844s
|
|
|
user 0m0.024s
|
|
|
sys 0m0.004s
|
|
|
|
|
|
postgres@hqdc034:~$ time psql -c 'select count(*) from test;'
|
|
|
count
|
|
|
---------
|
|
|
7000000
|
|
|
(1 row)
|
|
|
|
|
|
|
|
|
real 0m0.268s
|
|
|
user 0m0.024s
|
|
|
sys 0m0.004s
|
|
|
postgres@hqdc034:~$
|
|
|
</code></pre>
|
|
|
|
|
|
<p>咦,怪怪的,為什麼只有砍掉300萬筆?
|
|
|
好,這邊先不管,等等正好來驗證restore的狀況</p>
|
|
|
|
|
|
<p>假設剛剛這個刪除是錯誤的動作,我要回到1000萬資料的狀態,就可以用zfs rollback 來達成</p>
|
|
|
|
|
|
<h4 id="第一次還原">第一次還原</h4>
|
|
|
|
|
|
<p>目標是還原到包含1000萬筆資料的狀態(現在是700萬筆)</p>
|
|
|
|
|
|
<pre><code>2019-09-06 09:25:21 [changch@hqdc034 ~]$ sudo service postgresql stop
|
|
|
* Stopping PostgreSQL 10 database server [ OK ]
|
|
|
2019-09-06 10:14:12 [changch@hqdc034 ~]$ sudo zfs rollback -r zp/database@demo_db_test_table_with_10M_rows
|
|
|
2019-09-06 10:14:28 [changch@hqdc034 ~]$ sudo service postgresql start
|
|
|
* Starting PostgreSQL 10 database server [ OK ]
|
|
|
2019-09-06 10:14:57 [changch@hqdc034 ~]$
|
|
|
</code></pre>
|
|
|
|
|
|
<p>檢查一下</p>
|
|
|
|
|
|
<pre><code>postgres@hqdc034:~$ time psql -c 'select count(*) from test;'
|
|
|
count
|
|
|
----------
|
|
|
10000000
|
|
|
(1 row)
|
|
|
|
|
|
|
|
|
real 0m5.019s
|
|
|
user 0m0.040s
|
|
|
sys 0m0.008s
|
|
|
postgres@hqdc034:~$
|
|
|
</code></pre>
|
|
|
|
|
|
<p>沒錯,又回到1000萬筆資料的狀態了</p>
|
|
|
|
|
|
<p>要注意的是,如果回到更之前的狀態,在該狀態之後的快照將會被清除,除非你先做clone
|
|
|
比如我現在要回到 200萬筆的狀態,那1000萬筆資料的快照就會被刪除</p>
|
|
|
|
|
|
<pre><code>2019-09-06 10:17:32 [changch@hqdc034 ~]$ sudo service postgresql stop
|
|
|
* Stopping PostgreSQL 10 database server [ OK ]
|
|
|
2019-09-06 10:18:50 [changch@hqdc034 ~]$ sudo zfs rollback -r zp/database@demo_db_test_table_with_2M_rows
|
|
|
2019-09-06 10:18:57 [changch@hqdc034 ~]$ sudo zfs list -t snapshot
|
|
|
NAME USED AVAIL REFER MOUNTPOINT
|
|
|
zp/database@init_db_no_demo 250K - 231M -
|
|
|
zp/database@demo_db_just_restore 138K - 2.08G -
|
|
|
zp/database@demo_db_create_test_table_but_no_data 116K - 2.08G -
|
|
|
zp/database@demo_db_test_table_with_1M_rows 218K - 2.15G -
|
|
|
zp/database@demo_db_test_table_with_2M_rows 0 - 2.23G -
|
|
|
2019-09-06 10:19:04 [changch@hqdc034 ~]$ sudo service postgresql start
|
|
|
* Starting PostgreSQL 10 database server [ OK ]
|
|
|
2019-09-06 10:19:17 [changch@hqdc034 ~]$
|
|
|
|
|
|
postgres@hqdc034:~$ time psql -c 'select count(*) from test;'
|
|
|
count
|
|
|
---------
|
|
|
2000000
|
|
|
(1 row)
|
|
|
|
|
|
real 0m0.175s
|
|
|
user 0m0.024s
|
|
|
sys 0m0.008s
|
|
|
postgres@hqdc034:~$
|
|
|
</code></pre>
|
|
|
|
|
|
<p>啊,我剛剛應該先clone的….
|
|
|
沒關係,我們再做一次,新增800萬筆資料,湊齊1000萬筆,然後快照</p>
|
|
|
|
|
|
<pre><code>postgres@hqdc034:~$ time psql -c 'with aa as ( select * from generate_series (1,8000000) a ) insert into test select aa.a, md5(aa.a::varchar) from aa;'
|
|
|
INSERT 0 8000000
|
|
|
|
|
|
real 0m35.662s
|
|
|
user 0m0.048s
|
|
|
sys 0m0.004s
|
|
|
postgres@hqdc034:~$ time psql -c 'select count(*) from test;'
|
|
|
count
|
|
|
----------
|
|
|
10000000
|
|
|
(1 row)
|
|
|
|
|
|
real 0m5.259s
|
|
|
user 0m0.024s
|
|
|
sys 0m0.008s
|
|
|
postgres@hqdc034:~$
|
|
|
</code></pre>
|
|
|
|
|
|
<p>做快照</p>
|
|
|
|
|
|
<pre><code>2019-09-06 10:19:17 [changch@hqdc034 ~]$ sudo zfs snapshot zp/database@demo_db_test_table_with_10M_rows
|
|
|
2019-09-06 10:22:59 [changch@hqdc034 ~]$ sudo zfs list -t snapshot
|
|
|
NAME USED AVAIL REFER MOUNTPOINT
|
|
|
zp/database@init_db_no_demo 250K - 231M -
|
|
|
zp/database@demo_db_just_restore 138K - 2.08G -
|
|
|
zp/database@demo_db_create_test_table_but_no_data 116K - 2.08G -
|
|
|
zp/database@demo_db_test_table_with_1M_rows 218K - 2.15G -
|
|
|
zp/database@demo_db_test_table_with_2M_rows 56.4M - 2.23G -
|
|
|
zp/database@demo_db_test_table_with_10M_rows 0 - 1.81G -
|
|
|
2019-09-06 10:23:02 [changch@hqdc034 ~]$
|
|
|
</code></pre>
|
|
|
|
|
|
<p>接著來測試看看 clone snapshot,這是基本的說明</p>
|
|
|
|
|
|
<pre><code>Clones can only be created from a snapshot and a snapshot can not
|
|
|
be deleted until you delete the clone that is based on this snapshot.
|
|
|
To create a clone, use the zfs clone command.
|
|
|
</code></pre>
|
|
|
|
|
|
<p>clone 會做出一份跟clone來源一模一樣的資料,在快照模式下,資料是唯讀的,clone出來後,就可以做異動。但是不能刪除clone來源的快照,會提示錯誤。</p>
|
|
|
|
|
|
<pre><code>2019-09-06 10:28:31 [changch@hqdc034 ~]$ sudo zfs clone zp/database@demo_db_test_table_with_10M_rows zp/database/clone_with_10M_rows
|
|
|
2019-09-06 10:29:21 [changch@hqdc034 ~]$ sudo zfs list
|
|
|
NAME USED AVAIL REFER MOUNTPOINT
|
|
|
zp 3.08G 231G 22K /zp
|
|
|
zp/database 3.08G 231G 1.88G /zp/database
|
|
|
zp/database/clone_with_10M_rows 0 231G 1.81G /zp/database/clone_with_10M_rows
|
|
|
2019-09-06 10:29:26 [changch@hqdc034 ~]$
|
|
|
</code></pre>
|
|
|
|
|
|
<p>可以看到做了clone之後,多了一個 zfs dataset
|
|
|
試試看把資料庫路徑直接改到這個新做的dataset 看看能不能啟動資料庫
|
|
|
修改 /etc/postgresql/10/main/postgresql.conf,然後重起postgresql</p>
|
|
|
|
|
|
<pre><code>#data_directory = '/var/lib/postgresql/10/main' # use data in another directory
|
|
|
#data_directory = '/zp/database/10/main'
|
|
|
data_directory = '/zp/database/clone_with_10M_rows/10/main'
|
|
|
</code></pre>
|
|
|
|
|
|
<p><strong>啟動有比較久一點</strong> 而且好像沒成功啟動</p>
|
|
|
|
|
|
<pre><code>2019-09-06 10:32:27 [changch@hqdc034 ~]$ sudo service postgresql restart
|
|
|
* Restarting PostgreSQL 10 database server [ OK ]
|
|
|
2019-09-06 10:33:37 [changch@hqdc034 ~]$
|
|
|
2019-09-06 10:33:37 [changch@hqdc034 ~]$ sudo netstat -antlp |grep 5432
|
|
|
</code></pre>
|
|
|
|
|
|
<p>而且在 syslog & postgresql log 中看不到什麼異常,怪了!
|
|
|
而且再啟動一次就好了?</p>
|
|
|
|
|
|
<p>再來測試一次看看</p>
|
|
|
|
|
|
<pre><code>2019-09-06 10:37:22 [changch@hqdc034 ~]$ sudo service postgresql stop
|
|
|
* Stopping PostgreSQL 10 database server [ OK ]
|
|
|
2019-09-06 10:38:03 [changch@hqdc034 ~]$ sudo zfs list
|
|
|
NAME USED AVAIL REFER MOUNTPOINT
|
|
|
zp 3.24G 231G 22K /zp
|
|
|
zp/database 3.24G 231G 1.88G /zp/database
|
|
|
zp/database/clone_with_10M_rows 165M 231G 1.88G /zp/database/clone_with_10M_rows
|
|
|
2019-09-06 10:38:13 [changch@hqdc034 ~]$ sudo zfs destroy zp/database/clone_with_10M_rows
|
|
|
2019-09-06 10:38:21 [changch@hqdc034 ~]$ sudo zfs clone zp/database@demo_db_test_table_with_10M_rows zp/database/clone_with_10M_rows
|
|
|
2019-09-06 10:38:32 [changch@hqdc034 ~]$ sudo zfs list
|
|
|
NAME USED AVAIL REFER MOUNTPOINT
|
|
|
zp 3.08G 231G 22K /zp
|
|
|
zp/database 3.08G 231G 1.88G /zp/database
|
|
|
zp/database/clone_with_10M_rows 0 231G 1.81G /zp/database/clone_with_10M_rows
|
|
|
2019-09-06 10:38:35 [changch@hqdc034 ~]$ sudo service postgresql start^C
|
|
|
2019-09-06 10:38:44 [changch@hqdc034 ~]$ sudo zfs list
|
|
|
NAME USED AVAIL REFER MOUNTPOINT
|
|
|
zp 3.08G 231G 22K /zp
|
|
|
zp/database 3.08G 231G 1.88G /zp/database
|
|
|
zp/database/clone_with_10M_rows 0 231G 1.81G /zp/database/clone_with_10M_rows
|
|
|
2019-09-06 10:38:45 [changch@hqdc034 ~]$ sudo service postgresql start
|
|
|
* Starting PostgreSQL 10 database server [ OK ]
|
|
|
2019-09-06 10:39:04 [changch@hqdc034 ~]$ netstat -antlp |grep 5432
|
|
|
(Not all processes could be identified, non-owned process info
|
|
|
will not be shown, you would have to be root to see it all.)
|
|
|
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN -
|
|
|
tcp6 0 0 :::5432 :::* LISTEN -
|
|
|
2019-09-06 10:39:13 [changch@hqdc034 ~]$
|
|
|
</code></pre>
|
|
|
|
|
|
<p>這次就沒問題?看來是我第一次下指令的時候,不該用sudo netstat -antlp 去檢查?
|
|
|
anyway ,回到psql 來看看內容</p>
|
|
|
|
|
|
<pre><code>postgres@hqdc034:~$ time psql -c 'select count(*) from test;'
|
|
|
count
|
|
|
----------
|
|
|
10000000
|
|
|
(1 row)
|
|
|
|
|
|
|
|
|
real 0m4.716s
|
|
|
user 0m0.028s
|
|
|
sys 0m0.004s
|
|
|
postgres@hqdc034:~$
|
|
|
</code></pre>
|
|
|
|
|
|
<p>Good ,clone 出來的果然是1000萬筆資料時的狀態</p>
|
|
|
|
|
|
<hr />
|
|
|
|
|
|
<p>這次測試就先到此為止,後面再來測試zfs的 replication and send/recv</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>
|
|
|
|
|
|
<a href="/tags/zfs" title="View all posts tagged zfs">zfs</a>
|
|
|
|
|
|
<a href="/tags/backup" title="View all posts tagged backup">backup</a>
|
|
|
|
|
|
<a href="/tags/restore" title="View all posts tagged restore">restore</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=1586334318"></script>
|
|
|
|
|
|
</body>
|
|
|
</html>
|