#!/usr/bin/perl use strict; use warnings; use POSIX 'strftime'; use File::Basename; use File::Copy; use File::Find::Rule; use Getopt::Long; # Variables. my $dir = '/etc/httpd/conf'; my $filename = 'httpd.conf'; my @object = File::Find::Rule ->file() ->name($filename) ->in($dir); my $conf = "$_"; my $conf_bn = basename($conf); my $conf_mtime = (stat($conf))[9]; my $date = strftime '%m%d%Y', localtime; my $backup = "$conf.$date"; my $backup_bn = basename("$conf.$date"); my $ltime = localtime(); my $domain = undef; my $client = undef; my $job_code = undef; # Display usage. sub usage { die "Usage: $0 --domain companydomain.com --client company [--job-code 12345] [--help].\n"; } &usage() unless @ARGV > 2; GetOptions ( 'domain=s' => \$domain, 'client=s' => \$client, 'job-code:i' => \$job_code, 'help' => &usage() ) or &usage(); sub add_vhost { if (-e $conf) { open (CONF, ">>$conf") || die "ERROR: Unable to open $conf_bn.\n"; print CONF "\n# Domain: $domain"; print CONF "# Client: $client"; print CONF "# Job Code: $job_code"; print CONF "# Date: $ltime"; print CONF ""; print CONF " ServerAdmin admin@$domain"; print CONF " DocumentRoot /var/www/html/$domain"; print CONF " ServerName $domain"; print CONF " ServerAlias www.$domain"; print CONF " ErrorLog /var/log/httpd/$domain/$domain-error_log"; print CONF " CustomLog /var/log/httpd/$domain/$domain-access_log common"; print CONF ""; print CONF "# End of configuration for $domain."; close CONF; if ($? != 0) { die "ERROR: $!.\n"; } } } foreach (@object) { # my $conf = "$_"; # my $conf_bn = basename($conf); # my $conf_mtime = (stat($conf))[9]; # my $date = strftime '%m%d%Y', localtime; # my $backup = "$conf.$date"; # my $backup_bn = basename("$conf.$date"); # my $ltime = localtime(); # Backup of old configuration. copy ($conf, $backup) || die "ERROR: Unable to create a backup. $!.\n"; if ($? == 0) { utime $conf_mtime, $conf_mtime, $backup || die "ERROR: Unable to preserve stats."; # Update time using old file's mtime. if ($? == 0) { print "OK: $backup_bn has been created.\n"; } } # New configuration update. &add_vhost(); }