0

I've only got a little question for you.

I have made a little shell script that allows me to connect to a server and gather certain files and compress them to another location on another server, which works fine.

It is something in the vane of:

#!/bin/bash  

ssh -T user@server1

mkdir /tmp/logs

cd /tmp/logs

tar -czvf ./log1.tgz /somefolder/docs

tar -czvf ./log2.tgz /somefolder/images

tar -czvf ./log3.tgz /somefolder/video

cd ..

-czvf logs_all.tgz /tmp/logs

What I would really like to do is:

  1. Login with the root password when connect via ssh
  2. Run the commands
  3. Logout
  4. Login to next server
  5. Repeat until all logs have been compiled.

Also, it is not essential but, if I can display the progress (as a bar perhaps) then that would be cool!!

If anyone can help that would be awesome.

I am in between n00b and novice so please be gentle with me!!

  • Logging in with a root password like that would be a gaping security hole... I wouldn't recommend it for SSH in as root user, nor SSH in as a normal use via sript and automatically go into root since you'd have to store it somewhere readable and pass that to your login. Maybe try using Ansible or something where you can store sudo passwords or user passwords in encrypted format and specify which user to run commands as. – 264nm Apr 21 '16 at 01:10

2 Answers2

1

ssh can take a command as argument to run on the remote machine:

ssh -T user@server1 "tar -czf - /somefolder/anotherfolder"

This will perform the tar command on the remote machine, writing the tar's output to stdout which is passed to the local machine by the ssh command. So you can write it locally somewhere (there's no need for that /tmp/logs/ on the remote machine):

ssh -T user@server1 "tar -czf - /somefolder/anotherfolder" > /path/on/local/machine/log1.tgz

If you just want to collect them on the remove server (no wish to transfer them to the local machine), just do the straight forward version:

ssh -T user@server1 "mkdir /tmp/logs/"
ssh -T user@server1 "tar -cvzf /tmp/logs/log1.tgz /somefolder/anotherfolder"
ssh -T user@server1 "tar -cvzf /tmp/logs/log2.tgz /somefolder/anotherfolder"
…
ssh -T user@server1 "tar -czvf /tmp/logs_all.tgz /tmp/logs"
Alfe
  • 56,346
  • 20
  • 107
  • 159
1

You could send a tar command that writes a compressed archive to standard out and save it locally:

ssh user@server1 'tar -C /somefolder -czvf - anotherfolder' > server1.tgz
ssh user@server2 'tar -C /somefolder -czvf - anotherfolder' > server2.tgz
...
Cole Tierney
  • 9,571
  • 1
  • 27
  • 35