Rsync include or exclude directories using text file - bash

I'm using rsync to backup some data from a remote host.
this is how I'm using the rsync cmd:
rsync --dry-run -avhi -e ssh --include-from=/home/rsync_list/test.txt root#10.10.4.61:/ /mnt/BACKUP/my_BACKUP/
this is the file /home/rsync_list/test.txt
+ /usr/acs/conf/**
+ /usr/acs/bin/**
+ /raid0/opmdps/TEMP_folder/**
- *
I want to copy only the listed folders excluding the remaining files.
I always get
receiving file list ... done
sent 103 bytes received 48 bytes 302.00 bytes/sec
total size is 0 speedup is 0.00 (DRY RUN)
Could you tell me what I'm doing wrong? How should I write the rsync command if I would like to sync, for example, only /raid0/opmdps/TEMP_folder/ without its subfolders?

I wonder if you really only tried with the command you posted?
Not only you are using "--dry-run" option, even the output indicates this:
total size is 0 speedup is 0.00 (DRY RUN)
Please consult the manpage:
-n, --dry-run perform a trial run with no changes made
https://linux.die.net/man/1/rsync
May I suggest you give it a run without --dry-run?

Related

Rsync calculate file count before transfer?

When transferring lots of small files in many folders with rsync, it tends to stay around ir-chk=1000/xxxxx, where xxxxx keeps counting up as it discovers new files, and the amount to check stays around 1000 until its on its last few folders.
How can I have it check for the entire file count before copying?
The command I was using to copy was:
rsync -av --progress source dest
rsync -av --progress --dry-run --stats source dest
Option --dry-run doesn't transfer any files but will show how many bytes will be transferred.
Option --stats shows a summary.
a sample output:
...
tests/
tests/__init__.py
tests/test_config.py
Number of files: 5,033 (reg: 2,798, dir: 2,086, link: 149)
Number of created files: 5,032 (reg: 2,798, dir: 2,085, link: 149)
Number of deleted files: 0
Number of regular files transferred: 2,798
Total file size: 26,035,530 bytes
Total transferred file size: 26,032,322 bytes
Literal data: 0 bytes
Matched data: 0 bytes
File list size: 0
File list generation time: 0.004 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 158,821
Total bytes received: 17,284
sent 158,821 bytes received 17,284 bytes 117,403.33 bytes/sec
total size is 26,035,530 speedup is 147.84 (DRY RUN)
Get the number of files will be transferred
rsync -av --progress --dry-run --stats source dest |
fgrep 'Number of files' |
cut -d' ' -f4 |
tr -d ,
Since version 3.0.0, you can use this option to explicitly turn off incremental recursion:
--no-i-r
Details from the rsync man page:
Beginning with rsync 3.0.0, the recursive algorithm used is now an
incremental scan that uses much less memory than before and begins the transfer after the scanning of the first few directories have been completed. This incremental scan only affects our recursion algorithm, and does not change a non-recursive transfer. It is also only possible when both ends of the transfer are at least version 3.0.0.
Some options require rsync to know the full file list, so these options
disable the incremental recursion mode. These include: --delete-before, --delete-after, --prune-empty-dirs, and --delay-updates. Because of this, the default delete mode when you specify --delete is now --delete-during when both ends of the connection are at least 3.0.0 (use --del or --delete-during to request this improved deletion mode explicitly). See also the --delete-delay option that is a better choice than using --delete-after.
Incremental recursion can be disabled using the --no-inc-recursive
option or its shorter --no-i-r alias.

How can I compare the file sizes match between duplicate directories?

I need to compare two directories to validate a backup.
Say my directory looks like the following:
Filename Filesize Filename Filesize
user#main_server:~/mydir/ user#backup_server:~/mydir/
file1000.txt 4182410737 file1000.txt 4182410737
file1001.txt 8241410737 - <-- missing on backup_server!
... ...
file9999.txt 2410418737 file9999.txt 1111111111 <-- size != main_server
Is there a quick one liner that would get me close to output like:
Invalid Backup Files:
file1001.txt
file9999.txt
(with the goal to instruct the backup script to refetch these files)
I've tried to get variations of the following to no avail.
[main_server] $ rsync -n ~/mydir/ user#backup_server:~/mydir
I cannot do rsync to backup the directories itself because it takes way too long (8-24hrs). Instead I run multiple threads of scp to fetch files in batches. This completes regularly <1hr. However, occasionally I find a few files that were somehow missed (perhaps dropped connection).
Speed is a priority, so file sizes should be sufficient. But I'm open to including a checksum, provided it doesn't slow the process down like I find with rsync.
Here's my test process:
# Generate Large Files (1GB)
for i in {1..100}; do head -c 1073741824 </dev/urandom >foo-$i ; done
# SCP them from src to dest
for i in {1..100}; do ( scp ~/mydir/foo-$i user#backup_server:~/mydir/ & ) ; sleep 0.1 ; done
# Confirm destination has everything from source
# This is the point of the question. I've tried:
rsync -Sa ~/mydir/ user#backup_server:~/mydir
# Way too slow
What do you recommend?
By default, rsync uses the quick check method which only transfers files that differ in size or last-modified time. As you report that the sizes are unchanged, that would seem to indicate that the timestamps differ. Two options to handlel this are:
Use -p to preserve timestamps when transferring files.
Use --size-only to ignore timestamps and transfer only files that differ in size.

cwRsync, network drive, file modification time issue

I'm using cwRsync 5.4.1 x86 Free under windows and trying to sync folder to network drive.
I execute following command:
rsync.exe -rLtv --delete --ignore-errors "/cygdrive/d/1/" "/cygdrive/z/ZipNB/"
Where D is local drive and Z is network drive (external HDD connected to router, RT-N16)
Executing it several times gives same result:
>rsync.exe -rLtv --delete --ignore-errors "/cygdrive/d/1/" "/cygdrive/z/ZipNB/"
sending incremental file list
./
1.pdf
sent 11,893,922 bytes received 38 bytes 1,829,840.00 bytes/sec
total size is 11,890,918 speedup is 1.00
I have one file in the folder and it sends its content each execution. File is same each time and was not changed in the middle.
If I add additional parameter --size-only it works as expected:
>rsync.exe -rLtv --delete --ignore-errors --size-only "/cygdrive/d/1/" "/cygdrive/z/ZipNB/"
sending incremental file list
./
sent 72 bytes received 22 bytes 188.00 bytes/sec
total size is 11,890,918 speedup is 126,499.13
DIR for both directories:
D:\1>dir
Volume in drive D is XXX
Volume Serial Number is XXXX-XXX
Directory of D:\1
08.12.2016 10:04 <DIR> .
08.12.2016 10:04 <DIR> ..
24.11.2016 18:31 11 890 918 1.pdf
1 File(s) 11 890 918 bytes
Z:\ZipNB>dir
Volume in drive Z is BackUp (at Portable)
Volume Serial Number is XXXX-XXX
Directory of Z:\ZipNB
08.12.2016 10:04 <DIR> .
08.10.2016 20:40 <DIR> ..
24.11.2016 18:31 11 890 918 1.pdf
1 File(s) 11 890 918 bytes
I'm not sure but as I'm aware of rsync by default makes verification of file by modification time and size. Both files seems identical. But it seems like cwRsync for some reason gets/sets wrong modification date on file at Z drive. cwRsync works correctly if both directories are on local drive. It happens only with network drive.
In windows properties there is difference in modification time in 1 second, which can cause the problem.
I took only 1 file as an example only to simplify output, the situation is same heaving any amount of different files. It always sends full content of each file.
What can be wrong here, and how I can fix it?
I'm guessing the HDD on the network share uses FAT, because from File Times:
For example, the resolution of create time on FAT is 10 milliseconds,
while write time has a resolution of 2 seconds and access time has a
resolution of 1 day, so it is really the access date.
That would explain the time difference.
And for this kind of reason rsync added the --modify-window option:
-#, --modify-window
When comparing two timestamps, rsync treats the timestamps as being equal if they differ by no more than the modify-window value.
The default is 0, which matches just integer seconds. If you specify a
negative value (and the receiver is at least version 3.1.3) then
nanoseconds will also be taken into account. Specifying 1 is useful
for copies to/from MS Windows FAT filesystems, because FAT represents
times with a 2-second resolution (allowing times to differ from the
original by up to 1 second).
So try to add -#1 to your command.

rsync script expands variable incorrectly

I have a script that takes in a unique location number from a file. These are formatted like this 7325-05, 5269-09 and 7479-14, for example. The first four numbers are what the folder is called and the second number is the first two characters of the filename unique within each folder.
So I wrote this script to use locate and find to get the full path of the folder and then use a wildcard to download the specific file using rsync. Here's the script that I have right now:
#!/bin/bash
#IFS='
#'
oIFS=$IFS
IFS=$'\n'
while read line;
do
name=$line;
folder=${line:0:4}
track=${line: -2}
folderlocation="$(locate -r '/'$folder'$')"
filelocation="$(find "$folderlocation" -type f -name "$track*")"
rsync -vazhn --progress "$filelocation" /cygdrive/c/
# mkdir /cygdrive/c/test/"$folder"
# cp -rvi "$filelocation" /cygdrive/c/test/"$folder"
echo "";
done < $1
The code using cp that is commented out works just fine. I would just really prefer to use rsync, mainly due to better feedback and more accurate progress reporting, as far as I can tell.
Using the code as pasted above (with rsync) throws this error:
./filelocator classic-locations.txt
sending incremental file list
rsync: change_dir "/home/emil//\\sandrew-nas/SMMUSIC/MMIMUSIC/7001-8000/7201-7300/7252/Unknown Album (29-12-2012 09-52-02)" failed: No such file or directory (2)
sent 20 bytes received 12 bytes 64.00 bytes/sec
total size is 0 speedup is 0.00 (DRY RUN)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1165) [sender=3.1.1]
sending incremental file list
rsync: change_dir "/home/emil//\\sandrew-nas/SMMUSIC/MMIMUSIC/7001-8000/7201-7300/7252/Unknown Album (29-12-2012 09-52-02)" failed: No such file or directory (2)
sent 20 bytes received 12 bytes 64.00 bytes/sec
total size is 0 speedup is 0.00 (DRY RUN)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1165) [sender=3.1.1]
As you can see, my home folder (where I issue the command) is suddenly included in the script, leading me to believe that a variable or wildcard is being expanded in the local shell, but no amount of escape characters seem to accomplish what I want with rsync.

Is rsync really any faster on files that have changed?

Why can't I trust rsync to be minimally as fast as cp? (I'm ignoring negligible differences for overhead.)
It seems to me like rsync is fairly slow on files with no content difference, but a changed timestamp.
If I make a file: cp -a testfile-100M destfile
And then I rsync them, I get what you would expect:
$ rsync -av testfile-100M destfile
sending incremental file list
sent 56 bytes received 12 bytes 8.00 bytes/sec
total size is 104857600 speedup is 1542023.53
But that's just because rsync is checking the size and the timestamp and skipping the file. What if I just change the timestamp?
$ touch testfile-100M
$ rsync -av testfile-100M destfile sending incremental file list
testfile-100M
sent 104870495 bytes received 31 bytes 113804.15 bytes/sec
total size is 104857600 speedup is 1.00
Also note that even though the speedup is 1, the inital copy took about 1/4 the time to complete than the final rsync, even though the contents are exactly the same. So what's going on here? Is it just all the overhead of doing the comparisons?
If that's the case, then when does rsync ever provide a performance advantage? Only when files are exactly the same on both sides?
For local files, if the size or mtime have changed, rsync by default just copies the whole thing without using its delta algorithm. You can turn this off with the --no-whole-file option, but for local copies this will typically be slower.
For the specific case of touching a file without changing it:
If you give the --size-only option, it will assume that files that have the same size are unchanged.
If you give the --checksum option, it will first hash the file to see if anything has changed, before copying it.
When the source and destination are both locally mounted filesystems rsync just copies the file(s) if the timestamps or sizes don't match. Rsync wins where you have large files with small differences and they are on machines separated by a low bandwidth link.
EDIT: Since someone felt the need to downvote this ancient answer... As to why rsync on local files might be slower than cp, there does not seem to be any good reason.
It appears the answer is that rsync does some extra steps in order to keep files in a consistent state, and not in a "partial-transferred" state while operating. Using the --inplace option removes this overhead.
Interestingly, for me rsync is about 4× faster than cp for copying to an external USB drive.

Resources