1. 排除文件夹
zip -r jason.zip jason/* -x jason/backports-4.4.2-1/\*
以上命令打包jason文件夹,排除其中的backports-4.4.2-1文件夹
如果改成这样:
zip -r jason.zip jason/* -x jason/backports-4.4.2-1\*
会排除其中以backports-4.4.2-1开头的文件和文件夹,比如jason下还有文件backports-4.4.2-1.tar.gz,也会被排除。
那这样呢?
zip -r jason.zip jason/* -x jason/backports-4.4.2-1/**\*
会排除backports-4.4.2-1目录下的所有文件和文件夹,backports-4.4.2-1目录保留
zip -r jason.zip jason/* -x jason/backports-4.4.2-1/*
这样只排除backports-4.4.2-1目录下的文件了
2. 指定时间范围
zip支持-t(--from-date mmddyyyy), -tt(--before-date mmddyyyy)参数,可以根据mtime打包文件。"yyyy-mm-dd"格式也支持。
zip -rt 2016-11-11 jason.zip jason
那要根据ctime,atime呢,只能借助find了,使用find的优点是支持更精确的时间。
CentOS 7 find支持-newerXY参数,如根据ctime则为-newerct,可以查找ctime比指定时间新的文件。
find jason -newerct "2016-11-11 11:11:11" | zip -@ jason.zip
CentOS 6 find不支持-newerXY,只能使用参数-ctime,-atime/-cmin,-amin, 只是要计算一下时间,稍麻烦点。
find jason -cmin -100 | zip -@ jason.zip
可以通过以下的dateDiff function计算时间,比如:
min=$(date_diff "-m" "now" "2016-11-11 11:11")
#!/bin/bash
date2stamp () {
date -d "$1" +%s
}
dateDiff () {
case "$1" in
"-s")
sec=1
shift
;;
"-m")
sec=60
shift
;;
"-h")
sec=3600
shift
;;
"-d")
sec=86400
shift
;;
*)
sec=86400
;;
esac
date1=$(date2stamp "$1")
date2=$(date2stamp "$2")
diffSec=$((date2-date1))
if [ "$diffSec" -lt 0 ]; then
abs=-1
else
abs=1
fi
# 通过echo返回值
echo $((diffSec/sec*abs))
}
3. 示例Shell
#!/bin/bash
. $(dirname $0)/datediff.sh
FILE_PATH="File"
EXCLUDE="File/tmp/\* File/20140505/\*"
FORMAT1="+%Y-%m-%d %H:%M:%S"
FORMAT2="+%Y%m%d%H%M"
start_time1=$(date "$FORMAT1")
start_time2=$(date "$FORMAT2")
backup_all() {
echo "$start_time1 start backup all files"
filename=$(get_filename "all" "$start_time2")
zip -r "$filename" "$FILE_PATH"/* -x "$EXCLUDE"
echo $(date "$FORMAT1") backup finished
}
backup_from() {
echo "$start_time1 start backup files from $1"
filename=$(get_filename $(date_format "$1") "$start_time2")
min=$(date_diff "-m" "$1" "now")
find "$FILE_PATH" -cmin -"$min" | zip -@ "$filename"
# zip "$filename" -d "$EXCLUDE"
echo $(date "$FORMAT1") backup finished
}
backup_range() {
echo "$start_time1 start backup files from $1 to $2"
filename=$(get_filename $(date_format "$1") $(date_format "$2"))
min1=$(date_diff "-m" "$1" "now")
min2=$(date_diff "-m" "$2" "now")
find "$FILE_PATH" -cmin -"$min1" -cmin +$(($min2-1)) | zip -@ "$filename"
# zip "$filename" -d "$EXCLUDE"
echo $(date "$FORMAT1") backup finished
}
date_format () {
date -d "$1" "$FORMAT2"
}
get_filename () {
echo "File_$1_$2.zip"
}
check_date () {
flag=$(echo "$1" | grep -cE "(^[0-9]{4}-[0-9]{2}-[0-9]{2}$)|(^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$)")
if [ "$flag" -ne 1 ]; then
echo "failure"
fi
}
if [ "$#" -eq 2 ]; then
flag1=$(check_date "$1")
flag2=$(check_date "$2")
if [ "x$flag1" = "x" ] && [ "x$flag2" = "x" ]; then
backup_range "$1" "$2"
else
echo "Please input valid date"
fi
exit
fi
case "$1" in
"-h"|"--help")
echo "Usage: $0 {all|2016-11-11|'2016-11-11 11:11'|2016-11-11 '2016-11-13 23:59'}
exit 1
;;
"all")
backup_all
;;
*)
flag=$(check_date "$1")
if [ "x$flag" = "x" ]; then
backup_from "$1"
else
echo "Please input valid date"
fi
;;
esac