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
Related
I know there has been a huge discussion about this but I have not found something this specific.
Im trying to copy all .key files in /home// directory
This does not work
/usr/bin/rsync -auPA --include="*/*.key" --exclude="*" /home/* /tmp/test
This works but it copies over unwanted empty directories like /home/uname/Documents
/usr/bin/rsync -auPA --include="*/" --include="*.key" --exclude="*" /home /tmp/test
Basically what i need for rsync to do is to copy only files with .key extension and only create necessarily folders that contain .key files
I think you are looking for the -m option. From the man page:
-m, --prune-empty-dirs
This option tells the receiving rsync to get rid of empty directories from the file-list, including nested directories that
have no non-directory children. This is useful for avoiding the creation of a bunch of useless directories when the sending
rsync is recursively scanning a hierarchy of files using include/exclude/filter rules.
Note that the use of transfer rules, such as the --min-size option, does not affect what goes into the file list, and thus
does not leave directories empty, even if none of the files in a directory match the transfer rule.
Because the file-list is actually being pruned, this option also affects what directories get deleted when a delete is active.
However, keep in mind that excluded files and directories can prevent existing items from being deleted due to an exclude both
hiding source files and protecting destination files. See the perishable filter-rule option for how to avoid this.
You can prevent the pruning of certain empty directories from the file-list by using a global "protect" filter. For instance,
this option would ensure that the directory "emptydir" was kept in the file-list:
--filter ’protect emptydir/’
Here’s an example that copies all .pdf files in a hierarchy, only creating the necessary destination directories to hold the
.pdf files, and ensures that any superfluous files and directories in the destination are removed (note the hide filter of
non-directories being used instead of an exclude):
rsync -avm --del --include=’*.pdf’ -f ’hide,! */’ src/ dest
If you didn’t want to remove superfluous destination files, the more time-honored options of "--include='*/' --exclude='*'"
would work fine in place of the hide-filter (if that is more natural to you).
If I copy files from one directory to another directory:
Will their inode numbers also change?
Changes in file of one directory, will it reflect in same file of another directory also?
When I use command like:
cp -r dir1/ dir2/
With a simple copy the file system handle the copied files as newly created ones, therefore assining new inodes to them.
Any change made in the origin wouldn't change the copys. This only happens when you create symbolic or hard links between files.
You can check the inodes of your files with "ls -i filename".
I'm trying to back up some key files and directories of a machine, as root, including some of the /home data, hand-picking some files to reduce the tarball size. Everything is OK for the most part, since most files are owned by root anyway, but say I just try this:
# tar -cf backup.tar /home/user/file
When I restore the contents, /home/user/file is as expected owned by user, but /home/user is owned by root. I tried, however,
# tar -cf backup.tar /home
and in this case all /home owners are preserved. (Note that I don't need the -p flag as I'm root. Still I tried it...)
Is this normal behavior? If so, is there a way to hand-pick regular-user files to back up while keeping the /home ownership information? My goal is to simply untar everything from /.
Thanks!
To properly set permissions for directories, the tarball needs to contain entries for those directories, so you need to add them to the tarball.
When you create a tarball that only contains /home/user/file, and not /home/user, there is then no information about the permissions of /home/user in the tarball, so tar doesn't know what to do. It automatically creates the directories but has no permissions, owner or group to give them, so they get the defaults.
You could add the directories as well:
# tar -cf backup.tar --no-recursion /home/user /home/user/file
But this may not make things any simpler for you. Note the --no-recursion - that tells tar to not add everthing under a directory, just the directory itself. If you wanted to actually also add direcory trees, you would have to use find(1) to pass it every file under that directory manually. That would get ugly quickly.
Keep in mind that we are talking about backing up and restoring particular files. If you were to ever restore, you would presumably also create the unix accounts to restore to if they weren't there already. So there would be no need to set permissions on home directories, at least. The same cannot be said of subdirectories of those though.
I am rsyncing a huge (18000 files) directory, and I need use the --delete option as there is a lot of junk in the destination folder. However, the destination is under SVN revision control, so I need it to keep the .svn/ subdirectories that each directory has. I tried using the --ignore=".svn/" flag but that seems to ignore only what is on the source, and still deletes these directories on the target. Is there any way around this? Both machines are recent CentOS servers.
Thanks.
You probably want the --exclude option; --delete-excluded would allow you to do the opposite, if desired (actually delete excluded files)
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