一、HBase数据备份核心方式选择
在CentOS上制定HBase备份策略前,需根据数据规模、恢复需求选择合适的方式:
二、基础环境准备
hbase-daemon.sh status master/regionserver)。hbase-site.xml,设置备份目录(本地或HDFS)及自动备份参数:<property>
<name>hbase.backup.enabled</name>
<value>true</value>
</property>
<property>
<name>hbase.backup.dir</name>
<value>hdfs:///hbase/backup</value> <!-- 或本地路径如/home/hbase/backup -->
</property>
<property>
<name>hbase.backup.scheduled</name>
<value>true</value>
</property>
<property>
<name>hbase.backup.interval</name>
<value>24h</value> <!-- 自动备份间隔 -->
</property>
hbase)对备份目录有读写权限:mkdir -p /home/hbase/backup
chown -R hbase:hbase /home/hbase/backup
三、具体备份实施方法
user_table):hbase shell <<EOF
snapshot 'user_table', 'snapshot_user_table_$(date +%Y%m%d_%H%M%S)'
EOF
hbase-site.xml中设置自动快照周期和保留数量,避免磁盘空间耗尽:<property>
<name>hbase.snapshot.period</name>
<value>86400</value> <!-- 每天一次(单位:秒) -->
</property>
<property>
<name>hbase.snapshot.max</name>
<value>10</value> <!-- 最多保留10个快照 -->
</property>
hbase shell <<EOF
disable 'user_table'
restore_snapshot 'snapshot_user_table_20250918_120000'
enable 'user_table'
EOF
/hbase/export/user_table):hbase org.apache.hadoop.hbase.mapreduce.Export user_table /hbase/export/user_table
-D hbase.mapreduce.scan.row.start和-D hbase.mapreduce.scan.row.stop参数指定时间范围或行键范围,实现增量备份。hbase org.apache.hadoop.hbase.mapreduce.Import user_table /hbase/export/user_table
hbase-site.xml中启用复制并设置端口:<property>
<name>hbase.replication</name>
<value>true</value>
</property>
<property>
<name>hbase.replication.source.port</name>
<value>60000</value> <!-- 源集群Replication端口 -->
</property>
zk1:2181:/hbase):hbase shell <<EOF
add_peer '1', "zk1:2181:/hbase_backup"
EOF
四、定期备份自动化(Cron任务)
使用Linux的cron定时执行备份脚本,实现每日/每周自动备份:
/home/hbase/scripts/daily_backup.sh):#!/bin/bash
BACKUP_DIR=/home/hbase/backup/$(date +%Y%m%d)
mkdir -p $BACKUP_DIR
# 创建表级快照
for table in user_table order_table; do
hbase shell <<EOF
snapshot '$table', 'snapshot_$table_$(date +%Y%m%d_%H%M%S)'
EOF
# 将快照复制到备份目录(可选)
hbase org.apache.hadoop.hbase.snapshot.ExportSnapshot -snapshot snapshot_$table_$(date +%Y%m%d_%H%M%S) -copy-to $BACKUP_DIR
done
# 删除7天前的备份
find $BACKUP_DIR -type d -mtime +7 -exec rm -rf {} \;
chmod +x /home/hbase/scripts/daily_backup.sh
crontab -e
添加以下内容:0 2 * * * /home/hbase/scripts/daily_backup.sh >> /var/log/hbase_backup.log 2>&1
五、备份策略优化建议
六、注意事项
hdfs dfs -ls /hbase/backup)。