Bash

警告並提供將受 rsync 命令影響的文件數

  • January 2, 2021

rsync--dry-run輸出是:

% rsync --dry-run -avi --delete-after /home/blueray/Documents/rsync-test/src /home/blueray/Documents/rsync-test/dest
building file list ... done
.d..t...... src/
>f.st...... src/empty-asciidoc-document 3.adoc
>f+++++++++ src/empty-asciidoc-document 4.adoc
>f+++++++++ src/empty-asciidoc-document-renamed.adoc
*deleting   src/empty-asciidoc-document.adoc
*deleting   src/empty-asciidoc-document 2.adoc

sent 254 bytes  received 27 bytes  562.00 bytes/sec
total size is 16  speedup is 0.06 (DRY RUN)

我正在尋找如下輸出:

building file list ... done
.d..t...... src/
>f.st...... src/empty-asciidoc-document 3.adoc
>f+++++++++ src/empty-asciidoc-document 4.adoc
>f+++++++++ src/empty-asciidoc-document-renamed.adoc
*deleting   src/empty-asciidoc-document.adoc
*deleting   src/empty-asciidoc-document 2.adoc

sent 254 bytes  received 27 bytes  562.00 bytes/sec
total size is 16  speedup is 0.06 (DRY RUN)

Warning: n files will be affected, do you want to continue?

到目前為止我想出的腳本是:

#!/bin/bash

affected_files=$(rsync --dry-run -avi --delete-after /home/blueray/Documents/rsync-test/src /home/blueray/Documents/rsync-test/dest)
echo $affected_files

number_of_affected_files=$(echo $affected_files | grep src | wc -l)

echo "Warning: ${number_of_affected_files} files will be affected, do you want to continue?"

while true; do
   case $yn in
       [Yy]* ) rsync -avi --delete-after /home/blueray/Documents/rsync-test/src /home/blueray/Documents/rsync-test/dest; break;;
       [Nn]* ) exit;;
       * ) read -p "Please answer yes or no: " yn;;
   esac
done

但是,它有一些問題。$affected_files不保持換行符。所以,腳本不起作用。此外,我不確定是否$affected_files | grep src | wc -l給出受影響文件的實際數量。

我能做些什麼?

在這裡,我目前使用的腳本是:

#!/bin/bash

rsync -ab --dry-run --stats --human-readable --inplace --debug=NONE --log-file=rsync.log --backup-dir=rsync_bak.$(date +"%d-%m-%y_%I-%M-%S%P") --log-file-format='%t %f %o %M' --delete-after /home/blueray/Documents/rsync-testsrc /home/blueray/Documents/rsync-testdest | sed -e '1d;5,12d;14,17d'

echo -e "\nDo you want to continue?"

while true; do
   case $yn in
       [Yy]* ) rsync -ab --human-readable --inplace --info=PROGRESS2,BACKUP,DEL --debug=NONE --log-file=/home/blueray/Documents/rsync-testdest/rsync.log --backup-dir=rsync_bak.$(date +"%d-%m-%y_%I-%M-%S%P") --log-file-format='%t %f %o %M' --delete-after /home/blueray/Documents/rsync-testsrc /home/blueray/Documents/rsync-testdest; break;;
       [Nn]* ) exit;;
       * ) read -p "Please answer yes or no: " yn;;
   esac
done

引用自:https://unix.stackexchange.com/questions/627065