New on LowEndTalk? Please Register and read our Community Rules.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
SFTP over shell script
Hey,
I try to create a shell script which connects to a server over sftp and uploads a file. I used it with FTP and it worked great but with SFTP I'm stuck. Here is a piece of my code:
#!/bin/bash echo "Backup uploading" sleep 1 sftp [email protected] put testfile /root/ sleep 1
I created the ssh keys and the connection without password works when I do it over my normal shell. But not as shell script. The script connects to the server but put doesn't work as it should.
Thanks for your help
Comments
Why not use rsync and lock the key down to rsync server only?
I don't want to use a program for that. I just need a simple sh script.
From a quick search I think you will find this reference helpful:
http://stackoverflow.com/questions/5386482/how-to-run-the-sftp-command-with-a-password-from-bash-script
sftp -oBatchMode=no -b - sftp-user@remote-host << ! cd incoming put your-log-file.log bye !
Also, just putting this out there, it is a LOT easier to use scp in this instance than sftp and they both accomplish the same thing. With scp it would look like:
scp <file> [email protected]:/directory/on/destination/for/file/
Which is mush more simple than having to fight with sftp in my opinion.
Either way, hope this helps.
Cheers!
I'm not sure what you mean, rsync is includes by default in most distros. Using SCP as the above poster suggested is probably the best way to go if you don't want to use rsync for some reason.
@TheLinuxBug
Thank you so much! I will use scp for that. It's a lot easier.
@nunim
I don't have anything against rsync, it's an awesome program for sure. But I prefer having my own small shell script so I can add features and something like that later
Just a little question. I create an archieve with tar witht this command:
tar czfv $backup_name.tar.gz $backup_pfad
and then tar gives that message:
tar: Removing leading / from member names /root/test/ /root/test/auto /root/test/flugzeug
But everything works great. What does tar want to tell me? :P
http://www.linuxquestions.org/questions/linux-general-1/bin-tar-removing-leading-`-'-from-member-names-269508/
That
Thanks. I don't really understand it: does this mean tar normally uses relative paths and I use absolute paths? I fixed the error message with tar czPfv
Just for the hell of it try this:
Doesn't work as well. But I already use SCP which works great
As I said I use SCP now, but I wanted to limit the directory of the users to their homefolder. I used openssh and this tutorial: http://blog.swiftbyte.com/linux/allowing-sftp-access-while-chrooting-the-user-and-denying-shell-access/
It works great for SFTP, but of course I can't use scp now. Is there a way to enable SFTP and SCP? I need both.
It looks like I need my files to be transfered over sftp. So I can't use scp anymore. Can you please help me to make it working over SFTP?