Last active
January 7, 2020 19:42
-
-
Save Sleaker/ec1424ec9040327fe0d14a799e363495 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my @info = (); | |
my @keys = (); | |
my @systemGroups = (); | |
my @backupGroups = (); | |
my %groups = (); | |
my $key = ""; | |
my $value = ""; | |
my $newGID = 666; | |
my $success = 0; | |
my $count = 0; | |
print "\nRecreating groups ....\n"; | |
#TODO: refactoring | |
if (open(FH_GROUP, "</etc/group") and open(FH_OLD_GROUP, "<${DIR_OLD_LINUX}/etc/group")) { | |
@systemGroups = <FH_GROUP>; | |
@backupGroups = <FH_OLD_GROUP>; | |
close(FH_GROUP); | |
close(FH_OLD_GROUP); | |
} | |
foreach (@backupGroups) { | |
my @groupInfo = split(":", $_ ); | |
my $groupName = $groupInfo[0]; | |
foreach my $oldinfoline (grep { /^$groupName:x:/ } @backupGroups) { | |
my @oldInfo = split(":", $oldinfoline); | |
$GID_MAP{$groupName} = $oldInfo[0]; | |
} | |
} | |
if (open(FH_OLD_GROUP, "<${DIR_OLD_LINUX}/etc/group")) | |
{ | |
$success = 1; | |
while (<FH_OLD_GROUP>) { | |
my @groupInfo = (); | |
chomp; | |
@groupInfo = split(":", $_); | |
my $groupName = $groupInfo[0]; | |
if ($BACKUP_OS ne "redhat" and $GID_MAP{$groupName}) { | |
$newGID = $GID_MAP{$groupName}; | |
} | |
else { | |
$newGID = getNewGID($groupName); | |
} | |
if ($newGID >= $DEFAULT_GID) { | |
$groupInfo[3] = $newGID; | |
$groups{"$newGID"} = \@groupInfo; | |
} | |
} | |
close(FH_OLD_GROUP); | |
# | |
# Sort groups | |
# | |
@keys = sort(keys(%groups)); | |
foreach $key (@keys) { | |
@info = @{$groups{$key} }; | |
$value = $info[0] . ":x:" . $info[3] . ":"; | |
print "\n Group: " . $info[0] . " [" . $info[2] . " --> " . $info[3] . "]\n"; | |
if ( grep { /^$value/ } @systemGroups ) { | |
print " Status: IGNORED\n Group already exists\n"; | |
} | |
else { | |
if (addGroup($info[3], $info[0])) { | |
print " Status: SUCCESS\n"; | |
} | |
else { | |
print " Status: FAILED\n Failed to create group\n"; | |
last; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment