How to take selective backup?

Say there is a folder called domains in glassfish. It has domain1, domain2domainN.

I want to take everything from domain1 except the logs directory and save them to /home/user/backup directory.

I can do this manually,

cp -R /home/user/glassfish/domains/domain1 /home/user/backup
rm -rf /home/user/backup/domain1/logs

But how can I do this more easily?

Asked By: achhainsan


You could use rsync instead of cp. Mostly the same for local-to-local copies except that there’s an exclude option

rsync --archive --exclude /logs /home/user/glassfish/domains/domain1 /home/user/backup
Answered By: roaima

You can use zip or tar to make archived copy:

cd /home/user/glassfish/domains/domain1
tar --exclude="logs" cvf /home/user/backup/domain1.tar .

and so on. Or with zip:

cd /home/user/glassfish/domains/domain1
zip /home/user/backup/  -x logs/*** .

This way you have the advantage of naming archives based on date so you can keep history of records there.

Answered By: Romeo Ninov

You can use the cp command and exclude the folder with the logs, with !(excluded_folder) if you don’t want to use rsync or tar

cp -var /home/user/glassfish/domains/domain1/!(/home/user/backup/domain1/logs) /home/user/backup


cp -R /home/user/glassfish/domains/domain1/!(/home/user/backup/domain1/logs) /home/user/backup

If you want to exclude more folders you can use the | like

Make sure that your shell supports extended pattern matching (e.g., Bash with the extglob option enabled).

If you want to use it in a bash script:

shopt -s extglob
cp -var /home/user/glassfish/domains/domain1/!(/home/user/backup/domain1/logs) /home/user/backup

If you receive error something like !": event not found"

Check this post:

What is "-bash: !": event not found"

Answered By: Blockchain Office

If you can’t rely on shell syntax you can resort to find and cpio in pass-through mode:

find SOURCE ( -depth 1 -name logs -prune ) -o ( -type f -print0 ) |
cpio -p -v -0 -d DESTINATION

The following cpio options might be of further interest (if your implementation supports them):

  • -a, --reset-access-time – Reset access times on files after they are read.

  • -m, --preserve-modification-time – Set file modification time on created files to match those in the source.

  • --no-preserve-owner – Do not attempt to restore file ownership. This is the default when run by non-root users.

  • -R [user][:][group], --owner [user][:][group] – Set the owner and/or group on files in the output. […] this option can only be used by the super-user.

  • -u, --unconditional – Unconditionally overwrite existing files. Ordinarily, an older file will not overwrite a newer file on disk.

Answered By: David Foerster

There was no rsync on server and installation access wasn’t allowed. Likewise

cp -R /home/user/glassfish/domains/domain1/!(/home/user/backup/domain1/logs) /home/user/backup

This command didn’t work.

What worked was this:(From the directory you want to backup)

cp -R `ls -A | grep -v 'logs'` /path/to/backup/directory
Answered By: achhainsan
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.