180727-時序數據庫InfluxDB之備份和恢復策略


logo

influxdb 備份與恢復

參考: influxdb backup and restore

環境:

  • influxdb v1.6.0
  • 使用influx自動的控制台進行

I. 備份

備份命令

influxd backup
    [ -database <db_name> ]  --> 指定需要備份的數據庫名
    [ -portable ]            --> 表示在線備份
    [ -host <host:port> ]    --> influxdb服務所在的機器,端口號默認為8088
    [ -retention <rp_name> ] | [ -shard <shard_ID> -retention <rp_name> ]  --> 備份的保留策略,注意shard是掛在rp下的;我們需要備份的就是shard中的數據
    [ -start <timestamp> [ -end <timestamp> ] | -since <timestamp> ]   --> 備份指定時間段的數據
    <path-to-backup>   --> 備份文件的輸出地址

1. 實例演示

首先創建一個數據庫 yhhblog, 里面包含兩個measurement,對應的數據如下

> show databases
name: databases
name
----
_internal
yhhblog

> use yhhblog
Using database yhhblog
> show measurements
name: measurements
name
----
netLoad
serviceLoad

> select * from netLoad
name: netLoad
time                host      netIn netOut service
----                ----      ----- ------ -------
1532658769048100401 127.0.0.1 13m   521K   app.service.about

> select * from serviceLoad
name: serviceLoad
time                cpu   host      load mem   qps  rt   service
----                ---   ----      ---- ---   ---  --   -------
1532658713805369067 45.23 127.0.0.2 1.21 4145m 1341 1312 app.service.about
1532658718726259226 45.23 127.0.0.1 1.21 4145m 1341 1312 app.service.about

a. 備份所有的數據庫

將influxdb中的所有的數據庫都備份下來,不加任何的參數

influxd backup -portable /tmp/data/total

b. 備份指定數據庫

如果只想要備份上面的yhhblog數據庫, 添加 -database 參數指定即可

# influxd backup -portable -database yhhblog /tmp/data/yhhblog

2018/07/27 10:38:15 backing up metastore to /tmp/data/yhhblog/meta.00
2018/07/27 10:38:15 backing up db=yhhblog
2018/07/27 10:38:15 backing up db=yhhblog rp=autogen shard=10 to /tmp/data/yhhblog/yhhblog.autogen.00010.00 since 0001-01-01T00:00:00Z
2018/07/27 10:38:15 backup complete:
2018/07/27 10:38:15 	/tmp/data/yhhblog/20180727T023815Z.meta
2018/07/27 10:38:15 	/tmp/data/yhhblog/20180727T023815Z.s10.tar.gz
2018/07/27 10:38:15 	/tmp/data/yhhblog/20180727T023815Z.manifest

c. 備份數據庫中指定時間段的數據

對上面的數據,只備份部分時間滿足要求的數據,可以添加start/end參數

# influxd backup -portable -database yhhblog -start 2018-07-27T2:31:57Z -end 2018-07-27T2:32:59Z  /tmp/data/yhhblog_per

2018/07/27 10:42:14 backing up metastore to /tmp/data/yhhblog_per/meta.00
2018/07/27 10:42:14 backing up db=yhhblog
2018/07/27 10:42:14 backing up db=yhhblog rp=autogen shard=10 to /tmp/data/yhhblog_per/yhhblog.autogen.00010.00 with boundaries start=2018-07-27T02:31:57Z, end=2018-07-27T02:32:59Z
2018/07/27 10:42:14 backup complete:
2018/07/27 10:42:14 	/tmp/data/yhhblog_per/20180727T024214Z.meta
2018/07/27 10:42:14 	/tmp/data/yhhblog_per/20180727T024214Z.s10.tar.gz
2018/07/27 10:42:14 	/tmp/data/yhhblog_per/20180727T024214Z.manifest

現在備份ok了,問題就是如何確認備份的問題有沒有問題呢,備份后的數據如何恢復呢?

