简体   繁体   中英

MySQL backup and delete old backups

I am using following simple script to take backup of all of my databases via tar and than delete old backup directories 7 days older(including files in it) so it does not occupy all space

#!/bin/bash
# Simple script to backup MySQL databases

# Parent backup directory
backup_parent_dir="/backup/mysql"

# MySQL settings
mysql_user="root"
mysql_password="mypass"
mysql_server="localhost"

# Read MySQL password from stdin if empty
if [ -z "${mysql_password}" ]; then
  echo -n "Enter MySQL ${mysql_user} password: "
  read -s mysql_password
  echo
fi

# Check MySQL password
echo exit | mysql --host=${mysql_server} --user=${mysql_user} --password=${mysql_password} -B 2>/dev/null
if [ "$?" -gt 0 ]; then
  echo "MySQL ${mysql_user} password incorrect"
  exit 1
else
  echo "MySQL ${mysql_user} password correct."
fi

# Create backup directory and set permissions
backup_date=`date +%Y_%m_%d_%H_%M`
backup_dir="${backup_parent_dir}/${backup_date}"
echo "Backup directory: ${backup_dir}"
mkdir -p "${backup_dir}"
chmod 755 "${backup_dir}"

# Get MySQL databases
mysql_databases=`echo 'show databases' | mysql --host=${mysql_server} --user=${mysql_user} --password=${mysql_password} -B | sed /^Database$/d`

# Backup and compress each database
for database in $mysql_databases
do
  if [ "${database}" == "information_schema" ] || [ "${database}" == "performance_schema" ]; then
        additional_mysqldump_params="--skip-lock-tables"
  else
        additional_mysqldump_params=""
  fi
  echo "Creating backup of \"${database}\" database"
  mysqldump ${additional_mysqldump_params} --host=${mysql_server} --user=${mysql_user} --password=${mysql_password} ${database} | gzip > "${backup_dir}/${database}.gz"
  chmod 600 "${backup_dir}/${database}.gz"
done

#Delete backups older than 7 days
find ${backup_parent_dir} -name '*' -type d -mtime +6 -exec rm -rfv "{}" \;

Now, backup is working fine and it takes all databases backups into separate directories, but for some reason it does not delete the old backup directories via this part

#Delete backups older than 7 days
find ${backup_parent_dir} -name '*' -type d -mtime +6 -exec rm -rfv "{}" \;

However, if I run a separate script which contains only following code than it works perfectly fine and deletes all 7 days older directories

# Parent backup directory
backup_parent_dir="/backup/mysql"

#Delete backups older than 7 days
find ${backup_parent_dir} -name '*' -type d -mtime +6 -exec rm -rfv "{}" \;

So, I am not sure why delete lines are not working when they are added into single script?

I would suggest to execute the script like this which will print more info about each step of the script

bash -x nameofscript

Also for debugging reasons instead of

find ${backup_parent_dir} -name '*' -type d -mtime +6 -exec rm -rfv "{}" \\;

use

find ${backup_parent_dir} -name '*' -type d -mtime +6

just to see which will be deleted

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM