BASIC script functions:
Automatic compression and backup of mysql Databases
Automatically Delete backup files from the past 10 days
The deletion progress is displayed.
#!/bin/bash#Author absolutely.xu@gmail.comMAXIMUM_BACKUP_FILES=10BACKUP_FOLDERNAME="database_backup"DB_HOSTNAME="localhost"DB_USERNAME="root"DB_PASSWORD="123456"DATABASES=( "openfire" "csp")#=========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=0while [ "x${DATABASES[count]}" != "x" ];do count=$(( count + 1 ))doneecho "[+] ${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)doneechoecho "[+] Packaging and compressing the backup folder..."tar -cv ${BACKUP_FOLDER} | bzip2 > ${BACKUP_FOLDER}.tar.bz2 && rm -rf $BACKUP_FOLDERBACKUP_FILES_MADE=$(ls -l ${BACKUP_FOLDERNAME}*.tar.bz2 | wc -l)BACKUP_FILES_MADE=$(( $BACKUP_FILES_MADE - 0 )) ###Convert into integer number.echoecho "[+] 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 fifor 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.1CHAR=#$CHARdone echofi
The database backup function test on the seventh day is as follows:
650) this. width = 650; "title =" bak1.jpg "alt =" wKioL1LgihrDG2aFAAGMEm4f4aY541.jpg "src =" http://www.bkjia.com/uploads/allimg/140207/2156326132-0.jpg "/>
The backups of the last 10 days will be automatically retained after 10 days, and the backups will be automatically deleted after 10 days.
Simulate the test Script Function and create a 31-day backup file as follows:
650) this. width = 650; "width =" 881 "height =" 223 "title =" bak.jpg "style =" width: 711px; height: 166px; "alt =" wKiom1Lgi1KD0mgyAAOJ-HIh3ks724.jpg "src =" http://www.bkjia.com/uploads/allimg/140207/2156322W9-1.jpg "/>
Execute scripts regularly for daily scheduled tasks to keep backups for the last 10 days
650) this. width = 650; "width =" 857 "height =" 443 "title =" bak.jpg "style =" width: 720px; height: 407px; "alt =" wKioL1Lgi4HxUyfDAALxxhhOinA434.jpg "src =" http://www.bkjia.com/uploads/allimg/140207/215632J06-2.jpg "/>
This article is from the "Old Xu's Private food" blog and will not be reposted!