How to take selective backup?

Joined
Apr 16, 2023
Messages
149
Reaction score
16
Credits
1,460
Say there is a folder called domains in glassfish. It has domain1, domain2...domainN.
I want to take everything from domain1 except the logs directory and save them to /home/user/backup directory.
I can do this manually,
Code:
cp -R /home/user/glassfish/domains/domain1 /home/user/backup
rm -rf /home/user/backup/domain1/logs

I've seen someone doing

Code:
cp -R  `ls | grep -v logs`  source destination

But how can I do this more easily?
 


This worked in a similar test for me:

First create a text file in your home folder called "exclude.txt" with just the word "logs" in the file (no quotes). You can use another name for the file, but it must match the command below.

Then use rsync instead of cp, like this:

Code:
rsync -avhs --delete --exclude-from="exclude.txt" "/home/user/glassfish/domains/domain1" "/home/user/backup/"

The exclude.txt file prevents the logs folder from being copied.

NOTE: I've shown the --delete option on rsync, and you may or may not want that setting. What this does is keep your backup in complete sync with your source folder.... meaning if files or folders are deleted in the source, they will also be deleted in your backup the next time your run the command. Otherwise, if your source folder deletes any files or folders, they will remain in your backup causing it to grow larger than the source.

I hope that helps. Good luck!
 

Staff online

Members online


Top