Hdfs數據備份
一、概述
本文的hdfs數據備份是在兩個集群之間進行的,如果使用snapshot在同一個集群上做備份,如果datanode損壞或誤操作清空了數據,這樣的備份就無法完全保證數據安全性。所以選擇將hdfs里面的數據備份到另外的地方進行存儲,選擇hadoop的分布式復制工具distcp。將集群的數據備份到一個制作備份使用的集群,不要怕浪費資源,因為只是做備份使用,所以配置不要求太高,並且可以只是用一個節點接收數據。平常的話此服務器可以跑其他的任務,只有在備份的時間周期內才會有備份任務。至於備份任務的周期可以自己根據實際情況以及可以接受丟失數據時間來確定備份計划任務,我這里采用的是每天備份一次,盡量選擇線上的hadoop集群任務不繁忙的時候進行。因為我們的數據量不是很大,需要備份的數據目錄也不是很多,所以就選擇為每天備份一次。
二、備份之前首先要了解distcp的工作原理以及考慮同步數據的速度以及磁盤io、網絡帶寬等。
1、distcp介紹
http://hadoop.apache.org/docs/r1.2.1/distcp.html
以下摘自官網:
DistCp (distributed copy) is a tool used for large inter/intra-cluster copying. It uses MapReduce to effect its distribution, error handling and recovery, and reporting. It expands a list of files and directories into input to map tasks, each of which will copy a partition of the files specified in the source list. Its MapReduce pedigree has endowed it with some quirks in both its semantics and execution. The purpose of this document is to offer guidance for common tasks and to elucidate its model.
2、使用方法
1)相同版本
#hadoop distcp -p -skipcrccheck -update –m 10 \
hdfs://spark:9000/data/metastore/userlogs \
hdfs://backup:9000/data/userlogs \
參數解釋:
-p:帶權限復制
-skipcrccheck: 跳過hdfs檢查
-update: 比較兩邊文件的大小,如果不一樣就更新,相同就不操作。如果不是追加數據,而是修改的數據,並且數據大小沒有變,那就要結合-overwrite、-delete來使用
2)如果版本不相同,則可以使用只讀的HftpFileSystem,需要在目標集群上執行:
#hadoop distcp -p -skipcrccheck -update –m 10 \
hftp://spark:50090/data/metastore/userlogs \
hdfs://backup:9000/data/userlogs \
hftp的端口是在hdfs-site.xml:
<property>
<name>dfs.namenode.http-address</name>
<value>spark:50090</value>
</property>
OPTIONS:
-p[rbugp] Preserve status
r: replication number
b: block size
u: user
g: group
p: permission
-p alone is equivalent to -prbugp
-i Ignore failures
-log <logdir> Write logs to <logdir>
-m <num_maps> Maximum number of simultaneous copies
-overwrite Overwrite destination
-update Overwrite if src size different from dst size
-f <urilist_uri> Use list at <urilist_uri> as src list
-filelimit <n> Limit the total number of files to be <= n
-sizelimit <n> Limit the total size to be <= n bytes
-delete Delete the files existing in the dst but not in src
3、計划任務
腳本:
#!/bin/bash
# copy spark:/data/metastore/userlogs to docker:/data/userlogs
/data/hadoop-2.7.3/bin/hadoop distcp -skipcrccheck -update -m 10 hdfs://spark:9000/data/metastore/userlogs hdfs://backup:9000/data/userlogs
#-m 是指啟動10個map任務執行,每個map任務默認256m,如果設定為10,則每個map是總數據量/10
#使用update是因為歷史數據沒有修改,如果修改則可以加上overwrite.
使用jenkins的pipline執行計划任務
pipeline { agent {label 'spark' } stages { stage('userlogs') { steps { dir('/data/scripts'){ sh 'sh userlogs.sh' } } } } post { failure { emailext subject: '$DEFAULT_SUBJECT', body: '$DEFAULT_CONTENT', recipientProviders: [ [$class: 'CulpritsRecipientProvider'], [$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider'] ], replyTo: '$DEFAULT_REPLYTO', to: '93048029849203@qq.com' } } }