Linux – Using rsync to maintain a copy of a directory with a name that changes

backuplinuxrsync

I am using rsync on a linux system to synchronize a directory between the local disk and an attached USB drive. The problem I am experiencing is that the 3rd party system that creates the backup daily on the server actually changes the name of a directory nested deep in the backup. This directory has the majority of the data required in the backup. When rsync looks at the directory, it sees that the name has changed and considers it to be a totally new directory. So my rsync copy on the USB drive has a new directory for every day that it runs.

I have written scripts that will actually change the directory name back, but it is a cumbersome way to do it!

I am looking for an "elegant" way to deal with this. Is it possible to create a link to the directory that remains constant? Can rsync be configured to detect that the directory is the same even though the name is changed? I am sure someone has had to deal with this before!

Best Answer

One approach would be to do it in two steps. 1st, rsync everything except the directory in question by using ignore patterns. 2nd, rsync just the directory using globbing in bash to get to the directory, like this:

rsync -av /usr/lib/mydata/bigdatadir*/ /mnt/usbvolume/bigdatadir/

Using a trailing slash on the source directory will effectively cause rsync to ignore the directory name, because it will be invoked on the contents of the directory rather than the directory itself. Of course, this globbing will be easiest if the directory is named with a constant prefix or suffix as in my example above. If it isn't, you could write a script to figure out the actual name of the directory, and do something more direct like this:

rsync -av /usr/lib/mydata/$BIGDATADIRNAME/ /mnt/usbvolume/bigdatadir/

In the end, your pseudo-code would be something like this:

  1. Find $BIGDATADIRNAME
  2. Rsync everything as you were before, but ignore $BIGDATADIRNAME
  3. Rsync the contents of $BIGDATADIRNAME
Related Topic