El 08/05/17 a las 23:09, Ulises González Horta escribió:
Hola

Con este script intento  resolver el clásico error de novatos de hacer
chmod -R 777 /var  o algo similar que luego deja todos los permisos
fuera de lugar y después nada levanta o trabajan con problemas. Mi
script permitiria "copiar" de un SO para otro los permisos y
propietarios. Digamos que tienes dos servidores, los cuales fueron
instalados con el mismo disco de instalación (algo que por lo general
hacemos todos), lo cual provoca que la estructura base de usuarios y
directorios sea la misma. Luego en el servidor A el admin mete la pata,
entoces puedes ir al servidor B, "copiar los permisos" y aplicarlos en
el servidor A. Obviamente no es algo perfecto, pero puede ayudar
bastante, si los dos servidores son 100% iguales entoces con solo correr
el script resuelves el problema de una vez, de lo contrario entoces
debes ver que te falta por ajustar, pero ya el script te ajustaria una
buena parte del problema

Espero que sea útil para alguien




ulises@ulinx:~/perl$ cat ownership.pl
#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Long;


my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
$blksize,$blocks,$permissions,$filename,$current_file);
#my $s_dir=shift || die "No arguments";
my @c_data;
my %seen_dirs;


# Variables for getoptions
my ($r_file,$w_file,$do,$s_dir,$noowner,$help);
GetOptions(
      'rfile=s' => \$r_file,
      'wfile=s' => \$w_file,
      'do=s' => \$do,
      'start=s' => \$s_dir,
      'noowner' => \$noowner,
      'help' => \$help,
);


if ( $help || !$do ) {
         help();
}

sub help {
         print <<EOF;
$0 Read/Restore the permissions recurively in a directory.

$0 has two main funtions:
1- Read the permissions and the ownership of files and directories and
store then into a file
2- Change the permissions and the ownership of files and directories in
concordance with a file
         previously created

Examples

$0 --do=read --wfile=store.txt  --start=/boot

Read the permissions in the directory /boot and store them in the file
"store.txt"

$0 --do=set --rfile=stote.txt [--noowner] --start=/boot

Set the permissions and ownership in the directory /boot as written in
the file "store.txt"

Options

--do  Set the behavior of the program to read or set permissions and
ownership.
         Possible values "set" for setting values or "read" for reading
values
         This option is mandatory if it's present in the command line the
program
         will exit and no modifications will be done
--wfile Defines the file for writing the collected data when running in
"read" mode
--rfile Defines the file for reading the permissions, ownership and
files to modify when
         running in mode "set"
--start Defines the start directory for all the operations
--noowner  Usefull only when do=set, it prevents $0 from changing the
owner of the files
--help  Shows this help

$0 version 0.2

EOF
         exit;
}

#Files
#$w_file="/tmp/ownership.txt";

#Collect file's data
sub collect {
         my $f=shift ;
         $current_file=$f;
         #Extract file data
         ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,
$ctime,$blksize,$blocks) = stat($f);

         #Take the interesting thing of the mode
         $permissions=sprintf "%04o", $mode & 07777;

         #print "$permissions $uid $gid $current_file". "\n";
         return $permissions,$uid,$gid,$current_file;
}

#Populate a data hash
sub populate_file {
         #Save the collect's retrieved data in an array
         #Permissions
         my $perm=shift;
         #uid
         my $own=shift;
         #gid
         my $group=shift;
         #filename
         my $fn=shift;
         open WDATA, ">> $w_file " or die "Unable to create $w_file";
         print WDATA "$perm;$own;$group;$fn\n";
         #Pte delete old data file!!!!!


}

# open a dir and extract the files
sub open_dir {
         my $l_s_dir =shift;
         if (  -d $l_s_dir ) {
                 print "Reading attribs in $l_s_dir \n";
                 opendir(my $dh, $l_s_dir) || die "Can't open $l_s_dir:
$!";
                         while (readdir $dh) {
                                 # Take away "." and ".."  --pending!!
                                 if ( $_ !~ /^\.(\.$)/ ) {
                                 if ( $_ !~ /^\.$/ ) {

                                         #print "here";
                                         #print "$_  \n";
                                         #print  "$s_dir/$_\n";
                                         ($permissions,$uid,$gid,
$current_file)=collect("$l_s_dir/$_");
                                         #print "$permissions $uid $gid
$current_file  \n";
                                         populate_file($permissions,$uid,
$gid,$current_file);
                                         if ( -d $current_file) {
                                                 #print "$current_file is
a directory \n";

#open_dir($current_file);
                                                 #
$seen_dirs{$current_file}++;
                                                 open_dir($current_file);
                                         }

                                 }}
                 }
                 closedir $dh;
         } else {
                 print "$l_s_dir is not a directory \n";
         }

         return $permissions,$uid,$gid,$current_file;

}

#Read data file
sub read_data {
         my $a_file = shift;
         my $t_dir= shift;
         open RDATA, "< $a_file " or die "Unable to open $a_file";
         my @readen_attrib=<RDATA>;
         close RDATA;
         for (@readen_attrib) {
                 #print "for $_ ";
                 chomp;
                 ($permissions,$uid,$gid,$filename)=split(/;/,$_ );
                 #print "$filename    regexp  $t_dir \n";
                 if ( $filename =~ /$t_dir/ ){
                         #print "$permissions,$uid,$gid,$filename\n";
                         set_attribs($permissions,$uid,$gid,$filename);
                 }
         }
}

#Set file's attributes
sub set_attribs {
         my $perm=shift;
         my $user=shift;
         my $group=shift;
         my $f_d=shift;
         #chomp $f_d;
         #print  "$perm $f_d \n" ;

         if ( -e $f_d ){
                 print "Using  $f_d \n" ;
                 #Perl level
                 print "Assigning $perm to '$f_d' \n";
                 chmod oct($perm), "$f_d";

                 unless ( $noowner ) {
                         print "Changing ownership of $f_d to user $user
and group $group \n\n";
                         chown $user, $group, "$f_d" ;
                 }

         } else {
                 print "File or directory: $f_d Not found!! \n";
         }



}


# See if I have to read permissions or set permissions
#print $do;
if ( $do eq "read"){
         help() if ! $w_file;
         my ($t_permission,$t_uid,$t_gid,$t_filename) =open_dir($s_dir);
} elsif ( $do eq "set" ) {
         help() if ! $r_file;
         #print "pte \n";
         read_data($r_file,$s_dir);
}


#for my $dirs ( keys %seen_dirs){
#       print "$dirs :-> ejecutando en segundo nivel de dirs \n";
#       open_dir($dirs);
#}

close WDATA;



gracias Ulises, por correos como este(y muchos otros parecidos por supuesto) es que aún tiene sentido para mi leer esta lista,

saludos,

--
Michael González Medina
Administrador de Red
Empresa de Informática y Comunicaciones del Ministerio de la Agricultura(EICMA)

______________________________________________________________________
Lista de correos del Grupo de Usuarios de Tecnologías Libres de Cuba.
Gutl-l@jovenclub.cu
https://listas.jovenclub.cu/cgi-bin/mailman/listinfo/gutl-l

Reply via email to