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
439 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Microsoft will be releasing the Windows 10 Creators Update in just a matter of weeks. Are you prepared? Follow these steps to ensure everything goes smoothly and you don't lose valuable data on your PC.
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 steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…

830 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