rsync files/folders from a list in an input file and exclude from an exclude file - linux

I have two files with a list of folders. I want to sync the folders, with relative paths, to the destination excluding those from the exclude file.
$ cat include.txt
/home/user
/etc
/data/app
/boot
$ cat exclude.txt
/data/app/temp
/etc/aide
I have tried using --include-from and --files-from but can't seem to figure out it.
This seems to sync the folders, but not the files:
rsync -av --files-from=include.txt / /destination
Ultimately I want to sync to /destination and have the folder structure look like:
/destination/home/user
/destination/home/user/...
/destination/etc
/destination/etc/...
/destination/data/app
/destination/data/app/...
/destination/boot
/destination/boot/...

Just add -r and --exclude-from options and You should be good to go:
rsync -av -r --files-from=./include.txt --exclude-from=./exclude.txt / /destination/

Related

Rsync specific files and folders in linux

I want to rsync specific files in multiple subfolders
Example: Here my folder structure
home
home/shared/
home/shared/bav
home/shared/backups
home/shared/plugins
I want to rsync only "bav" files from this path. I am trying below command
rsync -av --list-only --include 'home/' --include 'home/shared/***' --include 'home/shared/bav' --exclude '*' -e $source:$target
But above command lists all files under "home" dir, I want only "bav" file

Rsync, include only certain files types excluding some directories

I want to rsync only certain file types (e.g. .py) and I want to exclude some directories (e.g. venv).
This is what I have tried:
rsync -avz --include='*/' --exclude='venv/' --include='*.py' --exclude='*' /tmp/src/ /tmp/dest/
But it doesn't work.
What am I missing?
With rsync you do not need to use --include="*.py" to include '*.py' files in the copy. The --include option will only include files that have been excluded by --exclude= before. rsync specifies ** as the wildcard specifier. For example, if you want to copy all .py files in the current directory (and subdirectories), but not copy anything from the venc directory, you can do something similar to:
rsync -uav --exclude="venc" **.py destination
(note -a implies -rlptgoD)
which would recursively copy all .py files in present working directory to destination excluding the venc directory.
To recursively copy only *.py files from all directories below the current path excluding any venc directories, you can build a temporary file with the results of find containing the *.py files and exclude files containing venc/ as part of the path, and then transfer all filenames in the temporary file using the --files-from and --no-R (no relative) options to rsync as:
$ find /path/to -type f -name "*.py" | grep -v 'venc/' > tmpfile \
rsync -uav --no-R --files-from=tmpfile / host:/dest/dir \
rm tmpfile
This will capture all *.py files in any subdirectories excluding all directories including the name venc/ and anything below them. The --no-R option is needed to prevent the absolute filenames in tmpfile from be taken as relative to the current working directory.

rsync with link-dest and empty directories

When using rsync with --link-dest, rsync does not copy empty directories from source to destination, a file has to change for that to happen.
Is there anyway to force it to create the empty directories?
You could run an additional rsync to create the empty directories:
rsync -av -f"+ */" -f"- *" <src> <dest>

rsync recursively and exclude content of specific directory, not the directory

I have a directory tree like this :
dir1/
file11
file12
file13
...
file1548216479524594
dir2/
file21
file22
dir3/
dir31/
file311
file312
dir32/
file321
I would like to rsync entire directory tree but without content of directory dir1.
If I use the basic rsync command :
rsync --progress -v -ar --delete --exclude="dir1/*" src/ dst/<br>
It works. But if I use -n to make a dry run before execute, it lasts very long because dir1 contains a lot file (I do not know why during the dry-run it lists all files, even those excluded).
If I use --exclude="dir1/", the dry-run is fast but I don't have my directory tree.
How can I do a rsync dry run fast (avoiding recursively dir1 files which are very numerous.) with my entire directory tree excluding all content of dir1 ?
In recent versions of rsync, you can use the -F option and put a file ".rsync-filter" in the directory src, containing:
- dir1/***
That seemed to work for me. I'm assuming that your hierarchy above is all under "src/".

Rsync how to include directories but not files?

I have a directory structure and files like this
data/
data/a.txt
data/folder/
data/folder/b.txt
data/folder/folder/
data/folder/folder/c.txt
...
a.txt, b.txt, and c.txt are large files that are computer-generated and renewed frequently. They should NOT be backuped -- but I want to backup the directory structure :
data/
data/folder/
data/folder/folder/
How can I do this with rsync and --exclude-from, without specifying every folder, but something like rsync -a data/* --exclude-from=exclude.rsync "" --onlyfoldersandnotfiles""?
Thanks for help !
rsync -a --include='*/' --exclude='*' source/ destination/
Basically, first include all directories, then exclude all files.
$ rsync -a -f"+ */" -f"- *" source/ destination/
"The two -f arguments mean, respectively, "copy all directories" and then "do not copy anything else"."
Further details: http://psung.blogspot.com/2008/05/copying-directory-trees-with-rsync.html
rsync -a -f"-! */" source/ destination/
Filter rule means "Exclude anything that's not a directory."
If you want to sync everything except one folder but you still want to keep the directory structure of that excluded folder, you should do the following:
$ rsync -a -f"+ /var/log/**/*/" -f"- /var/log/**/*" source/ destination/
See the exclude-list and the rsync command as an example.

Resources