Download script

Just a little script to download files from my remote server and if need be unrar the files.

#!/bin/bash
LOCAL="/opt/files"
UNRAR="$LOCAL/extract"
LOG="$LOCAL/download.log"
REMOTE="server.com.au:path/to/files"
INPUT=$2

function init() {
  # create required directories
  [ ! -d "$LOCAL" ] && mkdir $LOCAL && echo "created $LOCAL"
  [ ! -d "$UNRAR" ] && mkdir $UNRAR && echo "created $UNRAR"
}

function download() {
  # append wildcard to input
  [ -n "$INPUT" ] && INPUT="${INPUT}*" && echo "Downloading $INPUT"
  [ -z "$INPUT" ] && INPUT="*" && echo "Downloading all files"
  # rsync files from remote server to local folder
  rsync -a --quiet --compress --log-file="$LOG" $REMOTE/$INPUT $LOCAL/ >> $LOG 2>&1 &
}

function unrar() {
  echo "Extracting *.rar files to $UNRAR"
  # unrar convert filenames to lower case, exclude paths and do not overwrite
  find $LOCAL -name "*.rar" -exec unrar x -cl -ep -o- {} $UNRAR \; >> $LOG 2>&1 &
}

init
case "$1" in
  download|d)
    download
    ;;
  unrar|u)
    unrar
    ;;
  both|b)
    download
    unrar
    ;;
  *)
    echo "Usage: $0 {download|unrar|both}"
esac

Leave a Reply