Skip to content

Instantly share code, notes, and snippets.

@Phuseos
Last active August 1, 2017 05:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Phuseos/800399e4d94646b0854258d80f95af08 to your computer and use it in GitHub Desktop.
Save Phuseos/800399e4d94646b0854258d80f95af08 to your computer and use it in GitHub Desktop.
Download FTP tree recursive with BAT and Bash
@echo off
echo Made by https://gist.github.com/Phuseos
echo Preparing to download the FTP full tree.
:: Set the dir to the dir where the BAT file is located
pushd %~dp0
:: Show the current file location for dubble checking
echo Current location: %cd%
:: Check if Bash is installed, else don't bother
IF NOT EXIST "C:\Windows\System32\bash.exe" (
echo You don't seem to have Bash installed. Exiting program.
pause
exit
)
:: Make sure the bash script we want is present in the folder
IF NOT EXIST "%cd%\ftpGet.bash" (
echo The ftpGet.bash file is missing! Can't run the command!
pause
exit
)
:: All components are present, rework the path to a unix path we can use
set "variable=%cd%\ftpGet.bash"
set "drive=%variable:~0,1%"
set variable=%variable:~2%
set "variable=%variable:\=/%"
:: Terrible, but BAT doesn't have a .toLower()
if %drive%==A set "drive=a"
if %drive%==B set "drive=b"
if %drive%==C set "drive=c"
if %drive%==D set "drive=d"
if %drive%==E set "drive=e"
if %drive%==F set "drive=f"
if %drive%==G set "drive=g"
if %drive%==H set "drive=h"
if %drive%==I set "drive=i"
if %drive%==J set "drive=j"
if %drive%==K set "drive=k"
if %drive%==L set "drive=l"
if %drive%==M set "drive=m"
if %drive%==N set "drive=n"
if %drive%==O set "drive=o"
if %drive%==P set "drive=p"
if %drive%==Q set "drive=q"
if %drive%==R set "drive=r"
if %drive%==S set "drive=s"
if %drive%==T set "drive=t"
if %drive%==U set "drive=u"
if %drive%==V set "drive=v"
if %drive%==W set "drive=w"
if %drive%==X set "drive=x"
if %drive%==Y set "drive=y"
if %drive%==Z set "drive=z"
:: Now set the direct Unix path to use
set "variable=/mnt/%drive%%variable%"
:: Run the bash command
bash %variable%
:: Simplified (thanks, https://gist.github.com/lyze237)
:: bash -c "wget -r -m --user="UserName" --password="Password" ftp://ftp.yourftpserver.com
#!/bin/bash
# Download full FTP tree recursive into current folder
wget -r -m --user="UserName" --password="Password" ftp://ftp.yourftpserver.com
@echo off
echo Preparing to download the FTP full tree.
:: Set variables to make sure we're not working on a UNC share
SET testunc=%~dp0
echo Downloading FTP tree and MySQL dump to %testunc%
SET testuncexp=%testunc:~0,2%
:: Test the path we're working on
IF "%testuncexp%"=="\\" (
echo Due to limitations in Windows, this program can't be run on a UNC share.
pause
exit
)
:: Set the dir to the dir where the BAT file is located
pushd %~dp0
cd %~dp0
:: Show the current file location for dubble checking
echo Current location: %cd%
:: Check if Bash is installed, else don't bother
IF NOT EXIST "C:\Windows\System32\bash.exe" (
echo You don't seem to have Bash installed. Exiting program.
pause
exit
)
:: Run the bash command to get the FTP dump
bash -c "wget -r -m --user="Koen" --password="password" ftp://127.0.0.1"
:: Now get the MySQL dump
bash -c "mysqldump -u kdroot --password='Password' -h 127.0.0.1 schema_name > %date%.sql"
popd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment