Restoring permissions of /var/www after wrong chmod

Discussion in 'Tips/Tricks/Mods' started by shrek, May 30, 2009.

  1. shrek

    shrek New Member

    Hello, today I have written a simple script to make back default chmods of whole /var/www, I wrote it because of chmod -R done in wrong directory. May be somebody will enjoy it and will find it helpful

    It is done for [domain] indentifiers. And modyfies all files to "standard" if you have your own directories you will have to modify script.

    You're using script on your own responsibility!!
    It works well for me.
    Any suggestions or misteakes I'll be updating it on my blog page

    Code:
    #! /bin/bash
    #Shrek ISPConfig2 Permissions script
    #Variables
    root=root:root
    www=www-data
    no=.no_delete
    dir=/var/www
     
    #Gets web[0-9]* directories into table
    ls -alh $dir | grep drwx | grep -e "[0-9][0-9]:[0-9][0-9] web[0-9]" | awk '{print $9}' > .web_dirs
    n=1
    exec < .web_dirs
    while read line
    do
            web_dirs_list[$n]=$line
            n=$n+1
    done
     
    #echo ${#web_dirs_list[@]}
    no_of_dirs=${#web_dirs_list[@]}
     
    for ((k=1;k<=no_of_dirs;k++))
    do
            web=${web_dirs_list[$k]}
            echo "Restoring Permissions of $dir"
            echo $web
            #Owning a web[0-9] directory to www-data:web[0-9]
            chown www-data:$web /var/www/$web -R
            #Owning root permission files
            chown $root $dir/$web/cgi-bin/$NO
            chown $root $dir/$web/log/error.log
            chown $root $dir/$web/log/$NO
            chown $root $dir/$web/phptmp/$NO
            chown $root $dir/$web/ssl/$NO
            chown $root $dir/$web/user/$NO
            #Owning User files
            #Getting user namse/dirs
            ls -alh $dir/$web/user/ | grep drwx | awk '{print $9}' | grep -e ".[a-z]_[a-z]" > .users
            m=1
            exec < .users
            while read line
            do
                    user_list[$m]=$line
                    m=$m+1
            done
     
            #echo ${user_list[@]}
            no_of_users=${#user_list[@]}
            echo $no_of_users
            zero=0
            if(($no_of_users>=$zero ))
            then
                    #Owning files to their owners
                    for((x=1;x<=$no_of_users;x++))
                    do
                            user=${user_list[$x]}
                            echo "$web/user/$user"
                            chown $user:$web $dir/$web/user/$user -R
                            chown $root $dir/$web/user/$user/.antivirus.rc
                            chown $root $dir/$web/user/$user/.autoresponder.rc
                            chown $root $dir/$web/user/$user/.html-trap.rc
                            chown $root $dir/$web/user/$user/.local-rules.rc
                            chown $root $dir/$web/user/$user/.mailsize.rc
                            #Some users does not have .procmailrc file
                            chown $root $dir/$web/user/$user/.procmailrc
                            chown $root $dir/$web/user/$user/.quota.rc
                            chown $root $dir/$web/user/$user/.spamassassin.rc
                            chown $root $dir/$web/user/$user/.user_prefs
                            chown $root $dir/$web/user/$user/.vacation.msg
                    done
                    no_of_users=0
                    unset user_list[*]
            fi
            #Owning symlinks
            symlink=`ls -alh /var/www | grep $web | grep lrwx | awk '{print $9}'`
            echo $symlink
            chown $www:$web $dir/$symlink
    rm -f .users
    rm -f .web_dirs
    done
     
  2. mccharlet

    mccharlet HowtoForge Supporter

    Hi,

    If the backup was a tar (and not a zip) file the permissions is backuped.
     
  3. shrek

    shrek New Member

    hi,
    If there is no backup then no permissions are backuped ;]
    Or if we want to restore permissions in a very quick way, script will help a little with it.
     

Share This Page