由于hive把元数据存储在mysql中,需要定期对mysql中hive用到的库进行备份,写了脚本如下。
#!/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 spaceDBS="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 directoryFTPDIR="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 itif [ -e $BACKDIR ]
then echo "Backups directory already exists !"else mkdir $BACKDIR echo "Create backup directory succeed !"fiif [ $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`fiecho "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.sqldoneif [ $FTP = "y" ]
thenecho "Initiating FTP connection..."cd $BACKDIRATTACH=`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 quitEOFecho -e "FTP transfer complete! \n"fiif [ $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."fiecho "Your backup is complete!"