Created
October 10, 2011 10:28
-
-
Save xurenlu/1275006 to your computer and use it in GitHub Desktop.
bash进行目录检查和备份
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#param 1: the name of the configuration | |
#param 2: the path to backup | |
back_dir_prefix="/home/x/back/" | |
back_up(){ | |
name=$1 | |
path=$2 | |
echo $name " => " $path | |
last_back_dir="${back_dir_prefix}${name}" | |
echo $last_back_dir | |
if [ -d $last_back_dir ]; then | |
echo "dir $last_back_dir exist" | |
lines=`diff -r $last_back_dir $path | grep 'diff -r'|wc -l` | |
#lines=`diff -r $last_back_dir $path | grep diff` | |
#|wc -l` | |
#lines=`diff -r $last_back_dir $path 2>&1| grep diff|wc -l` | |
echo "" | |
echo "" | |
echo "" | |
echo $lines "个文件不同" | |
if [ $lines != "0" ];then | |
#需要重新备份; | |
tm=`date +%Y%m%d%H%M%S` | |
tar -czf ${back_dir_prefix}/${name}.${tm}.tar.gz $path | |
rsync -rv $path $last_back_dir | |
fi | |
else | |
#mkdir $last_back_dir | |
rsync -rvz --safe-links $path $last_back_dir | |
echo "dir not exists" | |
fi | |
} | |
#usage: | |
back_up "hello" "/etc/" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment