Rsync syntax to copy specific subfolders - linux

Alright so my web server has the following file structure
/
/home
/home/username
/home/username/public_html
/home/username/mail
/home/username/etc
...
/home/username2
/home/username2/public_html
...
So im trying to figure out a way of doing a cronjob that does a rsync wich will only synchronise the public_html folder of the 600 accounts i have. I thought of maybee doing an exclusion list with every other subfolder name there is under the accounts directories but i wasn't sure that was the optimal solution
Is there a way of telling rsync to only sync the content of the public_html folders without having to manually type in the 600 accounts?
Thanks
PS.: My current solution was something amongst the lines of :
rsync –vaRu –exclude ‘mail*’ -exclude 'etc*' home root#xxx.xxx.xxx.xxx:home
With this solution if any filenames match the directories name they won't be copied over.

You can do it using three filters:
rsync -av --filter="+ /home" \
--filter="+ /home/*" \
--filter="+ /home/*/public_html" \
--filter="+ /home/*/public_html/**" \
--filter="- *" / root#xxx.xxx.xxx.xxx:mirror
It is important to add a "+" filter for all the directories in the tree before the public_html and the "**" to include everything behind public_html.
The only drawback is that all the home directories will be created on destination but just as empty dirs.

Related

rsync, ignore root synchronised directory hidden files

I'm trying to make snapshot backups of my user using rsync, the base user folder has a ton of hidden files and configurations that I'm not interested in backing up, however I am interested in the hidden folders inside of it's subdirectories.
Here is the layout
Source Directory
/Base
.ignore
.ignore2
.ignore3
/dir1
.keep
normalfiles
/dir2
.keep
normalfiles
Desired Backup Directory
/Backup
/dir1
.keep
normalfiles
/dir2
.keep
normalfiles
How can I ignore the first hidden files AND directories while preserving them in the subdirectories.
Solution
rsync -vrn --exclude="/.*" base/ base2/
By specifying the / before the file match /.* I managed to achieve what I was after. I have found this to be pretty useful when making snapshot backups of some mac's in my house. They tend to store tons of config information in the root folder for each user that isn't needed for my backups.
The hidden files for my own project configs and rc's is saved though since they aren't stored in the root directory.
I can not found "hidden folders inside of it's subdirectories" in your Code sample. Do you mean hidden files?
Here is a try:
rsync -nrv --include="/*/" --include="*/.*" --exclude="*" Base/ Base2/
-n simulate
-r recursive
-v verbose

How to copy directory from remote server with scp, excluding a specific subdirectory

I have a directory that I want to copy from a remote server to my desktop. In this directory there is a 40Gb subdirectory that I want to exclude from copying. Until now I used the scp command but there is no -exclude option or anything similar in the man page. By looking online I found a bunch of different ways to copy directory from remote host or exclude a subdirectory when copying locally but no way of how to combine those two. Any help is appreciated. Thanks.
To copy with excluding some file or folder, you should try rsync command.
There is --exclude-dir option you can use to exclude subdirectory.
Another option if you can't use rsync (on a readonly system for exemple) is to tar over ssh.
tar \
--exclude='temp' \
--exclude='work' \
--exclude='logs' \
-cpf \. # c: create p: preserve owner f: dest file... is output stream
- \
apache-tomcat-9.0.17 \. # file or folder to compress/send through ssh
| ssh foo#bar.com '(cd ~/my/save/directory; tar xfp - )'

How to archive files and sub folders in a location to another place in linux

I am trying to create a shell script to copy folders and files within those folders from one Linux machine to another linux machine. After copying I would like to delete only the files that are copied. I want to retain the folder structure as is.
Eg.
Machine X has a main folder named F with subfolders A,B,C folders in which each of them has 10 files.
I would like to make a copy in such a way that machine Y will have a folder named F with subfolders A,B,C containing the same files. Once the copy of all folders and files are complete, it should delete all the files in source folder but retain the folders.
The code below is untested. Use with care and backup first.
Something like this should get you started:
#!/bin/bash
srcdir=...
set -ex
rsync \
--verbose \
--recursive \
"${srcdir}/" \
user#host:/dstdir/
find "${srcdir}" -type f -delete
Set the srcdir variable and the remote argument to rsync to taste.
The rsync options are just from memory, so they may need tweaking. Read the documentation, especially options regarding deletion, backup, permissions and links.
(I'd rather not answer questions requests that show no signs of effort, but my fingers were itching, so there you go.)
scp the files, check the exit code of the scp and then delete the files locally.
Something like scp files user#remotehost:/path/ && rm files
If scp has failed, the second part of the command won't execute

Rsync / Linux : prevent new directory being created inside destination directory

Pardon my novice-ness, please.
A question regarding the usage of Rsync.
If I specify, as an example, this command:
rsync -av Downloads Downloads2
the result is that the directory "Downloads2" is created with "Downloads" as a subdirectory, and, within /Downloads2/Downloads are all of the contents of the original /Downloads directory.
I am wondering if there is an option which will cause rsync to copy the contents of /Downloads to /Downloads2 without creating the /Downloads2/Downloads structure?
As you can see from "man rsync", adding a trailing slash on the source directory will prevent that directory being created:
rsync -avz foo:src/bar/ /data/tmp
A trailing slash on the source changes this behavior to avoid creating
an additional directory level at the destination. You can think of a
trailing / on a source as meaning "copy the contents of this directory"
as opposed to "copy the directory by name", but in both cases the
attributes of the containing directory are transferred to the contain‐
ing directory on the destination. In other words, each of the follow‐
ing commands copies the files in the same way, including their setting
of the attributes of /dest/foo:
rsync -av /src/foo /dest
rsync -av /src/foo/ /dest/foo

Copy files excluding some folder in linux

I want to create script that copy my project and make it zip archive. I want to exclude all folder named .svn in all sub directories. Any suggestion?
I'd use rsync's FILTER RULES for this:
Create an .rsync-filter file (in the origin directory) containing, e.g.
-.svn/
Now run rsync like an exalted copy:
rsync -aFF origin/ destination/
You can do this using rsync. Although this is designed to synchronise directories across servers, it can also be used to copy directories on a single machine.
rsync has a --exclude option to exclude files and directories by pattern. See http://www.samba.org/ftp/rsync/rsync.html for help and examples.
Just call the zip utility on your project’s folder and use the -r option for recursive plus the -x option to exclude files / folders by pattern.
zip -r target-filename.zip source-folder -x \*exclude-pattern\*
exclude-pattern in your case would be .svn
See also man zip

Resources