Need script that will copy the ownerships of files in a backup directory (rsync) and apply them to the original directory, the ownership of which has been mistakenly changed.

Posted on 2009-03-31
Medium Priority
Last Modified: 2013-12-16
I need a script that will go through the backup directory and copy the ownership of each file and make a script that will restore ownership of those files. The script will have to begin in /home and then drill down, attaching the relative directories and putting them on the front of the filename. Something like the recursive 'ls -n' that goes through the backup directory would be great. The results would look like:
chown user.group /home/users/a/user/MyTrip.doc
For each file as I go down.
Question by:shinmaikeru
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
LVL 68

Accepted Solution

woolmilkporc earned 1000 total points
ID: 24027056
Try this -

echo "#!/bin/ksh" > chownscript.sh
find /home -type f -ls | awk '{print "chown", $5"."$6, $11}' >> chownscript.sh
echo "exit" >> chownscript.sh
chmod +x chownscript.sh

Assisted Solution

jdevera earned 1000 total points
ID: 24027099
I had to do something like this a while ago, I also needed to store the permissions.
The first command line will create a script that you can later run and will restore both permissions and ownership. If you only want ownership, use the second option.
# This will generate a permission and ownership restoration script.
# It can be run like this later:
# $ sh restorepo.sh
find /home | xargs -iXXX stat -c "chown %U.%G %n; chmod %a %n" XXX > restorepo.sh
# Alternatively, this will only generate ownership restoration.
# It can be run like this later:
# $ sh restoreo.sh
find /home | xargs -iXXX stat -c "chown %U.%G %n" XXX > restoreo.sh

Open in new window


Author Comment

ID: 24027284
I was trying the method with the find command on my own, but another complication is the fact that this file server contains lots of Japanese filenames and directory names. Xargs can't parse them and the xargs and find methods don't work on them.

I have given the users access again, however, by changing the user directories to the user and group with -R, so I am just working with the alphabetic top directories. Unfortunately, this wipes away any fine-tuned group memberships under the main directories.
What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.


Expert Comment

ID: 24027428
I don't know if this will solve the problem with file names, but you can try this alternative:
find /home | while read filename; do stat -c "chown %U.%G %n" $filename; done > restoreo.sh

Open in new window


Author Comment

ID: 24027594
It still chokes on multibyte. I have the users back on line, but I have no idea if the server will run after the next reboot.

I need to migrate to a new file server soon, so I will just have to hope that this will do until then.

Author Closing Comment

ID: 31564739
Thank you very much. EE is the best budget money I ever spent.

Author Comment

ID: 24033794
The server is still up, but overnight the Logwatch and rsync backups didn't work, so there must be some dot files which, invisible, were not changed back. How would I get 'stat' or 'find' to gather a list of the dot files that had the owner changed? I can do it with 'ls -lR | grep', but it would be nice to be able to filter out a list that includes the entire path. If 'ls' just had an option to do full path, I would be golden, because I am not that familiar with stat.

Expert Comment

ID: 24037098
There must be a different cause because find, by default, will list everything, including dot files.

I think you should avoid using ls for this kind of scripting tasks. Find is much more powerful and provides a nice list that can be piped to other scripts.

Doing "find . | grep yourfilter" will let you filter out a list of full paths.

About stat, the power in stat is that you can, as shown in my snippets above, specify the format that you want to use for its output, so it lets you write scripts or just summarize the information you want quickly.

So you could do something like

stat -c "%A %h %U %G %s %y %n" filename

And that would mimic (approximately) the output of ls -lR, only for that file. To get this recursively for a directory, you could call stat from within find:

find /home -exec stat -c "%A %h %U %G %s %y %n" {} \;

The -c switch for stat is always followed by a format string that will determine what information is shown, you can find a list of the available placeholders if you have a quick look at the stat manpage (just run: man stat), or here:


I hope this helps.

Author Comment

ID: 24037927
Thank you. That is exactly the information I need. I will look into this immediately tomorrow. My rsync's still aren't working, but I was not able to hit the servers today.

Thank you again. I think this will really help.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you looking to recover an email message or a contact you just deleted mistakenly? Or you are searching for a contact that you erased from your MS Outlook ‘Contacts’ folder and now realized that it was important.
This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question