Question

I have been hitting my head against the wall trying to get this to work.

I am writing an rsync backup script for use on freebsd. I do not have root privileges on the remote server, and I need to mv and cp the files well on the remote server so once I restore them I lose ownership.

I am trying to create an permissions file to backup all permissions and ownerships to re apply once a data restore is done.

this is the backup code

find /dir -ls >> output_file

this is the restore code

for f in `cat output_file|awk '{print $11}'` ; do chown `cat output_file|grep $f\$|awk '{print $5 ":" $6}'` $f ; done

this is an example of output_file

 23319     3857 -rwxrwxrwx    1 root                         root                          1867545 Jan  9 22:55 /mnt/Tank2/Users/root/stampede/Little_House.jpg
 23320     3089 -rwxrwxrwx    1 root                         root                          1530449 Jan  9 22:57 /mnt/Tank2/Users/root/stampede/Milky_Way.jpg
 23325     3857 -rwxrwxrwx    1 root                         root                          1938544 Jan  9 22:47 /mnt/Tank2/Users/root/stampede/Shy_Horses.jpg
 23324     3857 -rwxrwxrwx    1 root                         root                          1940621 Jan  9 22:50 /mnt/Tank2/Users/root/stampede/Reinforcements.jpg

everything works great unless there is a space in a file or folder name then it fails. if my folder is called "folder 1" the script only gets "folder" I think all I need to do is have "cat output_file|awk '{print $11}'" extend from $11 to the end of the line and before every space to add a \ so the output conforms to standard *nux file format but I am unable to figure out how to do it.

any help would be great!

Was it helpful?

Solution

You are working way too hard, and making the computer work too hard as well. Never pipe cat to grep to awk. In this case, you don't need any of those tools; just do:

while read inode blocks perm links owner group bytes month day time path; do 
    chown $owner:$group "$path"
done < output_file

read will put all of the trailing fields into path, preserving white space.

OTHER TIPS

First, check if the output is fine or not.

awk '{print $5 ":" $6,substr($0,112)}' output_file

if fine, try below script

awk '{print $5 ":" $6,substr($0,112)}' output_file |while read perm file
do
  chown "$perm" "$file"
done
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top