Permalink
Cannot retrieve contributors at this time
Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign up
Fetching contributors…
| #! /usr/bin/env bash | |
| # | |
| # v1.1.1 lftp sync script for us with WinSCP or as a standalone script. | |
| # | |
| # Credits: A heavily modified version of this idea and script http://www.torrent-invites.com/showthread.php?t=132965 towards a simplified end user experience. | |
| # Authors: Lordhades - Adamaze - userdocs | |
| # | |
| # Github - https://github.com/userdocs/lftp-windows | |
| # | |
| ###### Important Note - When using WinSCP with custom commands you only need to edit option 5 to change the download location. All other variables are passed to the script by WinSCP. | |
| ##### Editing options 1 - 4 is required. | |
| #### Editing options 5 is only required if you want to set a custom download directory andnot use the default /Download directory. Read the comments for correct formatting. | |
| ### Editing options 6 - 11 is optional. | |
| ## Editing option 10 is only required if you need to specify the tmp directory. This is the location the lock file, PID file and log file are created and checked by this script. | |
| # Editing option 11 is only needed if your ssh server uses any port that is not the default of 22. | |
| # | |
| # 1: Your sftp/ftp username goes here between the double quotes. | |
| username="" | |
| # 2: Your sftp/ftp password goes here between the double quotes. | |
| password="" | |
| # 3: Your seedbox server URL/hostname goes here between the double quotes. | |
| hostname="" | |
| # 4: The remote directory on the seedbox you wish to mirror from - set inside the single quotes. Can now be passed to the script directly using "$1". It must exist on the remote server. | |
| remote_dir='' | |
| # 5: Optional - The local directory your files will be mirrored to. It is relative to the portable folder and will be created if it does not exist so the default setting will work. | |
| # This local path is relative to the portable directory - Use the cygwin path format to specify full local paths - /cygdrive/c/download = C:\download | |
| local_dir="$HOME/../downloads" | |
| # 6: Optional - The number of parallel files to download. It is set to download 1 file at a time. | |
| parallel="3" | |
| # 7: Optional - set maximum number of connections lftp can open | |
| default_pget="16" | |
| # 8: Optional - Set the number of connections per file lftp can open | |
| pget_mirror="16" | |
| # 9: Optional - Add custom arguments or flags here. | |
| args="-c" | |
| # 10: Optional - This variable specifies the location of the tmp folder to use for the lock, PID and log file. This default should be working on both linux and windows at the same time. On Linux by using the /tmp folder and Windows by using the included tmp folder in the lftp directory. You should not need to change this unless you want to specify the tmp directory for debugging. | |
| tmpdir="/tmp" | |
| # 11: Optional - Set the SSH port if yours is not the default. | |
| port="22" | |
| # | |
| # LFTP Mirror switches - Visit the manual page for more commands - https://lftp.yar.ru/lftp-man.html | |
| # | |
| # Don't edit these checks - WinSCP custom command variables passed to the script in a specific order so we don't have to modify the script. The order is important. | |
| # | |
| # When WinSCP passes the path to the script it automatically wraps them in quotes only if the path has Unicode characters. So we need to remove them for consistency. | |
| [[ ! -z "$1" ]] && remote_dir="$(sed -e 's/^"//' -e 's/"$//' <<< "$1")" | |
| [[ ! -z "$2" ]] && username="$2" | |
| [[ ! -z "$3" ]] && hostname="$3" | |
| [[ ! -z "$4" ]] && port="$4" | |
| [[ ! -z "$5" ]] && keyname="$5" | |
| # Check to see whether $6 starts with a drive letter and is a path required for the mirror or pget to dir commands else assumes it is the password for mirror or pget commands. | |
| # If it is a URL then format the '/cygdrive/driverletter/path' url from the provided path - sed 1 converts driver letter to lowercase - sed 2 converts '\' to '/' - sed 3 removes the first instance of ':' | |
| [[ ! -z "$6" && "$6" == *\:\\* ]] && local_dir="/cygdrive/$(echo $6 | sed -re 's/^(.*):/\L\1:/' | sed 's.\\./.g' | sed -e 's/://1')" || [[ ! -z "$6" ]] && password="$6" | |
| # Password for the mirror or pget to dir command command only - not used by mirror or pget. | |
| [[ ! -z "$7" ]] && password="$7" | |
| # | |
| # on cygwin awk is a symlink to gawk. Testing for which to use. | |
| [[ -z $(whereis gawk | cut -d ':' -f 2) ]] && awkpath="awk" || awkpath="gawk" | |
| # Creates the lock file per session - hardcoded to be the same name in all scripts so that only one lftp job instance will run. | |
| lock_file="$tmpdir/lftp-winscp.lock" | |
| # | |
| # This checks to see if LFTP is actually running and if the lock file exists. It LFTP is not running and there is a lock file it will be automatically cleared allowing the script to run. | |
| [[ -z $(ps | grep /usr/bin/lftp | gawk '{print $1}') ]] && rm -f "$lock_file" | |
| # | |
| # ssh-pageant is loaded so that lftp can use our putty ppk keyfile for auth so we can use a single keyfile. | |
| eval $($HOME/bin/ssh-pageant -q -r -a "$HOME/tmp/ssh-pageant-lftp") | |
| # | |
| # A function that performs some useful commands upon the script exiting. | |
| function finish { | |
| # Kills the ssh-pageant process using the built in kill switch. | |
| "$HOME/bin/ssh-pageant" -q -k > /dev/null 2>&1 | |
| # Kills ssh-pageant using the kill command and the pid of the process. | |
| kill "$(ps x | grep ssh-pageant | gawk '{print $1}')" > /dev/null 2>&1 | |
| # Removes the ssh-pageant tmp file. | |
| rm -f "$HOME/tmp/ssh-pageant-lftp" > /dev/null 2>&1 | |
| # Removes the lock file. | |
| rm -f "$lock_file" | |
| # Remove the empty /cygdrive dir created by the script but never do this recursively or it will be interpreted as /cygdrive/c/your hard drive. | |
| rmdir "$HOME/cygdrive" 2> /dev/null | |
| } | |
| # | |
| # The trap command executes the finish function on the specified signals. | |
| trap finish EXIT SIGINT SIGTERM SIGHUP | |
| # | |
| if [[ -f "$lock_file" ]] | |
| # | |
| then | |
| echo "An lftp job is already running already." | |
| sleep 2 | |
| exit | |
| else | |
| # create the lock file | |
| touch "$lock_file" | |
| # the lftp command we use followed by the hardcoded settings - these variables are either set above or passed by WinSCP using the custom commands. | |
| lftp -p "$port" -u "$username,$password" "sftp://$hostname" <<-EOF | |
| set cache:enable no | |
| set net:timeout 10 | |
| set sftp:auto-confirm yes | |
| set pget:default-n $default_pget | |
| set mirror:parallel-transfer-count "$parallel" | |
| set mirror:use-pget-n $pget_mirror | |
| mirror $args "$remote_dir" "$local_dir/" | |
| quit | |
| EOF | |
| # | |
| pushbullet "@ $(date '+%H:%M:%S')" | |
| pushover "@ $(date '+%H:%M:%S')" | |
| # | |
| exit | |
| fi |