Recently, our team file server need to auto backup, so I write this script to back via contab.
This script is very easy, I just want save it for review later.
If it can help you, I will happy.
- #!/bin/sh
- log_path=/tmp
- echo ===========Start-date================= >> $log_path/backup.log
- date >> $log_path/backup.log
- source_path=/tmp/liangtest/shell
- folder_name=bk
- target_path=/tmp/liangtest/shell2
- bakup_date=`date "+%Y_%-m_%-d_%H_%M"`
- echo "Backup Date ${bakup_date}" >>$log_path/backup.log
- mkdir $target_path/${folder_name}_$bakup_date >>$log_path/backup.log
- cp $source_path/$folder_name/* $target_path/${folder_name}_$bakup_date
- sleep 5
- source_size=`du -sm $source_path/$folder_name`
- target_size=`du -sm $target_path/${folder_name}_$bakup_date`
- echo "source_size ${source_size}" >>$log_path/backup.log
- echo "target_size ${target_size}" >>$log_path/backup.log
- so=`echo $target_size|awk '{print $1;}'`
- ta=`echo $target_size|awk '{print $1;}'`
- if [ $so -eq $ta ]; then
- echo "Backup Done" $bakup_date >>$log_path/backup.log
- else
- echo "Backup Failure!!!!" >>$log_path/backup.log
- fi
- ####Starting Compress####
- echo ==========Starting Compress============ >> $log_path/backup.log
- date >> /tmp/backup.log
- if
- cd $target_path/${folder_name}_$bakup_date
- tar -cvzf $target_path/${folder_name}_Compressed_$bakup_date *
- then
- cd $target_path
- rm -rf $target_path/${folder_name}_$bakup_date
- echo "Backup Done,Delete folder Success!!!" $bakup_date >>$log_path/backup.log
- else
- echo "Compress Failure!!!!" >>$log_path/backup.log
- fi
- echo ===========End-date================= >> $log_path/backup.log
- date >> $log_path/backup.log
The output for log as below
- ===========Start-date=================
- Sun Jul 15 14:42:23 CST 2012
- Backup Date 2012_7_15_14_42
- source_size 2 /tmp/liangtest/shell/bk
- target_size 2 /tmp/liangtest/shell2/bk_2012_7_15_14_42
- Backup Done 2012_7_15_14_42
- ===========Starting Compress===========
- Sun Jul 15 14:42:33 CST 2012
- Backup Done,Delete folder Success!!! 2012_7_15_14_42
- ===========End-date====================
- Sun Jul 15 14:42:33 CST 2012
本文转自 hsbxxl 51CTO博客,原文链接:http://blog.51cto.com/hsbxxl/931593,如需转载请自行联系原作者