What I'm trying to do backup?

  1. users home dir which include Desktop,Documents,Pictures, thunderbird which i need to backup of every user
  2. all users are in /home partition with there respective user name
  3. there are certain user AND files in /home which need to be exclude

What I've tried so far?

$ tar cvf home01 -T include /home  

NOTE: T which says only take that which are mention in file but does not work

$ find . \( -name \*Desktop -o -name \*Documents\* -o -name \*Pictures\*  -o -name \.thunderbird\*   \)  |
  xargs  tar zcvf /opt/rnd/home-$(date +%d%m%y).tar.zip 

NOTE: which takes backup of mention dir but it puts every users dir into one folder.

For example

$ ls -l /home


From which I need to take only user1,user2,user3 home dir backup and exclude user4,user5


You're nearly there! This should do:

tar zcvf /opt/rnd/home-$(date +%d%m%y).tar.zip */{Desktop,Documents,Pictures,.thunderbird} --exclude=user4 --exclude=user5
| improve this answer | |

Here's a way to do it retaining your use of the find command:

$ find . \( -type d -path */Desktop -o -path */.thunderbird -o -path */Pictures \\) \! -path '*user[45]*' -prune | xargs  tar zcvf /opt/rnd/home-$(date +%d%m%y).tar.gz

Here's an easier version to look at:

$ find . \( -type d                                                   \
     -path */Desktop -o -path */.thunderbird -o -path */Pictures \)   \
     \! -path '*user[45]*' -prune                                     \
  | xargs  tar zcvf /opt/rnd/home-$(date +%d%m%y).tar.gz

This will find all the directories with the names in the first parens block \( ... \). These are the directories:

  • */Desktop
  • */thunderbird
  • */Pictures

The 2nd part excludes any paths that match the pattern *user[45]*. These pare pruned out of the list. Finally the resulting list of directories to passed to tar.

Additional things to consider

The above is NOT bullet-proof. It will exclude paths that include the string user4 or user5 in them. Also some care should be given to making sure that whatever you construct as a command can deal with spaces in filenames.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.