捕获文件夹内所有文件的权限,并生成脚本


捕获文件夹内的所有权限和属主并保留成文件,当权限混乱后可以执行之前生成的文件来恢复权限。

Script to capture and restore file permission in a directory (for eg. ORACLE_HOME) (文档 ID 1515018.1) 转到底部转到底部

In this Document

Main Content
  Purpose
  Requirements
  Configuring
  Instructions
  Caution
  Script


APPLIES TO:

Oracle Database - Enterprise Edition
Generic UNIX
Generic Linux

MAIN CONTENT

PURPOSE

This script is intended to capture and restore the file permission of a given directory example - ORACLE_HOME. The script will create a output file called permission_<timestamp> and permission_<timestamp>.cmd

 

REQUIREMENTS

The script needs to be run on command prompt of Unix platform .
Perl is required to execute this script
Shell is required to run the shell script .

CONFIGURING

Download and save the script on your server as permission.pl
Provide the execute permission on the script

INSTRUCTIONS

 Run the script from the location where you have downloaded and saved it

./permission.pl <Path name to capture permission>

CAUTION

This sample code is provided for educational purposes only and not supported by Oracle Support Services. It has been tested internally, however, and works as documented. We do not guarantee that it will work for you, so be sure to test it in your environment before relying on it.

Proofread this sample code before using it! Due to the differences in the way text editors, e-mail packages and operating systems handle text formatting (spaces, tabs and carriage returns), this sample code may not be in an executable state when you first receive it. Check over the sample code to ensure that errors of this type are corrected.

Note : This script can restore permission back to the point at which it was captured. It is not intended to reset the permission.

 

SCRIPT

Execute the script from the dollar ($) prompt

Steps to capture permission of a directory

 1. Download the script from here
 2. Log in as "oracle" user
 3. copy the file to a location say /home/oracle/scripts
 4. Give execute permission

    $ chmod 755 permission.pl


 5. Execute the script to capture permission

  $ cd /home/oracle/scripts
  $ ./permission.pl <Path name to capture permission>

 

Script generates two files

a. permission-<time stamp> - This contains file permission in octal value, owner and group information of the files captured
b. restore-perm-<time stamp>.cmd - This contains command to change the permission, owner, and group of the captured files

Steps to restore captured permission of the directory

1. Give execute permission to file generated during capture

    chmod 755 restore-perm-<timestamp>.cmd

2. execute .cmd file to restore the permission and the ownership

    $ ./restore-perm-<timestamp>.cmd

 

Sample output of the script

permission-<time stamp>

755 oracle oinstall /u03/app/oracle/OraHome_11202g
750 oracle oinstall /u03/app/oracle/OraHome_11202g/root.sh
644 oracle oinstall /u03/app/oracle/OraHome_11202g/install.platform
640 oracle oinstall /u03/app/oracle/OraHome_11202g/oraInst.loc
644 oracle oinstall /u03/app/oracle/OraHome_11202g/afiedt.buf
644 oracle oinstall /u03/app/oracle/OraHome_11202g/a.out
6755 root root /u03/app/oracle/OraHome_11202g/tsh.sh
644 oracle oinstall /u03/app/oracle/OraHome_11202g/Readme.txt
640 oracle oinstall /u03/app/oracle/OraHome_11202g/oraorcl1122
644 oracle oinstall /u03/app/oracle/OraHome_11202g/SQLtraining_day1.lst
751 oracle oinstall /u03/app/oracle/OraHome_11202g/bin/hsots
751 oracle oinstall /u03/app/oracle/OraHome_11202g/bin/nid
6751 oracle oinstall /u03/app/oracle/OraHome_11202g/bin/oracle
751 oracle oinstall /u03/app/oracle/OraHome_11202g/bin/orapwd
751 oracle oinstall /u03/app/oracle/OraHome_11202g/bin/wrap
750 oracle oinstall /u03/app/oracle/OraHome_11202g/bin/grdcscan

 

 restore-perm-<time stamp>.cmd

