Simple daily apt-get system update script!

Discuss Programming

Simple daily apt-get system update script!

Postby ZiaTioN » Tue Feb 03, 2004 9:03 pm

I wrote this for a few of my systems and thought I would share it here with all you good folks.

Code: Select all
#!/usr/bin/perl -w
 
################################################################
# Program: auto-update
# Programmer: ZiaTioN
# Requires: apt for Red Hat/Fedora
#
# This app does nightly checks for upgradable system packages.
# Simply drop this program in your cron.daily directory
# and set to executable (chmod +x).
#
# Note*
#  This could cause issues or loss of service, use at your own
#  risk. If a package is updated and has problems you might not
#  even be aware of the outage until hours (or days) later.
#
###############################################################
 
use strict;
 
my $timestamp = `date`;
my @time      = split(/ /, $timestamp);
my $file      = $time[2]."-".$time[1]."-".$time[5];
 
my $create_log    = "/var/log/system-update/$file";
my $grab_list     = `apt-get -y update`;
my $update_status = `apt-get -y dist-upgrade`;
my $clean         = `apt-get -y clean`;
my $timestamp2    = `date`;
 
open (FILE, ">>$create_log") || die "Cannot open $create_log\n";
print FILE "Log started at: ".$timestamp,"\n\n";
print FILE "###################\n";
print FILE "### List Status ###\n";
print FILE "###################\n";
print FILE $grab_list,"\n\n";
print FILE "##############################\n";
print FILE "### Package Upgrade Status ###\n";
print FILE "##############################\n";
print FILE $update_status,"\n\n";
print FILE "#######################\n";
print FILE "### Clean Up Status ###\n";
print FILE "#######################\n";
if ($clean eq "") {
   print FILE "Cleanup was successfull!\n\n";
}else{
   print FILE $clean,"\n\n";
}
print FILE "Log ended at: ".$timestamp2,"\n";
close(FILE);


You might need to customize the file path in the value for "$create_log" to reflect where you would like to log the results. If you like the path I have the directory "system-update" will need to be created inside "/var/log".

Just drop this little guy into "cron.daily" and forget about it. By default cron.daily will execute this script around 4:02 AM (depending on how many cron jobs you have in daily) every day.

Edited:
I added the "clean" and I hope you do not mind but I also kinda adopted your header style Void Main. :)
Last edited by ZiaTioN on Fri Feb 20, 2004 12:34 pm, edited 2 times in total.
ZiaTioN
administrator
administrator
 
Posts: 460
Joined: Tue Apr 08, 2003 3:28 pm

Postby Void Main » Tue Feb 03, 2004 9:20 pm

Don't know if you saw it or not but I also have a script for automatic updates in a cron job:

http://voidmain.is-a-geek.net/files/scr ... st-upgrade

It's been working flawlessly for me for quite a long time now (since I was running Red Hat 8 and apt).
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby ZiaTioN » Tue Feb 03, 2004 9:41 pm

No I did not know. Pretty funny it appears it does the exact same thing even logging the output of the commands. However I did not use "clean" which I might now. I also did not email the logs.
ZiaTioN
administrator
administrator
 
Posts: 460
Joined: Tue Apr 08, 2003 3:28 pm

UPDATE!

Postby ZiaTioN » Tue Jun 01, 2004 4:56 pm

Added a little more functionality as far as logging goes. Added a function to create the log directories if they do not exist and also added a little more organization as far as the log files go with seperating each month into it's own directory and creating them each time a new month hits.

Before there was minimal setup as far as creating the specific log file directory but now there is no setup required before running this app.

Code: Select all
#!/usr/bin/perl -w

################################################################
# Program: auto-update
# Programmer: ZiaTioN
# Requires: apt for Red Hat/Fedora
#
# This app does nightly checks for upgradable system packages.
# Simply drop this program in your cron.daily directory
# and set to executable (chmod +x).
#
# Note*
#  This could cause issues or loss of service, use at your own
#  risk. If a package is updated and has problems you might not
#  even be aware of the outage until hours (or days) later.
#
###############################################################

use strict;

my $timestamp = `date`;
my @time      = split(/ /, $timestamp);
my $file;
if ($time[2] ne "") {
   $file = $time[2]."-".$time[1]."-".$time[5];
}else{
   $file = $time[3]."-".$time[1]."-".$time[6];   
}

my $dir           = "/var/log/system-update/";
my $subdir        = $1 if ($file =~ /((\w+)(.)(\d+))/);
my $fullpath      = $dir.$subdir;
my $create_log    = $fullpath."/".$file;
my $grab_list     = `apt-get -y update`;
my $update_status = `apt-get -y dist-upgrade`;
my $clean         = `apt-get -y clean`;
my $timestamp2    = `date`;

system("mkdir $dir") if (!-e $dir);
system("mkdir $fullpath") if (!-e $fullpath);
open (FILE, ">>$create_log") || die "Cannot open $create_log\n";
print FILE "Log started at: ".$timestamp,"\n\n";
print FILE "###################\n";
print FILE "### List Status ###\n";
print FILE "###################\n";
print FILE $grab_list,"\n\n";
print FILE "##############################\n";
print FILE "### Package Upgrade Status ###\n";
print FILE "##############################\n";
print FILE $update_status,"\n\n";
print FILE "#######################\n";
print FILE "### Clean Up Status ###\n";
print FILE "#######################\n";

if ($clean eq "") {
   print FILE "Cleanup was successfull!\n\n";
}else{
   print FILE $clean,"\n\n";
}

print FILE "Log ended at: ".$timestamp2,"\n";
close(FILE);
ZiaTioN
administrator
administrator
 
Posts: 460
Joined: Tue Apr 08, 2003 3:28 pm


Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests