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

logo

influxdb 備份與恢復

參考: influxdb backup and restoregit

環境:github

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

I. 備份

備份命令sql

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 參數指定便可app

# 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了,問題就是如何確認備份的問題有沒有問題呢,備份後的數據如何恢復呢?rest

II. 恢復

命令以下code

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>

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

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

相關文章
相關標籤/搜索