- #!/bin/bash
-
olddir=~/shell
-
newdir=/media/LinuxShare/backup
-
-
# 若切换目录成功,则返回0,否则返回1
-
cd $newdir 2> /dev/null && cd $olddir 2> /dev/null
-
if [ $? != 0 ]
-
then
-
echo "Can not find directory $newdir or $olddir,backup fails." | mailx -s "Backup Notification" iamaric@139.com
-
exit
-
fi
-
-
i=0
-
-
cd $olddir
-
# 遍历目录所有文件
-
for filename in *
-
do
-
cd $newdir
-
if [ -e "$filename" ]
-
then
-
if ! diff $newdir/$filename $olddir/$filename > /dev/null
-
then # return 0
-
mv $newdir/$filename $newdir/$filename.$(date +%Y-%m-%d)
-
cp $olddir/$filename $newdir
-
((i++))
-
fi
-
elif [ ! -e "$filename" ]
-
then
-
cp $olddir/$filename $newdir
-
((i++))
-
fi
-
done
-
-
if [ $i -gt 0 ]
-
then
-
echo "$i file(s) have been updated.Backup succeed." | mailx -s "Backup Notification" iamaric@139.com
-
else
-
echo "Nothing new to backup." | mailx -s "Backup Notification" iamaric@139.com
- fi