LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 10-22-2019, 07:12 AM   #1
hatfani
LQ Newbie
 
Registered: Oct 2019
Posts: 7

Rep: Reputation: Disabled
Copy&delete script between servers


Hey everyone,

I have 2 remote Servers running Linux centos: Server 1 and Server 5.
On Server 1 I have a directory named: 'Example'
I need to copy 'Example' directory from Server 1 to Server 5 and then delete 'Example' directory from Server 1 (if there's a possibility to do it by one command it would be better of course).
I would like to create a script for those actions, I saw that there are some options with scp/rsync commands.
I think that I need to set up ssh-key too so they'll do it automatically without the need of authentications all the time - would like to have more information about how do I do that.

Thank you!
 
Old 10-22-2019, 07:22 AM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,639

Rep: Reputation: 754Reputation: 754Reputation: 754Reputation: 754Reputation: 754Reputation: 754Reputation: 754
yes rsync

man rsync


and yes

https://wiki.centos.org/HowTos/Network/SecuringSSH
Use Public/Private Keys for Authentication
 
Old 10-22-2019, 07:45 AM   #3
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,218
Blog Entries: 3

Rep: Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095
While reading the manual page for rsync, pay special attention to the options --dry-run, --archive, and either --remove-source-files or --delete.

Then once you have that worked out, and you have the keys working separately, look at the --rsh option.
 
Old 10-22-2019, 09:13 AM   #4
hatfani
LQ Newbie
 
Registered: Oct 2019
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
While reading the manual page for rsync, pay special attention to the options --dry-run, --archive, and either --remove-source-files or --delete.
I'm using rsync like that while i'm connected to server 1:
rsync -rav ip.of.server.5:_path/to/Example_directory /path/to/my_directory

It is copying the directory to server 1, but I don't know how to remove (once all of the directories & files are transferred) Example directory from server 5.
--remove-source-files is not helping since it's only for files, and I need recursive removal for the directory.
 
Old 10-22-2019, 09:21 AM   #5
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,218
Blog Entries: 3

Rep: Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095
Please note the trailing slash on each path:

Code:
rsync --dry-run \
        --archive \
        --verbose \
        ip.of.server.5:_path/to/Example_directory/ /path/to/my_directory/
Also, --archive includes many options including --recursive, so please review that part of the manual page.

So this will delete the source files after the copy on the destination is confirmed:

Code:
rsync --dry-run \
        --archive \
        --remove-source-files \
        --verbose \
        ip.of.server.5:_path/to/Example_directory/ /path/to/my_directory/
Then try it with SSH key, one that is known to work already:

Code:
rsync --dry-run \
        --archive \
        --remove-source-files \
        --rsh 'ssh -i ~/.ssh/some_key_ed25519' \
        --verbose \
        ip.of.server.5:_path/to/Example_directory/ /path/to/my_directory/
 
1 members found this post helpful.
Old 10-22-2019, 09:28 AM   #6
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,218
Blog Entries: 3

Rep: Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095
As for removing the empty directories from the source, that has to be done by another program such as find. The rsync utility does not do that for directories, only files.

Code:
ssh -i ~/.ssh/some_key_ed25519 \
        ip.of.server.5 \
        'find _path/to/Example_directory/ -type d -empty -print -delete'
 
2 members found this post helpful.
Old 10-22-2019, 11:51 AM   #7
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,639

Rep: Reputation: 754Reputation: 754Reputation: 754Reputation: 754Reputation: 754Reputation: 754Reputation: 754
Quote:
Originally Posted by Turbocapitalist View Post
As for removing the empty directories from the source, that has to be done by another program such as find. The rsync utility does not do that for directories, only files.

Code:
ssh -i ~/.ssh/some_key_ed25519 \
        ip.of.server.5 \
        'find _path/to/Example_directory/ -type d -empty -print -delete'
ahh, yes I had forgotten about rsync not deleting dirs

I would also add username@ip.of.server.5

not needed if username matches between server 1 and server 5
 
Old 10-22-2019, 02:47 PM   #8
hatfani
LQ Newbie
 
Registered: Oct 2019
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
ssh -i ~/.ssh/some_key_ed25519 \
ip.of.server.5 \
'find _path/to/Example_directory/ -type d -empty -print -delete'
[/code]
Just want to have a clarification - if I set up a passphrase, it still connect automatically to the server? or I'll have to insert my passphrase to a prompt?

Anyway, just want to tell you guys thank you so much for the support.
 
Old 10-22-2019, 02:55 PM   #9
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,639

Rep: Reputation: 754Reputation: 754Reputation: 754Reputation: 754Reputation: 754Reputation: 754Reputation: 754
as long as you have not setup the key with a passphrase you will not be prompted for one
 
Old 10-23-2019, 02:18 AM   #10
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,218
Blog Entries: 3

Rep: Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095Reputation: 2095
Quote:
Originally Posted by hatfani View Post
Just want to have a clarification - if I set up a passphrase, it still connect automatically to the server? or I'll have to insert my passphrase to a prompt?

Anyway, just want to tell you guys thank you so much for the support.
No problem. There are a bunch of ways to solve that but it depends on how you are going to be running the script. If it's via a cron job then you have to do some contortions to use an agent. But otherwise, if you are doing this from a regular login session then you most likely already have an agent running.

Code:
ssh-add ~/.ssh/some_key_ed25519

ssh -i ~/.ssh/some_key_ed25519 \
        -l remoteuser \
        ip.of.server.5 \
        'find _path/to/Example_directory/ -type d -empty -print -delete'
If you have a lot of keys then you'll need to set IdentitiesOnly=yes either as a runtime arguent or as an option in the client configuration file, ~/.ssh/config
 
Old 10-24-2019, 03:23 AM   #11
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,228

Rep: Reputation: 563Reputation: 563Reputation: 563Reputation: 563Reputation: 563Reputation: 563
Do not configure a passphrase when making ssh keys!

Another way is an NFS mount.
On server5 enable NFS server and export the target directory to server1 and add a root=server1 trust.
On server1 enable autofs service. Then you can copy or move files to /net/server5/path/to/targetdir/
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy file between servers from script gr0undz3r0 Linux - Newbie 2 11-13-2006 12:48 PM
have direfenre between >&, &>, 2>&1 and 1>&2 ? xcore_on Linux - Newbie 1 06-04-2006 01:04 AM
AOL UK && BT Voyager 100 && Slackware 10.2 && RP-PPPoE pitt0071 Linux - Networking 3 01-17-2006 07:10 AM
Phục hồi dữ liệu bị mất???, cứ pollsite General 1 06-27-2005 01:39 PM
Gotta love those ٱٱٱٱٱٱٱ&# iLLuSionZ Linux - General 5 11-18-2003 08:14 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 04:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration