hive全庫資料及表結構的備份與恢復

2021-10-04 20:44:08 字數 2440 閱讀 5318

在之前的文章中介紹了hive的export和import的使用方式,下面提供乙個簡單的指令碼實現對指定的庫列表做全庫的資料及元資料的備份以及恢復。

hive的資料匯入匯出,備份恢復

很簡單,是利用了awk和sed命令結合hive命令列。

首先傳入庫名列表,再遍歷庫名使用hive檢視該庫下所有表,然後拼接字串到export檔案內,前面介紹了export和import的命令僅僅是export和import以及to 和from的區別,所以這裡直接對export檔案處理得到import檔案,然後讀取export檔案裡的命令,使用hive命令列執行。這裡推薦改為使用beeline更安全。備份之後的元資料以及資料都在/tmp/warehouse_bak 下,參考下面的**即可寫出批量import的指令碼:傳入庫名列表,讀取import檔案,使用hive執行。

如不想使用python也可以將**改為shell,思路同上,這裡就不提供shell**了。

import subprocess

schema_list=['dh_party','dh_sales','dh_t3_group_index','dh_t3_group_index_pre','dl_h_nccp','dl_h_noahwm_trans','dl_h_noahwm_cbs','dh_t3_mutual_fund','dh_t3_report_hlwpp','dh_event','dh_t3_report_crm']

# schema_list=['dh_party','dh_sales','dh_t3_group_index','dh_t3_group_index_pre','dh_t3_mutual_fund','dh_t3_report_hlwpp','dh_event']

for s in schema_list:

try:

print('----------start: '+s+' --------------------')

hive_export_cmd=r"""hive -e 'use """+s+r""";show tables;' | awk '' | sed "s/|/'/g" > /home/tools/warehouse_bak/"""+s+r"""_export.hql"""

subprocess.getstatusoutput(hive_export_cmd)

cp_cmd = r"""cp /home/tools/warehouse_bak/{}_export.hql /home/tools/warehouse_bak/{}_import.hql""".format(s,s)

subprocess.getstatusoutput(cp_cmd)

sed1 = r"""sed -i 's/export table/import table/g' /home/tools/warehouse_bak/{}_import.hql""".format(s)

sed2 = r"""sed -i 's/ to / from /g' /home/tools/warehouse_bak/{}_import.hql""".format(s)

subprocess.getstatusoutput(sed1)

subprocess.getstatusoutput(sed2)

#export

with open(r"""/home/tools/warehouse_bak/{}_export.hql""".format(s),'r') as f:

cmd_list = f.readlines()

print(cmd_list)

for i in range(cmd_list.__len__()-2):

hive_tbl_export = r""" hive -e "{}" """.format(cmd_list[i].replace(r'\n',''))

try:

subprocess.getstatusoutput(hive_tbl_export)

except exception as e:

print(e)

pass

# hive_export = r"""hive -f /home/tools/warehouse_bak/{}_export.hql""".format(s)

# subprocess.getstatusoutput(hive_export)

print('----------end: '+s+' --------------------')

except exception as e:

print(e)

pass

#!/bin/bash

nohup /home/tools/python3/python36/bin/python3 /home/tools/warehouse_bak/hive_backup.py > /home/tools/warehouse_bak/log/backup_log.out 2>&1 &

MySQL資料庫全庫備份及增量備份指令碼

bin bash 定義預設值 user root passwd 123.com host localhost today date f dump cmd usr local mysql bin mysqldump full backupdir opt database full backup ema...

遷移hive表及hive資料

公司hadoop集群遷移,需要遷移所有的表結構及比較重要的表的資料 跨雲服務機房,源廣州機房,目標北京機房 1 遷移表結構 1 老hive中匯出表結構 hive e use db show tables tables.txt bin bash cat tables.txt while read ea...

mysql備份,備份資料,資料庫,表結構

mysql mysqldump 這裡我的資料庫先叫做xmen 備份資料庫 如下 mysqldump 資料庫名 資料庫備份名 mysqldump a u使用者名稱 p密碼 資料庫名 資料庫備份名 mysqldump d a add drop table uroot p sql 1.匯出結構不匯出資料 ...