Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #~~~~~~~~~~~~~Move2Server Script~~~~~~~~~~~~~#
- #~~A Simple scp copy script that takes care of a lot of things ! ~#
- #~~~~~~~~~~~~~VERSION 0.1~~~~~~~~~~~~~~~~~~~~#
- # Contains Code Obtained from http://stackoverflow.com/questions/8015888/linux-script-to-find-all-video-file-extensions-and-file-sizes (Automation)#
- # GPL v2 License http://www.gnu.org/licenses/
- # Feel free to contact me @ http://prasys.info should you have an improved version of this script
- # Tested to work on Debian v6 and Ubuntu 12.04
- # Usage - simply use mv2svr <file/folder> <optional type paramater>
- # For Optional Usage , recommend to use SSH keys/Passwordless SSH Login
- hostsrv=192.168.1.107 # IP ADDRESS FOR WOL/DESTINATION SERVER
- user=prasys #Destination User (i.e the user that you want to connect as in destination server)
- mac=AA:BB:CC:DD:EE:FF # MAC ADDRESS for WOL
- wol=1 #set it to 0 to disable automatic wake on lan if the server is asleep/down
- del=1 #set it to 0 if you do not want to delete from source after transfer is completed
- TYPES=( mov mp4 avi mkv) #TYPE CHECKING FOR AUTO FILE MOVING - Currently I've included for movie file type feel free to add more :)
- #SYSTEM VARIABLES
- file=$1
- dest=$2
- #TO DO FUNCTION FOR THE AUTOMATION
- ##function function_name () {
- #statements
- }
- #CHECK if there is null or an actual file
- if [ -z "$1" ]; then
- echo ERROR: Failed to find file. Make sure you insert file
- exit
- fi
- #TODO : CONVERT THIS INTO FUNCTIONS TO MAKE AUTOMATION MUCH MORE EASIER and migrate to the function on top
- if [ -z "$2" ]; then
- echo AUTOMATIC MODE STARTED
- echo NOTE : AUTO ONLY WORKS FOR MOVIES!
- TYPES_RE="\\("${TYPES[1]}
- for t in "${TYPES[@]:1:${#TYPES[*]}}"; do
- TYPES_RE="${TYPES_RE}\\|${t}"
- done
- TYPES_RE="${TYPES_RE}\\)"
- SAVEIFS=$IFS
- IFS=$(echo -en "\n\b")
- OUTPUT=""
- #FUTURE USE ,MAY NEED THE INFO TO MAKE A SMARTER GUESS
- for f in `find ${file} -type f -regex ".*\.${TYPES_RE}"`; do
- SIZE=`stat -c "%s" ${f}`
- SIZEK=`echo "scale=2; ${SIZE} / 1024" | bc -l`
- SIZEM=`echo "scale=2; ${SIZEK} / 1024" | bc -l`
- OUTPUT=`echo ${SIZE}b / ${SIZEK}Kb / ${SIZEM}Mb - ${f}`";"$OUTPUT
- done
- IFS=$SAVEIFS
- echo $OUTPUT
- if [ -z "$OUTPUT" ]; then
- echo "NO MOVIE FILES FOUND EXITING GRACEFULLY" #exit if we can't find movie type
- exit
- else
- echo "SETTING DESTINATION AUTOMATICALLY" #we found so let's set it up
- dest="/media/data/Movies"
- fi
- #MANUAL METHOD HERE
- #Feel free to add your own else if statement to make it easier
- elif [ $2 == "games" ]
- then
- echo "Games Folder is set"
- dest="/media/data/Games" #change Me
- elif [ $2 == "movies" ]
- then
- echo "Movies Folder is set"
- dest="/media/data/Movies" #change Me
- elif [ $2 == "os" ]
- then
- dest="/media/data/OS"
- elif [[ "$2" == *\/* ]] || [[ "$2" == *\\* ]] #TAKE CARE OF CUSTOM PATH
- then
- dest=$2
- else
- echo "Invalid Command" #if there is other junk or something else that does not match
- exit
- fi
- #CHECK if the server is up or not
- if [ $wol == "1" ]; then
- ping -c 1 -w 1 -q $hostsrv >/dev/null # are we able to ping ?
- status=$?
- if [ $status -ne 0 ]; then
- /usr/bin/wakeonlan $mac
- echo "Please Wait While Server is Being Turned ON"
- sleep 60 # wait a while for the server to be up !
- fi
- fi
- #Debug messages
- #echo $dest
- #echo $file
- #Checking if it is a file or folder
- #arcfour is used because it's fastest method
- if [[ -d $file ]]; then
- scp -c arcfour -r "$file" $user@$hostsrv:$dest # if this is a file
- else
- scp -c arcfour "$file" $user@$hostsrv:$dest #if it is a folder - we copy the entire folder
- fi
- if [ "$?" -eq "0" ];
- then
- echo "SUCCESS"
- #File Removal once we have copied
- if [ $del == "1" ]; then
- rm "$file" # works for file - need to tweak later for directory with rm -r
- echo "Removed File"
- else
- echo "FAIL"
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement