有些新的檔案會被加入,而舊的檔案會被移除,檔案的內容基本上是不會變動的,所以可透過檔名來進行同步。
檔案下載與錯誤處理:
這裡用 curl 來處理下載的問題,並且當發生錯誤時重試三次。
- #!/bin/bash
- # download_file.sh
- PATH=/bin:/sbin:/usr/bin:/usr/sbin
- export PATH
- LIMIT=3 # retry 3 times
- LOCAL_PATH=$1
- REMOTE_URL=$2
- FILE_NAME=$3
- while [ $LIMIT \> 0 ] ; do
- #echo $REMOTE_URL/$FILE_NAME
- rm -f $LOCAL_PATH/$FILE_NAME.tmp
- curl -f -o $LOCAL_PATH/$FILE_NAME.tmp $REMOTE_URL/$FILE_NAME > /dev/null
- if [ "$?" == "0" ]; then
- mv -f $LOCAL_PATH/$FILE_NAME.tmp $LOCAL_PATH/$FILE_NAME
- exit 0;
- fi
- LIMIT=$(($LIMIT-1))
- done
- exit 1;
清單比較:
從遠端下載檔案清單與本地端的目錄進行比對,並處理刪除跟下載的動作。
- #!/bin/bash
- # folder_sync.sh
- PATH=/bin:/sbin:/usr/bin:/usr/sbin
- export PATH
- REMOTE_URL=http://remote_site/pics
- REMOTE_LIST_URL=http://remote_site/pics/file.list
- LOCAL_PATH=/var/www/pics
- # get remote list
- REMOTE_LIST=$(curl -f $REMOTE_LIST_URL |tr -d '\r')
- if [ "$?" != "0" ]; then exit 1; fi
- # get local list
- cd $LOCAL_PATH
- LOCAL_LIST=$(ls -1)
- if [ "$?" != "0" ]; then
- LOCAL_LIST=""
- fi
- # Check loacl file exists in the list
- for ITEM in $LOCAL_LIST ; do
- echo "$REMOTE_LIST" |grep "^$ITEM\$" > /dev/null
- if [ "$?" != "0" ]; then
- # delete old file
- rm -f $ITEM &> /dev/null
- fi
- done
- # Check remote file exists in the list
- for ITEM in $REMOTE_LIST ; do
- echo "$LOCAL_LIST" |grep "^$ITEM\$" > /dev/null
- if [ "$?" != "0" ]; then
- # download new file
- download_file.sh $LOCAL_PATH $REMOTE_URL $ITEM &> /dev/null
- fi
- done
- exit 0;
0 回應:
張貼留言