加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

逐个文件夹递归备份文件

发布时间:2020-12-15 07:13:15 所属栏目:安全 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #!/usr/bin/bash#逐个文件夹递归备份文件#输入为一个或多个文件、文件夹hmdir="$HOME"bkdir="${HOME}/backup"dt=$(date +%Y%m%d)tm=$(date +%H%M%S)cr

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

#!/usr/bin/bash
#逐个文件夹递归备份文件
#输入为一个或多个文件、文件夹

hmdir="$HOME"
bkdir="${HOME}/backup"
dt=$(date +%Y%m%d)
tm=$(date +%H%M%S)

crtdir=$(pwd)
dtdir=${bkdir}${crtdir:${#hmdir}}

function usage()
{
	echo ""
	echo "backup file(s) dir(s)"
	echo ""
	exit 0 
}
       
function bkfile()
{
	#$1=crtdir
	#$2=destdir
	#$3=file

	mkdir -p $2
	if [ -f $2/$3 ];then
		cmp -s $2/$3  $1/$3
		[ $? -eq 1 ] &&  cp $2/$3 $2/$3.${tm}
	fi
	cp $1/$3 $2
}

function bkdir()
{
	local ctdir dtdir dtfiles

	ctdir=$1
	shift
	dtdir=$1
	shift
	dtfiles=$*

	for file in ${dtfiles};do
		[ -f ${ctdir}/${file} ] && bkfile $ctdir ${dtdir}/${dt} $file

		if [ -d ${ctdir}/${file} ];then
			nctdir=${ctdir}/$file
			ndtdir=${dtdir}/$file
			vars=$(ls $nctdir)
			bkdir $nctdir $ndtdir $vars
		fi
	done
}

function bk()
{
	while [ $# -gt 0 ]
	do
		[ -f "$1" ] && bkfile $crtdir ${dtdir}/${dt} $1
		[ -d "$1" ] && bkdir ${crtdir}/$1 ${dtdir}/$1 $(ls $1)
		shift
	done
}
	
[ $# -eq 0 ] && usage || bk $*

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读