prasys

mv2svr Script

Dec 28th, 2013
157
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. #~~~~~~~~~~~~~Move2Server Script~~~~~~~~~~~~~#
  3. #~~A Simple scp copy script that takes care of a lot of things ! ~#
  4. #~~~~~~~~~~~~~VERSION 0.1~~~~~~~~~~~~~~~~~~~~#
  5. # Contains Code Obtained from http://stackoverflow.com/questions/8015888/linux-script-to-find-all-video-file-extensions-and-file-sizes (Automation)#
  6. # GPL v2 License http://www.gnu.org/licenses/
  7. # Feel free to contact me @ http://prasys.info should you have an improved version of this script
  8. # Tested to work on Debian v6 and Ubuntu 12.04
  9. # Usage - simply use mv2svr <file/folder> <optional type paramater>
  10. # For Optional Usage , recommend to use SSH keys/Passwordless SSH Login
  11.  
  12. hostsrv=192.168.1.107 # IP ADDRESS FOR WOL/DESTINATION SERVER
  13. user=prasys #Destination User (i.e the user that you want to connect as in destination server)
  14. mac=AA:BB:CC:DD:EE:FF # MAC ADDRESS for WOL
  15. wol=1 #set it to 0 to disable automatic wake on lan if the server is asleep/down
  16. del=1 #set it to 0 if you do not want to delete from source after transfer is completed
  17.  
  18. TYPES=( mov mp4 avi mkv) #TYPE CHECKING FOR AUTO FILE MOVING - Currently I've included for movie file type feel free to add more :)
  19.  
  20. #SYSTEM VARIABLES
  21. file=$1
  22. dest=$2
  23.  
  24.  
  25. #TO DO FUNCTION FOR THE AUTOMATION
  26. ##function function_name () {
  27.     #statements
  28. }
  29.  
  30. #CHECK if there is null or an actual file
  31. if [ -z "$1" ]; then
  32. echo ERROR: Failed to find file. Make sure you insert file
  33. exit
  34. fi
  35. #TODO : CONVERT THIS INTO FUNCTIONS TO MAKE AUTOMATION MUCH MORE EASIER and migrate to the function on top
  36. if [ -z "$2" ]; then
  37.     echo AUTOMATIC MODE STARTED
  38.     echo NOTE : AUTO ONLY WORKS FOR MOVIES!
  39.     TYPES_RE="\\("${TYPES[1]}
  40.     for t in "${TYPES[@]:1:${#TYPES[*]}}"; do
  41.         TYPES_RE="${TYPES_RE}\\|${t}"
  42.     done
  43.     TYPES_RE="${TYPES_RE}\\)"
  44.  
  45.     SAVEIFS=$IFS
  46.     IFS=$(echo -en "\n\b")
  47.     OUTPUT=""
  48.     #FUTURE USE ,MAY NEED THE INFO TO MAKE A SMARTER GUESS
  49.     for f in `find ${file} -type f -regex ".*\.${TYPES_RE}"`; do
  50.         SIZE=`stat -c "%s" ${f}`
  51.             SIZEK=`echo "scale=2; ${SIZE} / 1024" | bc -l`
  52.             SIZEM=`echo "scale=2; ${SIZEK} / 1024" | bc -l`
  53.             OUTPUT=`echo ${SIZE}b / ${SIZEK}Kb / ${SIZEM}Mb - ${f}`";"$OUTPUT
  54.     done
  55.     IFS=$SAVEIFS
  56.     echo $OUTPUT
  57.     if [ -z "$OUTPUT" ]; then
  58.         echo "NO MOVIE FILES FOUND EXITING GRACEFULLY" #exit if we can't find movie type
  59.         exit
  60.     else
  61.         echo "SETTING DESTINATION AUTOMATICALLY" #we found so let's set it up
  62.         dest="/media/data/Movies"
  63.     fi
  64. #MANUAL METHOD HERE
  65. #Feel free to add your own else if statement to make it easier
  66. elif [ $2 == "games" ]
  67. then
  68.     echo "Games Folder is set"
  69.         dest="/media/data/Games" #change Me
  70. elif [ $2 == "movies" ]
  71. then
  72.         echo "Movies Folder is set"
  73.     dest="/media/data/Movies" #change Me
  74. elif [ $2 == "os" ]
  75. then
  76.         dest="/media/data/OS"
  77. elif [[ "$2" == *\/* ]] || [[ "$2" == *\\* ]] #TAKE CARE OF CUSTOM PATH
  78. then
  79.        dest=$2
  80. else
  81.     echo "Invalid Command" #if there is other junk or something else that does not match
  82.     exit
  83. fi
  84. #CHECK if the server is up or not
  85. if [ $wol == "1" ]; then
  86.     ping -c 1 -w 1 -q $hostsrv >/dev/null # are we able to ping ?
  87.     status=$?
  88.     if [ $status -ne 0 ]; then
  89.         /usr/bin/wakeonlan  $mac
  90.         echo "Please Wait While Server is Being Turned ON"
  91.         sleep 60 # wait a while for the server to be up !
  92.     fi
  93. fi
  94. #Debug messages
  95. #echo $dest
  96. #echo $file
  97.  
  98. #Checking if it is a file or folder
  99. #arcfour is used because it's fastest method
  100. if [[ -d $file ]]; then
  101.  
  102.     scp -c arcfour -r "$file" $user@$hostsrv:$dest # if this is a file
  103. else
  104.  
  105.     scp -c arcfour "$file" $user@$hostsrv:$dest #if it is a folder - we copy the entire folder
  106. fi
  107.  
  108. if [ "$?" -eq "0" ];
  109. then
  110.     echo "SUCCESS"
  111.     #File Removal once we have copied
  112.     if [ $del == "1" ]; then
  113.         rm "$file"  # works for file - need to tweak later for directory with rm -r
  114.         echo "Removed File"
  115. else
  116.     echo "FAIL"
  117. fi
RAW Paste Data