MySQL full backup, more than 10 days of automatic deletion

Source: Internet
Author: User

#!/bin/bash

#Author [email protected]

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= (

"Database Name"

"Database Name"

"Database Name"

)

#=========

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"];d o

count=$ ((count + 1))

Done

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

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

For DATABASE in ${databases[@]};d o

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 is ${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[@]};d o

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


This article is from the "Xieping_linux" blog, make sure to keep this source http://xieping.blog.51cto.com/3715452/1437201

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.