首页 > 数据库技术 > 详细

mysql完整备份,超过十天的自动删除

时间:2014-07-13 14:26:57      阅读:423      评论:0      收藏:0      [点我收藏+]

#!/bin/bash

#Author xieping1991@163.com

cd /sqlbak

/usr/local/mysql/bin/mysqladmin -uroot -p123456 flush-logs

MAXIMUM_BACKUP_FILES=10

BACKUP_FOLDERNAME="database_backup"

DB_HOSTNAME="localhost"

DB_USERNAME="root"

DB_PASSWORD="123456"

DATABASES=(

            "数据库名称"

            "数据库名称"

            "数据库名称"

)

#=========

echo "Bash Database Backup Tool"

#CURRENT_DATE=$(date +%F)

CURRENT_DATE=$(date +%F)

BACKUP_FOLDER="${BACKUP_FOLDERNAME}_${CURRENT_DATE}"

mkdir $BACKUP_FOLDER

#Count the database.

count=0

while [ "x${DATABASES[count]}" != "x" ];do

    count=$(( count + 1 ))

done

echo "[+] ${count} databases will be backuped..."

# Iterate over the database list and dump (in SQL) the content of echo one.

for DATABASE in ${DATABASES[@]};do

    echo "[+] Mysql-Dumping: ${DATABASE}"

    echo -n " Began: ";echo $(date)

    if $(mysqldump -h ${DB_HOSTNAME} -u${DB_USERNAME} -p${DB_PASSWORD} ${DATABASE} > "${BACKUP_FOLDER}/${DATABASE}.sql");then

        echo " Dumped successfully!"

    else

        echo " Failed dumping this database!"

    fi

        echo -n " Finished: ";echo $(date)

done

echo

echo "[+] Packaging and compressing the backup folder..."

tar -cv ${BACKUP_FOLDER} | bzip2 > ${BACKUP_FOLDER}.tar.bz2 && rm -rf $BACKUP_FOLDER

BACKUP_FILES_MADE=$(ls -l ${BACKUP_FOLDERNAME}*.tar.bz2 | wc -l)

BACKUP_FILES_MADE=$(( $BACKUP_FILES_MADE - 0 )) ###Convert into integer number.

echo

echo "[+] There are ${BACKUP_FILES_MADE} backup files actually."

if [ $BACKUP_FILES_MADE -gt $MAXIMUM_BACKUP_FILES ];then

    REMOVE_FILES=$(( $BACKUP_FILES_MADE - $MAXIMUM_BACKUP_FILES ))

echo "[+] Remove ${REMOVE_FILES} old backup files."

    ALL_BACKUP_FILES=($(ls -t ${BACKUP_FOLDERNAME}*.tar.bz2))

    SAFE_BACKUP_FILES=("${ALL_BACKUP_FILES[@]:0:${MAXIMUM_BACKUP_FILES}}")

echo "[+] Safeting the newest backup files and removing old files..."

    FOLDER_SAFETY="_safety"

if [ ! -d $FOLDER_SAFETY ]

then mkdir $FOLDER_SAFETY

fi

for FILE in ${SAFE_BACKUP_FILES[@]};do

    mv -i ${FILE} ${FOLDER_SAFETY}

done

    rm -rf ${BACKUP_FOLDERNAME}*.tar.bz2

    mv -i ${FOLDER_SAFETY}/* ./

    rm -rf ${FOLDER_SAFETY}

CHAR=‘‘

for ((i=0;$i<=100;i+=2))

do printf "Removing:[%-50s]%d%%\r" $CHAR $i

        sleep 0.1

CHAR=#$CHAR

done

    echo

fi


本文出自 “xieping_linux” 博客,请务必保留此出处http://xieping.blog.51cto.com/3715452/1437201

mysql完整备份,超过十天的自动删除,布布扣,bubuko.com

mysql完整备份,超过十天的自动删除

原文:http://xieping.blog.51cto.com/3715452/1437201

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!