博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hive元数据备份
阅读量:4691 次
发布时间:2019-06-09

本文共 2852 字,大约阅读时间需要 9 分钟。

由于hive把元数据存储在mysql中,需要定期对mysql中hive用到的库进行备份,写了脚本如下。

支持的功能:
1、备份指定的mysql数据库;
2、批量备份mysql中所有的数据库(默认关闭);
3、将备份的文件压缩后存储;
4、将备份文件传输到指定ftp服务器上备份(默认关闭);
5、定期删除n天之前的备份文件(默认关闭)。
 

#!/bin/bash

###############################################################################
# FileName: MysqlBackupScript
# Author: chenzheng
# Version: 1.0
# Description: Backup Mysql Database
# Histroy:
# Version: v1.0
# Date: 20140703
##############################################################################

# your MySQL server's name

SERVER="Hadoop4"

# directory to backup to

BACKDIR=~/MysqlBackups

# date format that is appended to filename

DATE=`date +'%Y-%m-%d'`

#----------------------MySQL Settings--------------------#

# your MySQL server's location (IP address is best)

HOST=10.190.113.999

# MySQL username

USER=hive

# MySQL password

PASS=xxxxoooo

# List all of the MySQL databases that you want to backup in here,

# each separated by a space
DBS="hive"

# set to 'y' if you want to backup all your databases. this will override

# the database selection above.
DUMPALL=n

#----------------------FTP Settings--------------------#

# set "FTP=y" if you want to enable FTP backups

FTP=n

# FTP server settings; should be self-explanatory

FTPHOST="10.190.113.999:21"
FTPUSER="hadoop"
FTPPASS="xxxx0000"

# directory to backup to. if it doesn't exist, file will be uploaded to

# first logged-in directory
FTPDIR="backups"

#-------------------Deletion Settings-------------------#

# delete old files?

DELETE=n

# how many days of backups do you want to keep?

DAYS=30

#----------------------End of Settings------------------#

# check of the backup directory exists

# if not, create it

if [ -e $BACKDIR ]

then
echo "Backups directory already exists !"
else
mkdir $BACKDIR
echo "Create backup directory succeed !"
fi

if [ $DUMPALL = "y" ]

then
echo "Creating list of all your databases..."
mysql -h $HOST --user=$USER --password=$PASS -e "show databases;" > ~/dbs_on_$SERVER.txt

# redefine list of databases to be backed up

DBS=`sed -e ':a;N;$!ba;s/\n/ /g' -e 's/Database //g' ~/dbs_on_$SERVER.txt`
fi

echo "Backing up MySQL databases..."

for database in $DBS

do
mysqldump -h $HOST --user=$USER --password=$PASS $database > $BACKDIR/$SERVER-mysqlbackup-$database-$DATE.sql
gzip -f -9 $BACKDIR/$SERVER-mysqlbackup-$database-$DATE.sql
done

if [ $FTP = "y" ]

then
echo "Initiating FTP connection..."
cd $BACKDIR
ATTACH=`for file in *$DATE.sql.gz; do echo -n -e "put ${file}\n"; done`
ftp -nv <<EOF
open $FTPHOST
user $FTPUSER $FTPPASS
cd $FTPDIR
$ATTACH
quit
EOF
echo -e "FTP transfer complete! \n"
fi

if [ $DELETE = "y" ]

then
NEWDATE=`date --date='30 days ago' +'%Y-%m-%d'`
rm -rf $BACKDIR/$SERVER-mysqlbackup-$database-$NEWDATE.sql.gz
echo "The backup from $DAYS days ago has been deleted."
fi

echo "Your backup is complete!"

 

转载于:https://www.cnblogs.com/chenz/articles/3822612.html

你可能感兴趣的文章
IT常用单词
查看>>
拓扑排序
查看>>
NYOJ--32--SEARCH--组合数
查看>>
JMS
查看>>
gulpfile 压缩模板
查看>>
【34.14%】【BZOJ 3110】 [Zjoi2013]K大数查询
查看>>
【 henuacm2016级暑期训练-动态规划专题 A 】Cards
查看>>
第五篇:白话tornado源码之褪去模板的外衣
查看>>
设备常用框架framework
查看>>
bootstrap模态框和select2合用时input无法获取焦点(转)
查看>>
21世纪经济网APP
查看>>
解决NetworkOnMainThreadException
查看>>
1039 到底买不买
查看>>
农银电商项目学习笔记(一)
查看>>
MockObject
查看>>
Chukwa
查看>>
(转)Maven仓库——私服介绍
查看>>
设计模式之工厂模式
查看>>
仿复制粘贴功能,长按弹出tips的实现
查看>>
Kubernetes-Host网络模式应用
查看>>