简体   繁体   English

在Bash备份脚本中删除旧的MySQL / MariaDB备份

[英]Removing old MySQL / MariaDB backups in Bash Backup Script

I've written a bash script, initiated on cron, that backups all databases on a particular machine nightly and weekly. 我编写了一个在cron上启动的bash脚本,该脚本每晚和每周备份一台特定计算机上的所有数据库。 The script correctly removes old databases, except for those cases when there's been a change in month. 该脚本可以正确删除旧数据库,但月份发生变化的情况除外。

As an example, let's say is November 2nd. 例如,假设是11月2日。 The script runs at 11:00pm, and correctly removes the backup made from November 1st. 该脚本在晚上11:00运行,并正确删除自11月1日以来所做的备份。 But come December 1st, the script gets confused, and does not correctly remove the backup made from November 30th. 但是12月1日到来时,脚本会变得混乱,并且不能正确删除11月30日以来所做的备份。

How can I fix this script to correctly remove the old backups in this case? 在这种情况下,如何解决此脚本以正确删除旧备份?

DATABASES=$(echo 'show databases;' | mysql -u backup --password='(password)' | grep -v ^Database$)
LIST=$(echo $DATABASES | sed -e "s/\s/\n/g")
DATE=$(date +%Y%m%d)
DAYOLD=$(($DATE-1))
SUNDAY=$(date +%a)
WEEKOLD=$(($DATE-7))

for i in $LIST; do
if [[ $i != "mysql" ]]; then
        mysqldump --single-transaction $i > /mnt/backups/mariadb/daily/$i.$DATE.sql
        if [ -f /mnt/backups/mariadb/daily/$i.$DAYOLD.sql ]; then
                rm -f /mnt/backups/mariadb/daily/$i.$DAYOLD.sql
        fi
        if [[ $SUNDAY == "Sun" ]]; then
                cp /mnt/backups/mariadb/daily/$i.$DATE.sql /mnt/backups/mariadb/weekly/$i.$DATE.sql
                rm -f /mnt/backups/mariadb/weekly/$i.$WEEKOLD.sql
        fi
fi
done

If you know the number of backups performed in a specific range of time, let's say you know from 2nd Nov until 2nd Dec you know that exactly 30 backups have been made and you now want to erase those, just use the number of backups, it's super simple to do and you don't have to deal with dates which is pretty complex in bash: 如果您知道在特定时间范围内执行的备份数量,那么假设您知道从11月2日到12月2日,您已经知道已经完成了30次备份,而现在您要擦除这些备份,只需使用备份数量即可,超级简单,您不必处理bash中非常复杂的日期:

$ (ls -t|head -n 30;ls)|grep -v ^Database|sort|uniq -u|xargs rm -rf

You can then easily automate this script by removing each day the older one so you only get the fix number of backups you want: 然后,您可以通过每天删除较旧的一天来轻松地自动执行此脚本,从而仅获得所需的固定备份数量:

#! /bin/bash
# Create new full backup
BACKUP_DIR="/path-to-backups/"
BACKUP_DAYS=1

# Prepare backup
cd ${BACKUP_DIR}
latest=`ls -rt | grep 201 | head -1`

# Change latest reference
ln -sf ${BACKUP_DIR}${latest} latest

# Cleanup older than one week (n days)
to_remove=`(ls -t | grep 201 | head -n 3;ls)|sort|uniq -u`
echo "Cleaning up... $to_remove"
(ls -t|head -n ${BACKUP_DAYS};ls)|sort|uniq -u|xargs rm -rf

echo "Backup Finished"
exit 0

Then you can link it to daily cron. 然后,您可以将其链接到每日cron。 This is explained in this blog entry, how to do this stuff in a very straightforward fashion (but with hot backups, no mysqldump): http://codeispoetry.me/index.php/mariadb-daily-hot-backups-with-xtrabackup/ 这是在此博客条目中说明的,如何以一种非常直接的方式(但有热备份,没有mysqldump)来完成这些工作: http : //codeispoetry.me/index.php/mariadb-daily-hot-backups-with- xtrabackup /

I was making this too complicated. 我让这太复杂了。 Instead of using the date at all, I'm just searching for the age of the file backup with: 我根本没有使用日期,而是使用以下命令搜索文件备份的期限:

find /mnt/backups/mariadb/weekly/* -type f -mtime +8 -exec rm -f {} \;

So the entire script becomes: 因此,整个脚本变为:

DATABASES=$(echo 'show databases;' | mysql -u backup --password='foo' | grep -v ^Database$)
LIST=$(echo $DATABASES | sed -e "s/\s/\n/g")
DATE=$(date +%Y%m%d)
SUNDAY=$(date +%a)

for i in $LIST; do
if [[ $i != "mysql" ]]; then
        /bin/nice mysqldump --single-transaction $i > /mnt/backups/mariadb/daily/$i.$DATE.sql
        find /mnt/backups/mariadb/daily/* -type f -mtime +1 -exec rm -f {} \;

        if [[ $SUNDAY == "Sun" ]]; then
                cp /mnt/backups/mariadb/daily/$i.$DATE.sql /mnt/backups/mariadb/weekly/$i.$DATE.sql
                find /mnt/backups/mariadb/weekly/* -type f -mtime +8 -exec rm -f {} \;
        fi
fi
chown -R backup.backup /mnt/backups
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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