Rsync how to include directories but not files? - linux

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.

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 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/".

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

rsync only directories that exist?

On my local machine I have several plugins, and on the server I have a few.
Local Machine:
~/wp-content/plugins/plugin1
~/wp-content/plugins/plugin1/includes
~/wp-content/plugins/plugin2
~/wp-content/plugins/plugin3
~/wp-content/plugins/plugin4
~/wp-content/plugins/plugin5
Remote Machine:
~/wp-content/plugins/plugin1
~/wp-content/plugins/plugin1/includes
~/wp-content/plugins/plugin3
~/wp-content/plugins/plugin5
What rsync command can I use to update all the files in the remote directories, but only if they exist? For this I would like to have plugin1, plugin1/includes, plugin3, and plugin5 synced - with all files and directories inside - but not plugin2 or plugin4. Basically, update all of the plugins if they exist with a single rsync command.
This is not completely possible with only one rsync command. If you only want to update the remote files, but do not want to add new files, you can use this command:
rsync -rP --existing source/ user#remote:target/
This will not create new files or directories at all, but update differing ones.
Edit: Maybe you could do something like this (assuming GNU find, if BSD/OS X: replace maxdepth with depth):
#!/bin/bash
REMOTE_TARGET_DIR=/some/path
REMOTE_DIRS=$(ssh user#remote "find $REMOTE_TARGET_DIR -maxdepth 1 -type d -printf '%P\n' | sed 1d"
for DIR in $REMOTE_DIRS
do
rsync -rP "$DIR" "user#remote:$REMOTE_TARGET_DIR/"
done
Warning: I have not tested this, you might want to add a "-n" to rsync (dry run) to see what happens.

Resources