Rsync specific files and folders in linux - 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

Related

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

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/

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.

Zip folder exclude some folders

I'm trying to backup my www-folder but hidden folders like .config inside www are added to the backup. I want to exclude the folder "backups" and all folders (and files) starting with a dot.
The problem is that it copies all the hidden folders like .config to the zip-file.
Current code:
zip -r /var/www/backups/site/$(date +\%Y-\%m-\%d-\%H-\%M).zip /var/www -x "*backups*" "*.*" "*/.*"
This should work for you.
zip -r --exclude=*backups* --exclude=*/.* /var/www/backups/site/$(date +\%Y-\%m-\%d-\%H-\%M).zip /var/www
Use a linux find command with an exclude flag, then pipe it into zip.
The following command will exclude all paths under the current directory containing the keywords "backups" or files with "/." in the path and then pipe the files into zip.
find . | grep -v "\(backups\|/\.\)" | xargs zip archive.zip

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>

Why does `rsync <...> /path/to/someDir /path/to/otherDir` leave me with `/path/to/otherDir/someDir`, not syncing files from `someDir` into `otherDir`?

Why is my rsync doing that? It's basically just making a copy of the someDir folder inside otherDir. If I run the command again after making changes in /path/to/someDir, rsync will sync all files from /path/to/someDir to /path/to/otherDir/someDir. How do I get all the files inside /path/to/someDir synced to /path/to/otherDir.
This is what the command looks like that I'm excuting:
rsync --stats --compress --recursive --times --perms --links --delete --exclude ".git" --exclude "wp-content/upload" --exclude "wp-content/uploads" --exclude "wp-content/gallery" /path/to/someDir /path/to/otherDir
rsync is one of the few commands that make a distinction between /your/path and /your/path/
When you don't use the trailing backslash you are referring to the directory, while when you use it you are referring to the contents of the directory.
Try
rsync --stats --compress --recursive --times --perms --links --delete --exclude ".git" --exclude "wp-content/upload" --exclude "wp-content/uploads" --exclude "wp-content/gallery" /path/to/someDir/ /path/to/otherDir
That extra trailing slash in /path/to/someDir/ will make the contents of it available in /path/to/otherDir.
BTW: Don't be tempted to use /path/to/someDir/* as was suggested, that will give you problems when you have many files and it won't copy files with names beginning with ..
The /path/to/someDir refers to the folder, someDir, not the files inside.
If you want instead to copy the files out of /path/to/someDir, try this:
rsync... /path/to/someDir/ /path/to/otherDir

Resources