chown  oracle:oinstall /u03/app/oracle/OraHome_11202g
chmod  755 /u03/app/oracle/OraHome_11202g
chown  oracle:oinstall /u03/app/oracle/OraHome_11202g/root.sh
chmod  750 /u03/app/oracle/OraHome_11202g/root.sh
chown  oracle:oinstall /u03/app/oracle/OraHome_11202g/install.platform
chmod  644 /u03/app/oracle/OraHome_11202g/install.platform
chown  oracle:oinstall /u03/app/oracle/OraHome_11202g/oraInst.loc
chmod  640 /u03/app/oracle/OraHome_11202g/oraInst.loc
chown  oracle:oinstall /u03/app/oracle/OraHome_11202g/afiedt.buf
chmod  644 /u03/app/oracle/OraHome_11202g/afiedt.buf
chown  oracle:oinstall /u03/app/oracle/OraHome_11202g/a.out
chmod  644 /u03/app/oracle/OraHome_11202g/a.out
chown  root:root /u03/app/oracle/OraHome_11202g/tsh.sh
chmod  6755 /u03/app/oracle/OraHome_11202g/tsh.sh
chown  oracle:oinstall /u03/app/oracle/OraHome_11202g/Readme.txt
chmod  644 /u03/app/oracle/OraHome_11202g/Readme.txt
chown  oracle:oinstall /u03/app/oracle/OraHome_11202g/oraorcl1122
chmod  640 /u03/app/oracle/OraHome_11202g/oraorcl1122
chown  oracle:oinstall /u03/app/oracle/OraHome_11202g/SQLtraining_day1.lst
chmod  644 /u03/app/oracle/OraHome_11202g/SQLtraining_day1.lst
chown  oracle:oinstall /u03/app/oracle/OraHome_11202g/bin/nid
chmod  751 /u03/app/oracle/OraHome_11202g/bin/nid
chown  oracle:oinstall /u03/app/oracle/OraHome_11202g/bin/oracle
chmod  6751 /u03/app/oracle/OraHome_11202g/bin/oracle
chown  oracle:oinstall /u03/app/oracle/OraHome_11202g/bin/orapwd
chmod  751 /u03/app/oracle/OraHome_11202g/bin/orapwd
chown  oracle:oinstall /u03/app/oracle/OraHome_11202g/bin/wrap
chmod  751 /u03/app/oracle/OraHome_11202g/bin/wrap
chown  oracle:oinstall /u03/app/oracle/OraHome_11202g/bin/grdcscan
chmod  750 /u03/app/oracle/OraHome_11202g/bin/grdcscan



点击(此处)折叠或打开

  1. #!/usr/bin/perl -w
  2. #
  3. # Captures file permissions and the owner of the files
  4. # useage : perm1.pl <path to capture permission>
  5. #

  6. use strict;
  7. use warnings;
  8. use File::Find;
  9. use POSIX();

  10. my (@dir) = @ARGV;
  11. my $linecount=0 ;

  12. #print @ARGV, $#ARGV;

  13. if ($#ARGV < 0) {
  14.    print "\n\nOpps....Invalid Syntax !!!!\n" ;
  15.    print "Usage : ./perm1.pl <path to capture permission>\n\n" ;
  16.    print "Example : ./perm1.pl /home/oralce\n\n" ;
  17.    exit ;
  18. }
  19. my $logdir=$dir[0] ;
  20. #my ($sec, $min, $hr, $day, $mon, $year) = localtime;
  21. ##my ($dow,$mon,$date,$hr,$min,$sec,$year) = POSIX::strftime( '%a %b %d %H %M %S %Y', localtime);
  22. my $date = POSIX::strftime( '%a-%b-%d-%H-%M-%S-%Y', localtime);
  23. my $logfile="permission-".$date;
  24. my $cmdfile="restore-perm-".$date.".cmd" ;

  25. open LOGFILE, "> $logfile" or die $! ;
  26. open CMDFILE, "> $cmdfile" or die $! ;
  27. find(\&process_file,@dir);

  28. print "Following log files are generated\n" ;
  29. print "logfile : ".$logfile. "\n" ;
  30. print "Command file : ".$cmdfile. "\n" ;
  31. print "Linecount : ".$linecount."\n" ;
  32. close (LOGFILE) ;
  33. close (CMDFILE) ;


  34. sub process_file {
  35.     my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks,$username,$user,$pass,$comment,$home,$shell,$group);
  36.     my %uiduname = () ;
  37.     my %gidgname = () ;
  38.     my $filename = $File::Find::name;


  39. #### Building uid, username hash

  40.     open (PASSWDFILE, '/etc/passwd') ;

  41.     while ( <PASSWDFILE>) {
  42.        ($user,$pass,$uid,$gid,$comment,$home,$shell)=split (/:/) ;
  43.        $uiduname{$uid}=$user ;
  44.     }
  45.     close (PASSWDFILE) ;


  46. #### Building gid, groupname hash

  47.     open (GRPFILE, '/etc/group') ;

  48.     while ( <GRPFILE>) {
  49.        ($group,$pass,$gid)=split (/:/) ;
  50.        $gidgname{$gid}=$group ;
  51.     }
  52.     close (GRPFILE) ;

  53.     ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat("$filename");
  54. # printf "%o %s %s %s\n", $mode & 07777, $uiduname{$uid}, $gidgname{$gid}, $filename ;
  55.     printf LOGFILE "%o %s %s %s\n", $mode & 07777, $uiduname{$uid}, $gidgname{$gid}, $filename ;
  56.     printf CMDFILE "%s %s%s%s %s\n", "chown ",$uiduname{$uid}, ":", $gidgname{$gid}, $filename ;
  57.     printf CMDFILE "%s %o %s\n", "chmod ",$mode & 07777, $filename ;
  58.     # printf "%o %s %s %s\n", $mode & 07777, $uiduname{$uid}, $gidgname{$gid}, $filename ;
  59.     $linecount++ ;
  60. }

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/24052272/viewspace-2131510/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/24052272/viewspace-2131510/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值