1

We have a script running which picks up the report generated monthly on remote servers. I was trying to find a way to pick up the latest file from the remote servers only. Will find work in script or that's a bad practice?

for host in "${hosts[@]}"; do
    scp "$host":"$remote_path" "$local_target_dir"/filename."$host"
done

File format = servername_BBC-3.0_2014-06-04_164510_.txt

1

You can run a ls -rt via SSH on the server within the directory to find out the last modified file (based on its last modification date instead of the filename)

fileToCopy=$(ssh "$host" "cd $remote_path && ls -rt | tail -1")
scp "$host":"$remote_path"/"$fileToCopy" "$local_target_dir"/filename."$host"
| improve this answer | |
  • surely you meant command substitution at fileToCopy='ssh "$host$" "cd $remote_path" && ls -rt | tail -1"' ? – iruvar Jun 12 '14 at 17:23
  • I meant store the name of the file inside the variable fileToCopy and then use the filename in the script given in the question. – Chirag Bhatia - chirag64 Jun 12 '14 at 17:26
  • 1
    Chirag, understood. But you are attempting to run ssh within single quotes, you must have meant back-quotes? – iruvar Jun 12 '14 at 17:27
  • @1_CR: You're right. I tested the code in parts using double quotes, but forgot that it wouldn't work in single quotes. – Chirag Bhatia - chirag64 Jun 12 '14 at 17:34
0

I suggest to find it out validating your date and have in consideration the last backup, for example:

#!/bin/bash
day=${date +%d}
last_month=${date -d "-1 month" date +%Y-%m-%d}
if [ $day -eq 15]
then
    echo "Is 15th, time to make get last backup!"
    scp -P port user@server:/dir/servername_BBC-3.0_$last_month* destination
fi
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.