LetsEncrypt auto renew
Jump to navigation
Jump to search
Skriptas ima info iš mysql, pagal duombazes su prefix'u sport_ ir generuoja sąrašą pridėdant domeną, taip padaromas subdomenų sąrašas tolimesniam pratęsimui ar jo didinimui...
#!/usr/bin/perl -w
# Generate automatic certificate with certbot and make LetsEncrypt extending certificate for subdomains of the project domain, that expands drastically!
# (c) 2017 \dev\null
my $domain = ".sportinn.lt";
my $certbot = "/usr/bin/certbot";
my $certbot_param = "";
my $wwwdir = "/home/sportinn/public_html";
my $sql_debug = 1;
my $sql_prefix = "sport_";
my $log = "/var/log/autorenew.log";
my $service = "/etc/init.d/nginx reload";
use DBI;
my $dbh = DBI->connect("DBI:mysql:mysql:localhost;mysql_read_default_file=$ENV{HOME}/.my.cnf", undef, undef,{ PrintError => $sql_debug, RaiseError => 0 }) or die "Unable to connect to MySQL ($DBI::errstr)\n";
my $line = "-w $wwwdir";
my $sql = "SELECT SCHEMA_NAME AS `Database` FROM INFORMATION_SCHEMA.SCHEMATA where SCHEMA_NAME like \"$sql_prefix%\"";
my $sth = $dbh->prepare($sql);
$sth->execute();
while (my $row = $sth->fetchrow_hashref) {
my $string = $row->{Database};
$string =~ s/$sql_prefix//g;
$line = $line . " -d " . $string.$domain;
}
#my $cmd = "id -uag > $log 2>&1"; # TEST
my $cmd = "$certbot $line $certbot_param > $log 2>&1"; # PRODUCTION
if (system( $cmd ) == 0) {
print "Certificate generation complete!\n";
print "Now restarting $service ...\n";
system( $service ) == 0 or die "Unable to restart service $service: $!\n";
print "All done!\n";
} else {
print "Certificate renew failed!: View log on $log\n";
exit;
}