SCP + FIND from local [SOLVED]

Forum rules
Before you post please read how to get help
Post Reply
User avatar
zip
Level 1
Level 1
Posts: 9
Joined: Wed Sep 27, 2017 8:19 am

SCP + FIND from local [SOLVED]

Post by zip » Wed Jul 25, 2018 5:03 am

Hello! Need a help.

The task is:

Connect from local to remote, find there some files and SCP them to the local from remote.

Now I'm use (and it works well):

scp -r user@server:/some/directory /home/backups

but I want to copy only FIND /some/directory -mtime +1

How I can compose both this command?

P.S.

I know, that exist a metod:

find /some/directory -mtime +1 -exec scp -r /some/directory user@server:/home/backups {} \;

but I need to initialise connection from the local computer, not from remote.
Last edited by zip on Mon Jul 30, 2018 5:26 am, edited 1 time in total.

User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1438
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: SCP + FIND from local

Post by Pilosopong Tasyo » Wed Jul 25, 2018 7:03 pm

Use ssh to run the find command remotely that will eventually scp those files from the remote machine to the local host, i.e. ssh <user@server> <command>
Image
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].

User avatar
zip
Level 1
Level 1
Posts: 9
Joined: Wed Sep 27, 2017 8:19 am

Re: SCP + FIND from local

Post by zip » Thu Jul 26, 2018 4:20 am

Pilosopong Tasyo wrote:
Wed Jul 25, 2018 7:03 pm
Use ssh to run the find command remotely that will eventually scp those files from the remote machine to the local host, i.e. ssh <user@server> <command>
Yea, that's true, but I can't find the correct syntax.

rene
Level 8
Level 8
Posts: 2097
Joined: Sun Mar 27, 2016 6:58 pm

Re: SCP + FIND from local

Post by rene » Fri Jul 27, 2018 8:35 am

scp is a bit annoying in this regard seeing as how it doesn't automatically create leading directories; I'd advise using rsync. I.e.,

Code: Select all

rsync -avn --files-from <(ssh user@server find /some/directory -mtime +1 -printf "%P\\\n") user@server:/some/directory /home/backups/
Remove the -n (--dry-run) once satisfied this does what you want.

galaxy0815

Re: SCP + FIND from local

Post by galaxy0815 » Fri Jul 27, 2018 2:33 pm

Code: Select all

for i in $(ssh user@server find /some/directory -type d -mtime +1);
do
  scp -r user@server:$i /home/backups
done
In a different way you can run a script job on the server.

Code: Select all

for i in $(ssh user@server ~/find_my_dirs);
do
  scp -r user@server:$i /home/backups
done
In ~/find_my_dirs on the server you can write any search jobs in any scripting language with many lines of code you want.
zip wrote:
Wed Jul 25, 2018 5:03 am
but I need to initialise connection from the local computer, not from remote.
If you meant which directory is to be searched for relevant files you can do that:

Code: Select all

for i in $(ssh user@server "~/find_my_files $SEARCH_DIR);
do
  scp -r user@server:$i /home/backups
done

User avatar
zip
Level 1
Level 1
Posts: 9
Joined: Wed Sep 27, 2017 8:19 am

Re: SCP + FIND from local

Post by zip » Mon Jul 30, 2018 5:22 am

rene wrote:
Fri Jul 27, 2018 8:35 am
scp is a bit annoying in this regard seeing as how it doesn't automatically create leading directories; I'd advise using rsync. I.e.,

Code: Select all

rsync -avn --files-from <(ssh user@server find /some/directory -mtime +1 -printf "%P\\\n") user@server:/some/directory /home/backups/
Remove the -n (--dry-run) once satisfied this does what you want.
Thank you! That is just what I need. And this solution works!

Post Reply

Return to “Scripts & Bash”