How to prevent directory deletion in rsync

I am trying to create a script to backup my home and other important directories to an external drive, excluding certain directories and file types ( --exclude ). Excluded files must also be deleted in the backup if I add the file or directory to the exclusion list ( --delete-excluded ).

In addition, you must also copy everything that has been deleted, in case something is not so bad ( --backup --backup-dir=rsync/backup ).

I start with this:

 #!/usr/bin/env bash PATH_PWD="`pwd`" PATH_HOME=~ SRC="/home/redsandro" PATH_TARGET="/media/redsandro/MyBook 4TB/backup/`hostname`" PATH_BIN="${SRC}/bin" EXCL_FROM="${PATH_BIN}/rs-backup-external.sh.exclude.txt" DATE="`date +%Y-%m-%d`" OPTS="-ahl --update --del --delete-excluded --force --ignore-errors --progress --stats --exclude-from=$EXCL_FROM --log-file=~/rsync/rsync.$DATE.log" OPTS="$OPTS --backup --backup-dir=rsync/backup/${DATE}" #OPTS="$OPTS --dry-run" echo Backing up $SRC to $PATH_TARGET... echo sudo rsync $OPTS "$SRC/" "${PATH_TARGET}${SRC}" 

Now, obviously, --del conflicts with --backup --backup-dir=rsync/backup . Can this directory be excluded from the directory? Or is there perhaps a simpler way to do this?

My goal is to have a script that I can run on all my machines, where the part is synchronized with a specific computer directory, and the other part (documents, images) is synchronized with the global catalog, because they should all be the same.

+4
source share
2 answers

Rsync is a great tool, but sometimes it's hard to use.

The problem is that you use --exclude-from , which sets global exclusion rules on both the sending and receiving sides.

Before rsync performs operations on the file system, it will first collect two lists of files, one for the sending side (client) and the other for the receiving side (server). If the file is excluded on both sides, rsync will simply ignore it. If it is excluded only on the sending side and exists on the receiving side, rsync will delete it.

In your case, since your rules exclude files on both sides, rsync does not delete them. Then you use --delete-excluded to force the deletion. But, as you said, this option conflicts with --backup --backup-dir=rsync/backup .

I suggest you do the following.

  • Convert $EXCL_FROM to a regular filter file, i.e. insert a minus sign in front of each line to exclude rules.
  • Remove --exclude-from=$EXCL_FROM and --delete-excluded from the rsync options.
  • Include -FF in rsync options.
  • Include the following command before calling rsync.

     cp "$EXCL_FROM" "$SRC/.rsync-filter" 

Thus, your rules will apply only to the sending side, and files specified as exclusion rules will be deleted from the receiving side, if found. This should not conflict with backup options.

Update

Essentially, the -FF makes rsync files for directory files for each directory named .rysnc-filter on both sides of the transfer. In your case, it will exist only on the sending side, so its rules will not apply to the receiving side.

Actually, -FF is equal to -F -F . The first F tells rsync to combine the rules from .rsync-filter into the root directory of the source and destination directories, as I said. The second F tells rsync about their exception. You do not want .rsync-filter be passed, otherwise your exclusion rules will also apply to the receiving side, and your original problem will return - excluded files will no longer be deleted.

Here my man (1) rsync says:

 -F same as --filter='dir-merge /.rsync-filter' repeated: --filter='- .rsync-filter' 
+2
source

One option is to find your --backup-dir outside of your backup folder using absolute paths (specified with ../ ).

i.e. --backup-dir="../rsync/backup/${DATE}"

This will create a new folder at the same level as the destination folder. For example, if you exported to /home/user/rsync/backup then the above command will save your deleted files to home/user/rsync/rsync/backup/${DATE}

0
source

Source: https://habr.com/ru/post/1483182/


All Articles