Ubuntu清理冗余备份文件

2019年03月08日 99 字 教程整理


如果系统设置了定期系统备份/数据库备份,则有必要定期清理历史备份文件。

0.1. 脚本公式

    #自定义函数:删除指定路径下除最新修订子文件夹外全部其他子文件夹
    function remove_redundant_dump_file_dirs(){
        check_path=$1
        check_subpath=${check_path}'*'
        #查找当前目录下最近更新的目录
        newest_folder_name=`cd ${check_path} ; ls -l | grep "^d" | tail -n 1 | awk '{print $9}'`
        newest_folder_path=${check_path}${newest_folder_name}'/'
        echo ${newest_folder_path}
        ##删除当前路径下除最新文件之外的全部子目录
        [ `find ${check_subpath} -type d  | wc -l` -gt 1 ]  && (echo "more than one dump files,removing other histories";find ${check_subpath} -name ${newest_folder_name} -prune -o -type d -exec rm -r {} \;) || (echo "not many dump files, keeping the histories";)
    }
    #自定义函数: 删除指定路径下选定文件类型除最新修订文件外全部其他文件
    function remove_redundant_dump_files(){
        check_path=$1
        file_suffix=$2
        #查找当前目录下选定类型最近更新的文件
        newest_file_name=`cd ${check_path} ; ls -l | grep \.${file_suffix} | tail -n 1 | awk '{print $9}'`
        newest_file_path=${check_path}${newest_file_name}
        echo ${newest_file_path}
        ##删除当前路径下选定类型除最新文件之外的全部文件
        [ `find ${check_path} -name "*.${file_suffix}" -type f  | wc -l` -gt 1 ]  && (echo "more than one dump files,removing other histories";find ${check_path} -name ${newest_file_name} -prune -o -name "*.${file_suffix}" -type f -exec rm {} \;) || (echo "not many dump files, keeping the histories";)
    }
    
    
    #清除选定路径下的旧子文件夹(仅保留最新编辑子文件夹,其余全部删除)
    remove_redundant_dump_file_dirs <selected_path>
    
    #清除选定路径下指定类型的旧文件(仅保留最新编辑文件,其余全部删除)
    remove_redundant_dump_files <selected_path> <selected_file_suffix>

0.2. 示例脚本

    function remove_redundant_dump_file_dirs(){
        check_path=$1
        check_subpath=${check_path}'*'
        newest_folder_name=`cd ${check_path} ; ls -l | grep "^d" | tail -n 1 | awk '{print $9}'`
        newest_folder_path=${check_path}${newest_folder_name}'/'
        echo ${newest_folder_path}
        [ `find ${check_subpath} -type d  | wc -l` -gt 1 ]  && (echo "more than one dump files,removing other histories";find ${check_subpath} -name ${newest_folder_name} -prune -o -type d -exec rm -r {} \;) || (echo "not many dump files, keeping the histories";)
    }
    function remove_redundant_dump_files(){
    check_path=$1
    file_suffix=$2
    #查找当前目录下最近更新的目录
    newest_file_name=`cd ${check_path} ; ls -l | grep \.${file_suffix} | tail -n 1 | awk '{print $9}'`
    newest_file_path=${check_path}${newest_file_name}
    echo ${newest_file_path}
    ##删除当前路径下除最新文件之外的全部文件
    [ `find ${check_path} -name "*.${file_suffix}" -type f  | wc -l` -gt 1 ]  && (echo "more than one dump files,removing other histories";find ${check_path} -name ${newest_file_name} -prune -o -name "*.${file_suffix}" -type f -exec rm {} \;) || (echo "not many dump files, keeping the histories";)
    }
    
    
    #清除冗余本地系统备份文件路径
    remove_redundant_dump_file_dirs /usr/local/system_backups/local/
    
    #清除冗余远程MySQL数据表备份文件
    remove_redundant_dump_files /usr/local/mysqlDB_dumps_remote/47-100-64-208/ tar