I have seen a few posts lately about people accidently chmoding directories recursively and ending up not being able to access certain directories/run specific programs.
I recently had a need (for use on a webserver) to create a script that could backup permissions and restore them at a later date. I have seen other ways to do it but I chose this way since I could edit the restore file at a later date and only restore certain permissions.
For anyone that wants it - here it is:
if [ ! $1 ] || [ ! $2 ];then
echo Please run as \"permbackup.sh /path/to/base restore-perms.sh\"
echo To add a date \"permbackup.sh /path/to/base restore-perms-`date +%F`.sh\"
echo -n Starting to create restore file\
echo \#\!/bin/bash > $2
echo echo Restore permissions of $1 @ `date` >> $2
echo read -p \"Do you want to restore? [y/n] \" YN >> $2
echo if [ \"\$YN\" \!= \"y\" ]\; then >> $2
echo echo Abandoning restore >> $2
echo else >> $2
echo echo -n Starting restore\\\ >> $2
for file in `find $1`;do
echo -n .
echo echo -n . >> $2
type=`stat --format="%F" $file`
if [ "$type" != "Symbolic Link" ];then
echo chmod `stat --format="%a" $file` \"$file\" >> $2
echo chown `stat --format="%u:%g" $file` \"$file\" >> $2
echo echo -e \\\\nRestore complete >> $2
echo fi >> $2
chmod 700 $2
echo -e \\n\\nRestore point of permissions for $1 @ `date` created.\\n\\nPlease execute \"$2\" to restore permissions.\\n
As root - save this as a file called "permbackup.sh" in /usr/local/bin then chmod it 755:
chmod 755 /usr/local/bin/permbackup.sh
Running "permbackup.sh" will give you basic usage instructions. I have only tested this on RedHat 9 but it "should" work on any system where stat uses custom output formats.