-1

I looked up the forum but didn't find an article which matches my problem. Maybe there is some, and you can help me out with it.

My problem is I want to sync an folder with the command rsync -a -v. The point is I got 5 different Maschinen. On every maschine is a scratch folder I want to sync into the folder: ~/work_dir/scratch_maschines and inside the /scratch_maschines folder should be a folder for maschine_a, maschine_b and so on.

On the maschines it is always the same path: /scratch/my_name. So when I use now this command for the first two maschines:

rsync -a -v --exclude='*.chk' --exclude='*.rwf' --exclude='*.fchk' --delete sp02:/scratch/my_name ~/work_dir/scratch_maschine01; rsync -a -v --exclude='*.chk' --exclude='*.rwf' --exclude='*.fchk' --delete maschine02:/scratch/my_name ~/work_dir/scratch_maschine02

I got a folders for scratch_maschine01 and scratch_maschine02 in my working directory but inside these folders are not direct my data there is first a folder inside with my_name and this folder contains the data. So my question is how can I use the rsync command and get the files from the scratch directorys straight to the folders for each machine?

3
  • 1
    You need to add a slash to the end of the source path (maschine02:/scratch/my_name/) to tell rsync to just copy the contents of the my_name directory, not the directory itself. Commented Aug 23, 2022 at 9:34
  • 1
    Does this answer your question? Rsync copy directory contents but not directory itself Commented Aug 23, 2022 at 9:35
  • If the answer provided below offered the guidance you needed, it would be appreciated that you indicated that by clicking on the check-mark next to the Answer below. Thank you. Commented Nov 27, 2022 at 0:32

1 Answer 1

1

You might want to consider reformulating your commands similar to the following:

START=`pwd`
EXCLUDES="--exclude='*.chk' --exclude='*.rwf' --exclude='*.fchk'"
    
{   SOURCE="sp02:/scratch/my_name"
    REMOTE="${HOME}/work_dir/scratch_maschine01"
        
    cd "${SOURCE}"
    rsync --recursive -v --delete ${EXCLUDES} "./" "${REMOTE}/"
}>${START}/job.log 2>${START}/job.err

The key elements there are

  • the --recursive which will rsync will expand to include all content and subdirs of the SOURCE directory.
  • the / behind the ${SOURCE} notifies rsync to limit itself to content of the SOURCE directory, but not the directory itself.
  • the / behind the ${REMOTE} notifies rsync to limit itself to depositing content into that directory and expect it to already exist, to specifically fail if that does not already exist at REMOTE; this ensures that the remote site doesn't attempt a failsafe PWD and deposit files elsewhere than expected.

The above approach lends itself to a function form that could be placed into a loop with pre-attempt condition checks, along with having a complementary case for all variable assignments grouped under a destination heading (i.e. case statements).

Using such an approach with meaningful labels for variables lends itself to a type of implicit documentation, making the code more meaningful to someone not familiar with the code, as well as a refresher for yourself after a long period of not working or using the code.

I try to avoid the "~" because I prefer to always enclose definitions for variables in double quotes, to avoid issues that might arise from paths that may include unexpected characters or spaces. That way, you are sure to have your defined paths correctly interpreted by commands in scripts.

Lastly, I prefer to use the long form for the rsync options (and almost every other command) so that I don't have to refer to the manual every time to translate the single-character options when trying to understand what is coded, if the need arises for troubleshooting unexpected errors (I have always had poor memory).

My own backup command is as follows. The only reason why the

${PathMirror}${dirC}/

is not encapsulated in single quotes within the double quotes for COM is because I know those variables all evaluate to non-complex strings which cannot be misinterpreted.

enter image description here

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