0

I want to build a shell script that runs daily and fethces a file from another server using SCP command.

The problem is that when i enter this command in terminal it asks me for password.

a) How can a shells script enter password?

b) Is it really a good idea to store the password in a shell script?

Command example:

scp -r myUser@myServer:/local/java/log/archive/2018/02/16/log.2018-02-16.gz ~/Temp/
Solo
  • 57
  • See https://askubuntu.com/a/85655/158442 and comments for entering passwords in a shell script – muru Mar 02 '18 at 09:47

1 Answers1

5

First step : setting a password-less SSH access (via ssh-keygen) :

Run ssh-keygen (you end up with 2 files : id_rsa and id_rsa.pub, one is your .pub public key to share with the world, the other your private key). Then run ssh-copy-id myUser@myServer. Just type your password when prompted and you're now able to SSH without having to type your password for myUser@myServer.

Second step : automated daily scp :

Type crontab -e, pick your favorite editor and add a line at the bottom of the file : 0 0 * * * scp -r myUser@myServer:/local/java/log/archive/2018/02/16/log.2018-02-16.gz ~/Temp/ save and... Voilà! (you can use a @daily scp (...) crontab entry, wich is basicaly the same as 0 0 * * *)

See Anacron (Job scheduling using crontab, what will happen when computer is shutdown during that time?) for managing Cron task when you computer is down.