Solved

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
9
440 Views
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.
0
Comment
Question by:shinmaikeru
[X]
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
9 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 250 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
wmp
0
 
LVL 2

Assisted Solution

by:jdevera
jdevera earned 250 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

0
 

Author Comment

by:shinmaikeru
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.
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 2

Expert Comment

by:jdevera
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

0
 

Author Comment

by:shinmaikeru
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.
0
 

Author Closing Comment

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

Author Comment

by:shinmaikeru
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.
0
 
LVL 2

Expert Comment

by:jdevera
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:

http://unixhelp.ed.ac.uk/CGI/man-cgi?stat

I hope this helps.
0
 

Author Comment

by:shinmaikeru
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.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

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.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This tutorial will walk an individual through the process of installing of Data Protection Manager on a server running Windows Server 2012 R2, including the prerequisites. Microsoft .Net 3.5 is required. To install this feature, go to Server Manager…

734 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