自动备份脚本 第二版发布啦
脚本比上一版新增了,记录时间戳功能。可以将你实时修改的文件进行备份。适合一些个人照片,文档,代码的备份;
./backup_file 目录下 应该有一个 list_file 目录用来存储文件列表和文件时间戳。
$1 为你需要备份的目录 。 $2 为备份到的目录
$1 不能为空, 后续会继续修改一些bug, 并进一步完善脚本,
若你发现那一段代码存在bug ,可以告知博主。或者你有更好的实现方法,也可以一起探讨。
还有现在这个脚本,目前还不能备份子目录,只能备份文件。 以后发布将会加上这个对子目录备份的功能。
#!/bin/sh# name backup file # author acanoe # timedate 2012_11_9 13:50 # versions0.1judge_your_input(){if [ -z $1 ] ; thenecho "please input your updatedir!"echo ""echo "example:"echo "./back_file /backdir /updatedir"echo "like this example"echo ""exitfiif [ -z $2 ] ; thenecho "please input your backdir!"echo ""echo "example:"echo "./back_file /backdir /updatedir"echo "like this example"echo ""exitfi}clear_record(){if [ -e ./list_file/old_time_ioc ] ; thenrm ./list_file/old_time_iocfiif [ ! -e ./list_file/new_time_ioc ] ; thenecho "create a new_time_ioc"touch ./list_file/new_time_iocfiif [ -e ./list_file/new_time_ioc ] ; thenmv ./list_file/new_time_ioc ./list_file/old_time_iocelsetouch ./list_file/old_time_iocfi}find_change_file(){ls $1 > ./list_file/update_file_listls $2 > ./list_file/back_file_listwhile read linedo R=$(echo $line)echo "$R `stat $1/$R | sed -n '7p'`" >> ./list_file/new_time_iocif [ ! -e ./list_file/new_time_ioc ] ; thenecho "create a new_time_ioc"touch ./list_file/new_time_iocfidone < ./list_file/update_file_list# while read line# do # R=$(echo $line)# echo "$R `stat $2/$R | sed -n '7p'`" >> ./list_file/old_time_ioc# echo "$R"# done < ./list_file/back_file_listdiff ./list_file/new_time_ioc ./list_file/old_time_ioc > ./list_file/diff_filesed -n '/</p' ./list_file/diff_file > ./list_file/filesed -e 's/< //g' ./list_file/file > ./list_file/the_filesed -e 's/< //g' ./list_file/file > ./list_file/the_file}back_back_file(){while read linedo R=$(echo $line | sed -e 's/ .*//g')echo $Rcp $1/$R $2done < ./list_file/the_file}find_not_exist(){ls $1 > ./list_file/file_listls $2 > ./list_file/back_listdiff ./list_file/file_list ./list_file/back_list > ./list_file/diff_filesed -n '/</p' ./list_file/diff_file > ./list_file/filesed -e 's/< //g' ./list_file/file > ./list_file/the_filewhile read linedo R=$(echo $line)echo $Rcp -rf $1/$R $2 done < ./list_file/the_file}judge_your_input $1 $2clear_record find_change_file $1 $2back_back_file $1 $2find_not_exist $1 $2
- 1楼colorant昨天 10:08
- 用rsync不就完了么?
- Re: ACanoe昨天 11:22
- 回复colorantn呵呵,是为了学习shell 编程,才写这个应用的,不是为了完成这个任务,而编写应用。 要备份的话, svn 更强大。我就想练习一下shell ,sed 编程。