1

I have jenkins on a remote machine and the pipeline scripts are saved in the path /var/lib/jenkins/jobs in .xml format files. I can easily list the .xml files that are in that folder with this command:

find /var/lib/jenkins/jobs -name config.xml

which results in:

/var/lib/jenkins/jobs/productA/config.xml
/var/lib/jenkins/jobs/productB/config.xml
/var/lib/jenkins/jobs/productC/config.xml
/var/lib/jenkins/jobs/productD/config.xml
/var/lib/jenkins/jobs/productE/config.xml

From my local machine I have already managed to make the recursive copy of the folder in remote machine (Remote File to a Local Machine):

scp -r [email protected]:/var/lib/jenkins/jobs/ /mnt/c/backupJenkins/

What I would like to do is make a find that I described at the beginning of the question to use as input in scp.

There is a question that has already been asked here on the forum very similar, but the find command it uses on the local machine to transfer to remote:

scp --exec=`find <path> -name <expression>` user@host:<path_where_to_copy>

Here some attempts I've already made based on my internet research that I also adapted, but they didn't work:

scp [email protected]:$`find /var/lib/jenkins/jobs -name config.xml` /mnt/c/backupJenkins
for f in `find /var/lib/jenkins/jobs -name config.xml` ; do 
scp [email protected]/$f /mnt/c/backupJenkins
done

An scp command looks like this:

scp [other options] [source username@IP]:/[directory and file name] [destination username@IP]:/[destination directory]

In 'other options' I think that passing the -p parameter would even be able to maintain the folder structure

1
  • Is there a reason why you can't just ssh into the remote sever first? If you were on the remote, your 2nd attempt would be close to correct. Sep 23, 2022 at 4:22

1 Answer 1

0

I can´t make work with a single command, but with two it worked

Command A: ssh [email protected] 'cd /var/lib/jenkins/jobs; find . -name config.xml ! -path '*/backup*' | rsync -avz --files-from=- . ./backup'

'cd /var/lib/jenkins/jobs; - After ssh on my remote machine, I change directory to /var/lib/jenkins/jobs

find . -name config.xml ! -path '*/backup*' - Then make find and exclude the path of folder '/backup', this prevent rsync to copy 'backup' folder recursively.

rsync --files-from=- SRC/ DST/ using this format for the rsync command we add the parameters -avz and --files-from=-

| rsync -avz --files-from=- . ./backup' - The single dash - filename here means STDIN, and this is a common convention among unix tools

The command B we just use rsync to copy a File from a Remote Server to a Local Server with SSH, and save on my local machine on new folder DAY-MONTH-YEAR ( /mnt/c/backupJenkins/$(/bin/date +%d-%m-%Y) )

Command B: rsync -avz [email protected]:/var/lib/jenkins/jobs/backup/* /mnt/c/backupJenkins/$(/bin/date +%d-%m-%Y)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .