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
433 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
 
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
Too many email signature updates to deal with?

Do you feel like you are taking up all of your time constantly visiting users’ desks to make changes to email signatures? Wish you could manage all signatures from one central location, easily design them and deploy them quickly to users? Well, there is an easy way!

 

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

Integrate social media with email signatures

Is your company active on social media? Do you also use email signatures? Including social media icons in your email signature is a great way to get fans for free. Let all your email users know you’re on social media quickly and easily, in a single click.

Join & Write a Comment

If, like me, you have a lot of Dell servers in the estate you manage this article should save you a little time. When attempting to login to iDrac on any server I would be presented with two errors. The first reads "Do you want to run this applicati…
Workplace bullying has increased with the use of email and social media. Retain evidence of this with email archiving to protect your employees.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now