permfix.zsh: Fix broken permissions
Yesterday I messed up my NetBSD system while moving /usr and /var to a new, encrypted partition. I didn't reckon that NetBSDs tar needs "-p" to preserve the meta information attached to a directory or file, so I lost ownerships, groups and especially permissions. And as I discovered the mess I already had continue with setting up the system, installing ports, pkgsrc, /usr/src and so on...
Luckily I didn't simply remove the original directories, but kept them as "/usr.old" and "/var.old". So I wrote a small script using zsh and the "zsh/stat" module to fix it. So in case something like this might happen to you, too, here's a simple solution:
There might be a shorter solution, but I wanted to keep this simple and readable. ;-)
Luckily I didn't simply remove the original directories, but kept them as "/usr.old" and "/var.old". So I wrote a small script using zsh and the "zsh/stat" module to fix it. So in case something like this might happen to you, too, here's a simple solution:
#!/usr/pkg/bin/zsh
zmodload zsh/stat
typeset srcdir dstdir
typeset -A srcobj dstobj
typeset -i srcmode dstmode
[[ $# != 2 ]] && {
echo "Error: ${0:t} requires exactly two arguments: sourcedir and destdir"
exit 1
}
[[ ! -d $1 ]] && {
echo "Error: $1 is not a directory or does not exist."
exit 1
}
[[ ! -d $2 ]] && {
echo "Error: $2 is not a directory or does not exist."
exit 1
}
srcdir=$1
dstdir=$2
for obj in $( cd $srcdir ; find . )
do
[[ -e $dstdir/$obj ]] && {
stat -oH srcobj $srcdir/$obj
stat -oH dstobj $dstdir/$obj
srcmode=${${srcobj[mode]}[4,7]}
dstmode=${${dstobj[mode]}[4,7]}
[[ $srcmode -ne $dstmode ]] && {
echo "Changing $obj perm from $dstmode to $srcmode"
chmod $srcmode $dstdir/$obj
}
[[ $srcobj[uid] != $dstobj[uid] ]] && {
echo "Changing $obj owner from $dstobj[uid] to $srcobj[uid]"
chown $srcobj[uid] $dstdir/$obj
}
[[ $srcobj[gid] != $dstobj[gid] ]] && {
echo "Changing $obj group from $dstobj[gid] to $srcobj[gid]"
chgrp $srcobj[gid] $dstdir/$obj
}
}
done
There might be a shorter solution, but I wanted to keep this simple and readable. ;-)
cptsalek - 15. Dez, 09:06
Trackback URL:
https://cptsalek.twoday.net/stories/5388622/modTrackback