shell 文件备份脚本

前端之家收集整理的这篇文章主要介绍了shell 文件备份脚本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#!/bin/bash

#输入参数:文件名
filename=$1
#源文件目录
directory=/opt/docker/cloud-driver-training/apps
#备份文件目录
backupdir=$directory/backup

#删除备份文件函数(备份文件数量设有有上限,超过上限会删除更早之前的备份文件function deleteFiles() {
        if [ -z $filename ]; then
                echo "==========>param filename is null"
                return
        fi
        #列出所有同名文件,按文件更新时间倒序排序
        files=`ls -t $backupdir | grep $filename`
        index=
        #保留的最大备份文件数量
    maxFileCount=3
        for file in $files
        do
        #当前备份文件数量大于最大备份文件数量,则删除历史的备份文件
                if [ $index -gt $maxFileCount ]; then
                        ==========>backup files count > $maxFileCount,delete history file $backupdir/$file"
            rm -rf $backupdir/$file
                
                index=$[$index+]
        done
        ==========>fileCount:$index
}

#文件备份函数
 backup() {
       #源文件
    sourceFile=$directory/$filename
    #如果源文件存在,执行备份
    if [ -f $sourceFile ]; then
            backupFile=$backupdir/$filename`stat -c %y $sourceFile | cut -c 1-10`
            ==========> moving $sourceFile to $backupFilemv $sourceFile $backupFile
        #备份完后,清除历史备份文件(如果超出最大备份数)
            deleteFiles
    else
            ==========> $sourceFile is not found"
    
}

==========> do backup
backup
==========> backup finish"

 

猜你在找的Linux相关文章