BACKUP_PATH=${BACKUP_ROOT}/dat/${BACKUP_DATE} if [ ! -d ${BACKUP_PATH} ]; then mkdir -p ${BACKUP_PATH}; fi mkdir -p $BACKUP_PATH/$ORIGIN_DATABASE;
backup_beeline "create database if not exists $BACKUP_DATABASE;"
BACKUP_TABLES=$(backup_beeline "use $1; show tables;"|xargs) for TABLE in $BACKUP_TABLES do backup_beeline "show create table $ORIGIN_DATABASE.$TABLE;"|xargs > $BACKUP_PATH/$ORIGIN_DATABASE/$TABLE.sql backup_beeline "create table if not exists $BACKUP_DATABASE.$TABLE stored as CSVFILE as select * from $ORIGIN_DATABASE.$TABLE" done
for file in $(find $RESTORE_PATH/${DATABASE}/* -maxdepth 0 -type f); do TABLE=$(basename "${file%.sql}") echo "restore table $ORIGIN_DATABASE.$TABLE with $BACKUP_DATABASE.$TABLE and schema $file"|tee -a ${BACKUP_LOG} #restore_beeline "drop table $ORIGIN_DATABASE.$TABLE;" #restore_beeline "$(cat $file)" #restore_beeline "insert into $ORIGIN_DATABASE.$TABLE select * from $BACKUP_DATABASE.$TABLE;" done
echo "$(date +%c): restore timelyre database $1 with $2 finished!!!"|tee -a ${BACKUP_LOG} }
restore_timelyre() { echo "$(date +%c): restore timelyre starting..."|tee -a ${BACKUP_LOG}
RESTORE_PATH=$BACKUP_ROOT/dat/$RESTORE_DATE; if [ ! -d $RESTORE_PATH ]; then echo "backup $RESTORE_PATH not exists"|tee -a ${BACKUP_LOG} exit -1; fi
for db in $(find $RESTORE_PATH/* -maxdepth 0 -type d); do DATABASE=$(basename $db) echo "restore database: ${DATABASE} with ${RESTORE_PATH}"|tee -a ${BACKUP_LOG} restore_timelyre_database $DATABASE $RESTORE_PATH done
echo "$(date +%c): restore timelyre finished!!!"|tee -a ${BACKUP_LOG} }
# main echo "$(date +%c) restore ${RESTORE_DATE} starting..."|tee -a ${BACKUP_LOG}
clean_old_backups() { echo "$(date +%c): clean old backups starting..."|tee -a ${BACKUP_LOG}
for dir in $(find ${BACKUP_ROOT}/dat/* -maxdepth 0 -mtime $MTIME -type d); do echo "delete backup dir: $dir"|tee -a ${BACKUP_LOG} DELETE_DATE=$(basename $dir) for db in $(find $dir/* -maxdepth 0 -type d); do DATABASE=$(basename $db) echo "drop backup database: ${DATABASE}_backup_${DELETE_DATE}"|tee -a ${BACKUP_LOG} #backup_beeline "drop database if exists $DATABASE_backup_$DELETE_DATE;" done #rm -fr $dir done
echo "$(date +%c): clean old backups finished!!!"|tee -a ${BACKUP_LOG} }
# main echo "$(date +%c) delbak starting..."|tee -a ${BACKUP_LOG} clean_old_backups echo "$(date +%c) delbak finished!!!"|tee -a ${BACKUP_LOG}