Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ME=$(whoami)
- RSYNC_DATE=$(date "+%Y-%m-%d")
- RSYNC_HOST="host-www01"
- RSYNC_DEST="/home/proxy/"
- RSYNC_SOURCE="/home/proxy/"
- EXCLUDE_LIST="$HOME/common/exclude_list.txt"
- LOG_DIR="$HOME/logs/rsync_to_drc"
- LOG_FILE="$LOG_DIR/rsync_backup."$RSYNC_DATE".log"
- STATUS_FILE="/tmp/sync_"$ME".status"
- COUNTER=1
- SLEEP_TIME=4
- function say_now() {
- date +"[%Y-%m-%d %H:%M:%S]"
- }
- function preprepare() {
- mkdir -p "${LOG_DIR}" > /den/null 2>&1
- touch "${LOG_FILE}" > /dev/null 2>&1
- }
- preprepare
- while [ "$COUNTER" -le 3 ]
- do
- echo "$(say_now)[rsync] Start." >> "$LOG_FILE"
- /usr/bin/rsync --progress -ahvzP --delete --exclude-from="$EXCLUDE_LIST" "$RSYNC_SOURCE" "$USER@$RSYNC_HOST:$RSYNC_DEST" >> "$LOG_FILE"
- ret_rsync="$?"
- if [[ "$ret_rsync" == "0" ]] ; then
- echo "$(say_now)[rsync] Completed. Exit." >> "$LOG_FILE"
- if [ -f "$STATUS_FILE" ]; then
- echo "0" > "$STATUS_FILE"
- else
- echo "$(say_now)[rsync][ERROR] Status file does not exist ! Creating the file ... " >> "$LOG_FILE" touch $STATUS_FILE
- echo "0" > "$STATUS_FILE"
- fi
- else
- echo "$(say_now)[rsync] Failure. Retrying in "$SLEEP_TIME" seconds..." >> "$LOG_FILE"
- sleep "$SLEEP_TIME"
- echo "$(say_now)[rsync] Retrying." >> "$LOG_FILE"
- if [ "$COUNTER" -ge 3 ]; then
- echo "Rsync has failed three times " >> "$LOG_FILE"
- if [ -f "$STATUS_FILE" ]; then
- echo "$ret_rsync" > $STATUS_FILE
- else
- echo "$(say_now)[rsync][ERROR] Status file does not exist ! Creating the file ... " >> "$LOG_FILE"
- touch $STATUS_FILE
- echo "$ret_rsync" > $STATUS_FILE
- fi
- exit
- fi
- fi
- COUNTER=$(( $COUNTER + 1 ))
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement