大数据平台 HDP ambari pgsql 的备份
#!/bin/bash
export PGPASSWORD=bigdataBACKUP_PATH=/var/lib/pgsql/backups
LOG_FILE=/var/lib/pgsql/backups/pgbackup.log
DATE=$( date +%Y_%m_%d_%k:%M:%S)
remote_ip="test15"
log(){
LEVEL=$1
INFO=$2
if [ ! -d /var/lib/pgsql/backups ];then
mkdir -p $BACKUP_PATH
fi
test -f $LOG_FILE || touch $LOG_FILE
echo $LEVEL $( date +%Y_%m_%d_%k:%M:%S) $ $INFO 2>&1 |tee -a $LOG_FILE
}
backup1(){
export PGPASSWORD=bigdata
log DEBUG "Begin back up pg "
pg_dump -U ambari ambari -f $BACKUP_PATH/ambari_$DATE.sql
if [ $? -ne 0 ];then
log ERROR "Begin back up pg Failed".
return 1
else
if [ ! -f $BACKUP_PATH/ambari_$DATE.sql ];then
log ERROR "Back up pg Failed.[File $BACKUP_PATH/ambari_$DATE.sql is not exist. ]".
return 1
else
size=$(du -s $BACKUP_PATH/ambari_$DATE.sql |awk '{print $1}' )
echo $size
if [ $size -lt 40000 ];then
log ERROR "Back up pg Failed.[ du -s $BACKUP_PATH/ambari_$DATE.sql |awk '{print $1}' < 40000 ]".
return 1
fi
fi
fi
log SUCCECC "Back up pg Failed.".
return 0
}
backup2(){
export PGPASSWORD=mapred
log DEBUG "Begin back up pg mapred"
pg_dump -U mapred ambarirca -f $BACKUP_PATH/ambarirca_$DATE.sql
if [ $? -ne 0 ];then
log ERROR "Begin back up pg mapred Failed".
return 1
else
if [ ! -f $BACKUP_PATH/ambarirca_$DATE.sql ];then
log ERROR "Back up pg Failed.[File $BACKUP_PATH/ambarirca_$DATE.sql is not exist. ]".
return 1
else
size=$(du -s $BACKUP_PATH/ambarirca_$DATE.sql |awk '{print $1}' )
echo $size
if [ $size -lt 11 ];then
log ERROR "Back up mapred pg Failed.[ du -s $BACKUP_PATH/ambarirca_$DATE.sql |awk '{print $1}' < 11 ]".
return 1
fi
fi
fi
log SUCCECC "Back up mapred pg ."
return 0
}
traslate_sql(){
FLAG="0"
ssh root@$remote_ip "mkdir -p $BACKUP_PATH"
scp $BACKUP_PATH/ambarirca_$DATE.sql $BACKUP_PATH/ambari_$DATE.sql root@$remote_ip:$BACKUP_PATH
value=$(ssh root@$remote_ip "ls $BACKUP_PATH/ambarirca_$DATE.sql ")
if [ "x$( echo $value |grep $BACKUP_PATH/ambarirca_$DATE.sql )" = "x" ];then
log ERROR "Back up mapred pg Failed.[ ssh root@$remote_ip ls $BACKUP_PATH/ambarirca_$DATE.sql ]".
FLAG="1"
fi
value2=$(ssh root@$remote_ip "ls $BACKUP_PATH/ambari_$DATE.sql ")
if [ "x$( echo $value2 |grep $BACKUP_PATH/ambari_$DATE.sql )" = "x" ];then
log ERROR "Back up mapred pg Failed.[ ssh root@$remote_ip ls $BACKUP_PATH/ambari_$DATE.sql ]".
FLAG="2"
fi
if [ "x$FLAG" != "x0" ];then
log ERROR "Back up remote hosts filed"
else
log INFO "Back up retmote host successed"
fi
}
backup1
backup2
traslate_sql