Navigation

Contactez-nous

Kitpages
17 rue de la Frise
38000 Grenoble
tel : 04 58 00 33 81

Par Philippe Le Van (twitter accountplv) Dernière mise à jour : 26 June 2012

Scripter des accès FTP avec bash

Scripter des transferts FTP en shell

On a parfois envie d'exécuter des commandes FTP en cron ou dans des scripts non interactifs. (transfert d'un backup tous les jours par FTP par exemple).

Nous notons ici quelques scripts shell (en bash) qui permettent cela.

Copier un backup tous les mois sur un FTP

Dans l'exemple suivant, on a créé un fichier backup.tgz et on le transfère sur un serveur FTP.

#!/bin/sh

# constantes
HOST=ftp.monhost.com
LOGIN=mon_login
PASSWORD=mon_password
PORT=21

# le transfert lui même
ftp -i -n $HOST $PORT << END_SCRIPT
quote USER $LOGIN
quote PASS $PASSWORD
pwd
bin
put backup.tgz
quit
END_SCRIPT

Récupérer tous les fichiers d'un répertoire FTP

Dans cet exemple, on veut récupérer tous les fichiers d'un répertoire FTP sur son poste local.

Notons juste le "prompt" dans les commandes envoyées au serveur FTP, pour que le FTP nous pose pas de question à chaque fichier transféré

#!/bin/sh

# constantes
HOST=ftp.monhost.com
LOGIN=mon_login
PASSWORD=mon_password
PORT=21

# le transfert lui même
ftp -i -n $HOST $PORT << END_SCRIPT
quote USER $LOGIN
quote PASS $PASSWORD
pwd
cd le_repertoire_ou_sont_les_fichiers
bin
prompt
mget *
quit
END_SCRIPT

Récupérer de façon récursive tout le contenu d'un serveur FTP

Pour récupérer de façon récursive tous les documents d'un serveur FTP, on peut coder quelque chose avec un client FTP, mais il y a une astuce beaucoup plus simple utilisant wget.

 

#!/bin/sh

# constantes
HOST=ftp.monhost.com
LOGIN=mon_login
PASSWORD=mon_password
PORT=21

# le transfert lui même
wget -r ftp://$LOGIN:$PASSWORD@$HOST

Conclusion

N'hésitez pas à ajouter vos propres astuces dans les commentaires.

Commentaires

Ajouter un commentaire
Paragraph writing is also a fun, if you be familiar with then you can write if not it is difficult to write.
bookmarked!!, I really like your blog!
You ought to take part in a contest for one of the finest websites online.
I will recommend this site!
fantastic put up, very informative. I ponder why the other 
specialists of this sector don't realize this. You should continue your writing.
I am sure, you have a huge readers' base already!
Hi just wanted to give you a quick heads up and let you know a few of the images aren't loading 
properly. I'm not sure why but I think its a linking issue.
I've tried it in two different web browsers and both show the 
same outcome.
Heya i am for the first time here. I found this board and I find It 
really useful & it helped me out a lot. I hope to give something back and aid others like you helped me.
You really make it seem so easy with your presentation but I find this 
matter to be actually something which I think I would never understand.
It seems too complicated and very broad for me.
I'm looking forward for your next post, I will try to get the hang of it!
Great blog here! Also your web site loads up fast!

What host are you using? Can I get your 
affiliate link to your host? I wish my site loaded up as fast 
as yours lol
At this moment I am going away to do my breakfast, after having my breakfast coming over again to read further news.
Hey there terrific blog! Does running a blog such as this require a massive amount work?
I've very little understanding of computer programming but I had 
been hoping to start my own blog soon. Anyways, if you have any ideas or tips for new blog owners please 
share. I know this is off topic nevertheless I 
just wanted to ask. Many thanks!
que le premier
bonjour,
merci pour le script, par contre, quand je fais le transfert de plusieurs fichiers vers le serveur ftp, tout les mput apparaissent mais seul le premier est effectif. 

ai je oublié qq chose?
Permission Denied
Bonjour, 

nickel le script, j'ai juste un souci, j'utilise la commande put pour envoyer un fichier sur un serveur ftp, cela fonctionne avec un fichier basique de test, par contre lorsque je veux envoyer une archive tar.gz, j'ai le message "permission denied".
J'ai donné tous les droits sur le fichier, mais rien à faire.

Quelqu'un aurait une idée?

Merci
Nom fichier
Bonjour,

Une fois que l'on exécute une première fois le script, il récupère bien le fichier. 
Que se passe -t-il si on l’exécute à nouveau? Y-a-t-il un paramètre qui permet d’incrémenter le nom du fichier ou de modifier le nom du fichier à chaque récupération?
Et dans l'autre sens?
Pourrait-on faire upload avec wput au lieu de wget de la même façon?
Question
Bonjour, 

Merci cela me sauve la vie.
Par contre avec le wget  j'ai un dossier bizarre qui est créé "9R35T7~T"

Quel est ce dossier ?

Merci
pour le script de recuperation de fichiers, il faut juste changer put backup.tgz en get  backup.tgz .....
Thanx
Une inspiration
Merci
Exactement ce que je cherchais. Merci !