SFTP connexion + download all files AND all repertories

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
gazza8
Posts: 5
Joined: 19 Nov 2013 03:55

SFTP connexion + download all files AND all repertories

#1 Post by gazza8 » 19 Nov 2013 04:10

Hello,

I would like to create a Windows task, run every day at 11.00 am. It has to:
- connect by SFTP to a distant Ubuntu server
- download a full repertory (all its files AND all its sub-repertories except two of them)

To use PSFTP, I have already created a [backup.bat] file:

Code: Select all

@echo off
set $dir="D:\dokeos\batchbackupauto\"
pushd %$dir%
psftp.exe


And I've created a second file [ftpcommands.scr] :

Code: Select all

open [thehostname] 222
# [login]
# [password]
lcd D:\dokeos\backup\front\test
cd /home/thedistantrepertory
mget *.*
bye



=> Two questions:
1) In [backup.bat], how to tell to psftp.exe that it has to read the [ftpcommands.scr] file?
2) In [ftpcommands.scr] file, about the "mget" command, how to precise that it has to download all files AND all the sub-repertories?


Thanks in advance :)

AiroNG
Posts: 46
Joined: 17 Nov 2013 15:00
Location: Germany

Re: SFTP connexion + download all files AND all repertories

#2 Post by AiroNG » 19 Nov 2013 04:49

Hi,
to your first question:
while I was working on something similar i noticed that I could use following line in batch:

Code: Select all

ftp -s:NameOfFile.txt

It would read the contents of "NameOfFile.txt" and apply it to the ftp-prompt.

Though I'm not familliar with psftp.exe, a little bit of googling let me come across this:

Code: Select all

psftp frontrow@192.168.1.50 -pw aaa -b psftp.bat

From the looks of it the -b switch seems to work in psftp.exe like the -s switch does in ftp.exe

code-snippet from: http://stackoverflow.com/questions/694203/controlling-psftp-in-a-windows-batch-file

To your second question:
I would guess that "mget *.*" would get every single file in the current folder.
If you would want to download the content of another folder,
you would have to specify that folder. maybe something along the lines of: "mget /diffrentFolder/*.*"

:!: Once again: I have no experience using "psftp.exe" so my answer, when it comes to "mget", could be wrong :!:

gazza8
Posts: 5
Joined: 19 Nov 2013 03:55

Re: SFTP connexion + download all files AND all repertories

#3 Post by gazza8 » 19 Nov 2013 08:56

Thanks for your prompt reply :)

However, the snippet:

Code: Select all

psftp frontrow@192.168.1.50 -pw aaa -b psftp.bat

does not work.

Two remarks:
1) I have to user the 222 port: do I need to precise it? If yes, how?
Something like :

Code: Select all

psftp frontrow@192.168.1.50 222

does not work...

2) You write:

Code: Select all

psftp frontrow@192.168.1.50 -pw aaa -b psftp.bat

What is [psftp.bat] ?

Linked to my first post, I think it should be:

Code: Select all

psftp frontrow@192.168.1.50 -pw aaa -b ftpcommands.scr

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: SFTP connexion + download all files AND all repertories

#4 Post by Squashman » 19 Nov 2013 09:26

Would probably help you a lot if you read the documentation for PSFTP.
http://the.earth.li/~sgtatham/putty/0.5 ... pter6.html

gazza8
Posts: 5
Joined: 19 Nov 2013 03:55

Re: SFTP connexion + download all files AND all repertories

#5 Post by gazza8 » 19 Nov 2013 10:23

Ok, i fix it :)

Code: Select all

psftp login@server -P 222 -pw aaa -b ftpcommands.scr


Last question: how to download all files AND all subdirectories? The command:

Code: Select all

mget *.*

deals only with files...

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: SFTP connexion + download all files AND all repertories

#6 Post by Squashman » 19 Nov 2013 10:43

Just for clarification on your script command file.
You can name your script file anything you want. You don't need to name it ftpcommands.scr. You could certainly name it PSFTP.bat or anything else you wanted. It is just a text file and you are just telling PSFTP what the name of the file is to read the commands from. Heck, go name it foo.bar if you want.

AiroNG
Posts: 46
Joined: 17 Nov 2013 15:00
Location: Germany

Re: SFTP connexion + download all files AND all repertories

#7 Post by AiroNG » 19 Nov 2013 11:38

gazza8 wrote:Last question: how to download all files AND all subdirectories? The command:[...]
deals only with files...


one way would be to add a few lines in your ftpcommands.scr.
Example:

Code: Select all

open [thehostname] 222
# [login]
# [password]
lcd D:\dokeos\backup\front\test
cd /home/thedistantrepertory
mget *.*
cd /home/otherfolder
mget *.*
cd /home/nextfolder
mget *.*
bye


Assuming directory jumping works on ftp (it's been a while for me) it seems to be a fast yet inelegant solution.
Trouble should only arise if the names of the subdirectories would change,
because you would have to change them in your script as well.

another way would make use of a diffrent program called "wget".
http://www.gnu.org/software/wget/manual/wget.html#Examples

gazza8
Posts: 5
Joined: 19 Nov 2013 03:55

Re: SFTP connexion + download all files AND all repertories

#8 Post by gazza8 » 20 Nov 2013 04:25

@Squashman:
You can name your script file anything you want

Thanks for the clarification :)

@AiroNG:
There are plenty of "dynamic" subdirectories, so it would be unuseful to write a list...
The way I'm seeking for is to collect all the content (files, subdirectories, sub-subdirectories) at once.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: SFTP connexion + download all files AND all repertories

#9 Post by foxidrive » 20 Nov 2013 04:36

wget is a useful tool as AiroNG has said.

Standard FTP isn't designed to easily recurse through folders.

gazza8
Posts: 5
Joined: 19 Nov 2013 03:55

Re: SFTP connexion + download all files AND all repertories

#10 Post by gazza8 » 27 Nov 2013 07:19

Thanks a lot for your help :)

Finally I choose to do a global archive of the directory.

1) To create the archive via the [crontab] file of the remote server:

Code: Select all

tar -cvzf /home/www/website/backup.tar.gz /home/backup


2) To download the archive via the [ftpcommands.scr] file:

Code: Select all

lcd D:\dokeos\backup
cd /home/backup
mget backup.tar.gz
bye

Post Reply