将backup放到PATH包含的目录里。最后几行注释只是给的一些建议,比如当想手动执行的时候,先创建个别名,当想周期执行等等…
点击(此处)折叠或打开
#!/bin/sh
log_prefix=`which backup`
log_prefix="${log_prefix%/*}"
backup_log=$log_prefix/"backup.log"
backup_dir="/var/backup"
function help() {
echo -e "Usage:\n\toption -h for help\n"\
"\toption -a for add file\n"\
"\toption -d for del file, eg -d n (n is the number of the entry, use -p get it\n"\
"\toption -p for print backup file\n"\
"\toption -c for set backup dir, default is $backup_dir"
}
function add() {
pathname=`pwd`
pathname=$pathname/$1
echo $pathname 》$backup_log
}
function del() {
total_line=`wc -l $backup_log | cut -d ' ' -f 1`
if [ -z "${1//[0-9]/}" ]&&[ "$1" -le "$total_line" ]; then
sed -i "${1}d" $backup_log >/dev/null 2>&1
fi
# sed -i "/$1/d" $backup_log >/dev/null 2>&1
}
function parse() {
while getopts "a:d:c:ph" i;
do
case "$i" in
a)
filename=$OPTARG
len=${#filename}
c=${filename:len-1:len}
if [ "$c" == '/' ]; then
filename=${filename%/}
fi
add $filename
[ "$?" == 0 ] && echo "add backup file successed" || echo "add backup file failed"
exit
;;
d)
filenamenum=$OPTARG
[ ! -f $backup_log ] && echo not "any backup info!!!" && help && exit
del $filenamenum
[ "$?" == 0 ] && echo "del backup file successed" || echo "del backup file failed"
exit
;;
c)
if [ "$OPTARG" == "." ]; then
OPTARG=`pwd`
fi
backup_dir=$OPTARG"/backup"
;;
p)
[ ! -f $backup_log ] && echo not "any backup info!!!" && help && exit
cat -n $backup_log | awk '{printf("[%s]\t%s\n", $1, $2)}'
exit
;;
h)
help && exit
;;
*)
help && exit
;;
esac
done
}
(
flock -s 200
parse $@
[ ! -f "$backup_log" ] && echo "backup.log is empty!!!" && help && exit
entry=`cat $backup_log`
[ "$entry" == "" ] && exit
[ ! -d "$backup_dir" ] && mkdir -p $backup_dir
for i in $entry
do
rsync -azv $i $backup_dir
done
)200>/var/lock/abc
#*/1 * * * * /usr/bin/backup >/dev/null 2>&1
#alias backup='flock -s /var/lock/abc backup'
本文来源:中国IT实验室 作者:佚名