II. 恢復

命令如下

influxd restore 
    [ -db <db_name> ]       --> 待恢復的數據庫(備份中的數據庫名)
    -portable | -online
    [ -host <host:port> ]    --> influxdb 的服務器
    [ -newdb <newdb_name> ]  --> 恢復到influxdb中的數據庫名
    [ -rp <rp_name> ]        --> 備份中的保留策略
    [ -newrp <newrp_name> ]  --> 恢復的保留策略
    [ -shard <shard_ID> ]
    <path-to-backup-files>

首先拿簡單的方式來演示恢復策略,並查看下上面的備份數據是否有問題

1. 恢復到不存在的database

下面演示下將前面的導出的備份,恢復到一個新的數據庫 yhhblog_bk上,執行命令如下

influxd restore -portable -db yhhblog -newdb yhhblog_bk yhhblog_per

順帶驗證下上面備份的數據是否有問題,注意到我們恢復的是時間片段的數據備份,因此恢復的數據,應該會排除掉不再上面日期內的數據

> show databases
name: databases
name
----
_internal
yhhblog
yhhblog_bk
> use yhhblog_bk
Using database yhhblog_bk
> show measurements
name: measurements
name
----
netLoad
serviceLoad
> select * from netLoad
name: netLoad
time                host      netIn netOut service
----                ----      ----- ------ -------
1532658769048100401 127.0.0.1 13m   521K   app.service.about
> select * from serviceLoad
name: serviceLoad
time                cpu   host      load mem   qps  rt   service
----                ---   ----      ---- ---   ---  --   -------
1532658718726259226 45.23 127.0.0.1 1.21 4145m 1341 1312 app.service.about

注意看前面serviceLoad里面只有一條數據, 即表明我們按照時間進行備份沒啥問題

2. 恢復到存在的DB

看官網恢復的文檔中,如果想將備份恢復到一個已經存在的database中時,並不是上面那么簡單的就可以了,這里采用的一個策略是西安備份到一個臨時的db中;然后將臨時DB中的數據寫入已存在的db中

具體的演示步驟如下 (注意本小結的執行可以直接依賴前面恢復的備份數據庫中)

將備份恢復到已經存在的數據庫 yhhblogNew 中

# 首先是將備份恢復到一個不存在的數據庫 yhhblog_bk 中
influxd restore -portable -db yhhblog -newdb yhhblog_bk yhhblog_per

進入 influx 控制台,執行拷貝和刪除臨時數據庫

# 准備 yhhblogNew 數據庫
> create database yhhblogNew

# 將臨時數據庫中的數據導入已存在的數據庫中
> use yhhblog_bk
> SELECT * INTO yhhblogNew..:MEASUREMENT FROM /.*/ GROUP BY *
> drop yhhblog_bk

3. 保留策略已存在時,恢復

influxd restore -portable -db yhhblog -newdb yhhblog_tmp -rp autogen -newrp autogen_tmp  yhhblog

進入influx控制台,執行拷貝

> user yhhblog_tmp
> SELECT * INTO yhhblogNew.autogen.:MEASUREMENT FROM /yhhblog_tmp.autogen_tmp.*/ GROUP BY *
> drop database yhhblog_tmp

4. 其他

官方還寫了其他兩種恢復方式,一個被廢棄,一個離線的會導致數據丟失,也不推薦使用,而現在大部分的博文中備份和恢復都是這種過時的方案,不太友好,這里不詳細敘述

III. 其他

1. 一灰灰Bloghttps://liuyueyi.github.io/hexblog

一灰灰的個人博客,記錄所有學習和工作中的博文,歡迎大家前去逛逛

2. 聲明

盡信書則不如,已上內容,純屬一家之言,因個人能力有限,難免有疏漏和錯誤之處,如發現bug或者有更好的建議,歡迎批評指正,不吝感激

3. 掃描關注

小灰灰Blog&公眾號

QrCode

知識星球

zhishi


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM