删除指定日期的MySQL备份文件

时间:2022-02-17 14:06:49
数据库的备份脚本。
 
[url]http://blog.chinaunix.net/u/29134/showart_464801.html[/url]


这个脚本来配合原来的备份脚本做善后工作,只保留最新三天内的备份。

文件名称示例:

[root@mysql131-3 backup]# ls -sihl
total 1.2G
11075627 4.0M -rw-r--r-- 1 root root 4.0M Jan 27 01:00 backup_bbs20080127_010001.sql.gz
14843905 4.0M -rw-r--r-- 1 root root 4.0M Jan 28 01:00 backup_bbs20080128_010001.sql.gz
11075601 4.0M -rw-r--r-- 1 root root 4.0M Jan 29 01:00 backup_bbs20080129_010001.sql.gz
11075629  44K -rw-r--r-- 1 root root  39K Jan 27 01:00 backup_cms20080127_010001.sql.gz
14843908  44K -rw-r--r-- 1 root root  39K Jan 28 01:00 backup_cms20080128_010001.sql.gz
11075608  44K -rw-r--r-- 1 root root  39K Jan 29 01:00 backup_cms20080129_010001.sql.gz
11075628 403M -rw-r--r-- 1 root root 403M Jan 27 01:00 backup_site20080127_010001.sql.gz
14843906 403M -rw-r--r-- 1 root root 403M Jan 28 01:00 backup_site20080128_010001.sql.gz
11075602 403M -rw-r--r-- 1 root root 403M Jan 29 01:00 backup_site20080129_010001.sql.gz


脚本内容:

#!/bin/sh

#

# Created by david yeung 20080129.

#

# Delete overdue mysql backup file.

#

# Directory to list.

DIRNAME=/home/david_yeung/backup/
# Get the date part.

ARR1=`ls $DIRNAME | cut -d '_' -f3`
# Get three day ago.

CUR_DATE=`date +'%Y%m%d' -d '-3 day'`
# Delete the overdue file.

for CUR_FILENAME in $ARR1
do
  RESULT=`echo $CUR_FILENAME | tr -d "[a-z]"`
  if [ "$RESULT" -le "$CUR_DATE" ]
  then
    rm -rf `echo "${DIRNAME}backup_${CUR_FILENAME}_010001.sql.gz"`
  fi
done

本文出自 “上帝,咱们不见不散!” 博客,转载请与作者联系!