時序資料庫InfluxDB之備份和恢復策略

一灰灰發表於2019-03-04

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. 一灰灰Blog: https://liuyueyi.github.io/hexblog

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

2. 宣告

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

3. 掃描關注

小灰灰Blog&公眾號

QrCode

知識星球

zhishi

相關文章