Linux系统shell脚本之文件备份

  • A+
所属分类:Linux

一、脚本要求

1.备份/data目录到/backup/目录下面/backup/data/

二、脚本内容

[root@192 scripts]# cat backup_file.sh 
#!/bin/bash
########################################
#Author:jeven
#time:Mon 16 May 2022 05:07:13 PM CST
#filename:backup_file.sh
#Script description:
########################################


SOURCE=/data
DEST=/backup
if [ -d $DEST ];then
	cp -ar $SOURCE $DEST
	echo "the $DEST's backup success!"
elif [-f $DEST ];then
        read -p "rename $DEST to $DEST.bak(y/n):" NUM
	if [ $NUM = y ];then
		mv $DEST $DEST.bak
		mkdir -p $DEST
		cp -ar $SOURCE $DEST
		echo "the $DEST already created,backup finished!"
	else
		echo "inpute error ,please inpute y/n!"
	fi
else
	mkdir -p $DEST
	cp -ar $SOURCE $DEST
	echo "backup finished"
fi

三、执行脚本

1.当源目录和备份目录存在

[root@192 scripts]# ./backup_file.sh 
the /backup's backup success!

2.当备份目录不存在

修改备份目录为一个文件

root@192 scripts]# ./backup_file.sh 
rename /backup/dir to /backup/dir.bak(y/n):1a1a1
inpute error ,please inpute y/n!
[root@192 scripts]# ./backup_file.sh 
rename /backup/dir to /backup/dir.bak(y/n):y
the /backup/dir already created,backup finished!
[root@192 scripts]# 

w3cjava