writeDate.pl
From Sepiola Wiki
#!/usr/bin/perl -w # # Copyright (C) 2011 stepping stone GmbH # Switzerland # http://www.stepping-stone.ch # support@stepping-stone.ch # # Authors: # Michael Eichenberger <michael.eichenberger@stepping-stone.ch> # # Licensed under the EUPL, Version 1.1 or – as soon they # will be approved by the European Commission - subsequent # versions of the EUPL (the "Licence"); # You may not use this work except in compliance with the # Licence. # You may obtain a copy of the Licence at: # # http://www.osor.eu/eupl # # Unless required by applicable law or agreed to in # writing, software distributed under the Licence is # distributed on an "AS IS" basis, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either # express or implied. # See the Licence for the specific language governing # permissions and limitations under the Licence. # # $Id$ # # ###################################################################################################### # /usr/local/scripts/online-backup/writeDate.pl ###################################################################################################### # Description # This script writes the current date and time into the file for each backup befor the rsnapshots # rotates the backup. This information is used by the Sepiola Online Backup Client. ###################################################################################################### ###################################################################################################### # Incorporate code with use (evaluates the included file at compile time). ###################################################################################################### use warnings; use strict; use Config::IniFiles; use Getopt::Std; use Sys::Syslog; # This defines the variables in the symbol table of the current package. use vars qw($opt_d $opt_D $opt_h); $| = 1; # Turn buffering off, so that the output is flushed immediately ###################################################################################################### # Start the POD documentation information ###################################################################################################### =pod =head1 NAME writeDate.pl =head1 DESCRIPTION This Script ... The configuration file for this script is stored in the same directory and is called writeDate.conf The script uses syslog for logging purposes. Command Line Interface (CLI) parameters: =over =item -D directory The users home directory. =item -d debug Turns the debug mode on. =item -h help This online help. =back =head1 USAGE writeDate.pl [-D directory] [-d debug] [-h help] =head1 CREATED 2007-09-16 michael.eichenberger@stepping-stone.ch created =head1 VERSION =over =item 2007-09-16 michael.eichenberger@stepping-stone.ch created =back =head1 INCORPORATED CODE Incorporate code with use: =over =over =item warnings; =item strict; =item Config::IniFiles; =item Getopt::Std; =item Sys::Syslog; =back =back =cut ###################################################################################################### # End the POD documentation information ###################################################################################################### ###################################################################################################### # Read the configuration file ###################################################################################################### my $cfg = Config::IniFiles->new( -file => "/usr/local/scripts/online-backup/writeDate.conf"); ###################################################################################################### # Process the single character command line arguments. ###################################################################################################### getopts('dD:h'); ###################################################################################################### # Variable definitions ###################################################################################################### my $debug = 0; # Debug modus: 0: off, 1: on my $backupDirectory = $cfg->val('Global', 'BACKUP_DIRECTORY'); my $backupTimeFile = $cfg->val('Global', 'BACKUP_BACKUP_TIME_FILE'); my $syslogName = $cfg->val('Syslog', 'SYSLOG'); my $currentDateTime =POSIX::strftime( "%Y-%m-%d %H:%M", localtime(time)); ###################################################################################################### # Help text ###################################################################################################### my $help = "\nPlease use pod2text $0 for the online help\n\n"; ###################################################################################################### # Error Messages ###################################################################################################### my $errorHomeDirectory = "\nPlease pass a users home directory to the script\n\n"; ###################################################################################################### # Main Programme ###################################################################################################### # Start logging openlog ($syslogName, 0, 'root'); syslog('info', "$0 started"); # Check the command line arguments checkCommandLineArguments(); # Make sure, that the users home directory exists if (!checkUsersHomeDirectory($debug, $opt_D)) { exit 1; } # Make sure, that the users backup directory exists if (!checkUsersBackupDirectory($debug, $opt_D, $backupDirectory)) { exit 1; } # For each existing clienbt backup write the current day and time into the file backupTime # For example: # /home/eichi/incoming/quito/.sst_backup/backupTime # 2007-08-30 22:41 # /home/eichi : The users home directory # /incoming : The users backup directory # /quito : The name of the client that was backuped # /.sst_backup : The meta data directory # /backupTime : The file which needs to be updated with the current date and time if (!writeDateTime($debug, $opt_D, $backupDirectory, $currentDateTime)) { exit 1; } syslog('info', "$0 successfully finished"); ###################################################################################################### # Subroutines ###################################################################################################### ###################################################################################################### # checkCommandLineArguments ###################################################################################################### # Description: # Check the single character command line arguments ###################################################################################################### sub checkCommandLineArguments{ # Check, if help was chosen. If yes, display help and exit if ($opt_h){ print $help; exit 1; } # End of if ($opt_h) # Check, if debug modus was chosen if ($opt_d){ $debug = "1"; print "Debug modus was turned on\n"; syslog('debug', "Debug modus was turned on"); } # End of if ($opt_d) # Check, if the users home directory was passed if (!$opt_D){ print $errorHomeDirectory; syslog('info', "No users home directory was passed to $0"); exit 1; } # End of if ($opt_d } # End of sub checkCommandLineArguments ###################################################################################################### # checkUsersHomeDirectory ###################################################################################################### # Description: # Checks, if the passed users home directory exists. ###################################################################################################### # Usage: # checkUsersHomeDirectory($debug,$opt_D); # Example: # my $result = checkUsersHomeDirectory("1","/var/backup/7/687/723/3723687/chroot"); # Return values: # 0: if the users home directory exists # 1: if the users home directory does not exist ###################################################################################################### sub checkUsersHomeDirectory { my $localDebug = $_[0]; my $localUsersHomeDirectory = $_[1]; if ($localDebug) { print "\n"; print "Debug sub checkUsersHomeDirectory: \$localUsersHomeDirectory: $localUsersHomeDirectory\n"; syslog ('debug', "sub checkUsersHomeDirectory: \$localUsersHomeDirectory: $localUsersHomeDirectory"); } # End if if ($localDebug) if (-d $localUsersHomeDirectory) { if ($localDebug) { print "Debug sub checkUsersHomeDirectory: The \$localUsersHomeDirectory $localUsersHomeDirectory exists\n"; } # End if if ($localDebug) # Write some information to the syslog daemon syslog ('debug', "sub checkUsersHomeDirectory: The \$localUsersHomeDirectory $localUsersHomeDirectory exists"); # Return true, if the users home directory exists return 1; } else { # Return false, if the users home directory doesn't exist return 0; } # End of if (-d $localUsersHomeDirectory) } # End of sub checkUsersHomeDirectory ###################################################################################################### # checkUsersBackupDirectory ###################################################################################################### # Description: # Checks, if the passed users backup directory exists. ###################################################################################################### # Usage: # checkUsersBackupDirectory($debug,$opt_D,$backupDirectory); # Example: # my $result = checkUsersBackupDirectory("1","/var/backup/7/687/723/3723687/chroot","/incoming); # Return values: # 0: if the users home directory exists # 1: if the users home directory does not exist ###################################################################################################### sub checkUsersBackupDirectory { my $localDebug = $_[0]; my $localUsersHomeDirectory = $_[1]; my $localUsersBackupDirectory = $_[2]; my $localBackupPath = "$localUsersHomeDirectory$localUsersBackupDirectory"; if ($localDebug) { print "\n"; print "Debug sub checkUsersBackupDirectory: \$localUsersHomeDirectory: $localUsersHomeDirectory\n"; syslog ('debug', "sub checkUsersBackupDirectory: \$localUsersHomeDirectory: $localUsersHomeDirectory"); print "Debug sub checkUsersBackupDirectory: \$localUsersBackupDirectory: $localUsersBackupDirectory\n"; syslog ('debug', "sub checkUsersBackupDirectory: \$localUsersBackupDirectory: $localUsersBackupDirectory"); print "Debug sub checkUsersBackupDirectory: \$localBackupPath: $localBackupPath\n"; syslog ('debug', "sub checkUsersBackupDirectory: \$localBackupPath: $localBackupPath"); print "\n"; } # End if if ($localDebug) if (-d $localBackupPath) { if ($localDebug) { print "Debug sub checkUsersBackupDirectory: The \$localBackupPath $localBackupPath exists\n"; } # End if if ($localDebug) # Write some information to the syslog daemon syslog ('debug', "sub checkUsersHomeDirectory: The \$localBackupPath $localBackupPath exists"); # Return true, if the users home directory exists return 1; } else { # Return false, if the users home directory doesn't exist return 0; } # End of if (-d $localBackupPath) } # End of sub checkUsersBackupDirectory ###################################################################################################### # writeDateTime ###################################################################################################### # Description: # For each existing clienbt backup write the current day and time into the file backupTime # For example: # /home/eichi/incoming/quito/.sst_backup/backupTime # 2007-08-30 22:41 # /home/eichi : The users home directory # /incoming : The users backup directory # /quito : The name of the client that was backuped # /.sst_backup : The meta data directory # /backupTime : The file which needs to be updated with the current date and time ###################################################################################################### # Usage: # writeDateTime($debug, $opt_D, $backupDirectory, $currentDateTime) # Example: # my $result = checkUsersBackupDirectory("1","/var/backup/7/687/723/3723687/chroot","/incoming", "2007-09-16 22:45"); # Return values: # 0: if the users home directory exists # 1: if the users home directory does not exist ###################################################################################################### sub writeDateTime { my $localDebug = $_[0]; my $localUsersHomeDirectory = $_[1]; my $localUsersBackupDirectory = $_[2]; my $localBackupPath = "$localUsersHomeDirectory$localUsersBackupDirectory"; my $localCurrentDateTime = $_[3]; if ($localDebug) { print "Debug sub writeDateTime: \$localUsersHomeDirectory: $localUsersHomeDirectory\n"; syslog ('debug', "sub writeDateTime: \$localUsersHomeDirectory: $localUsersHomeDirectory"); print "Debug sub writeDateTime: \$localUsersBackupDirectory: $localUsersBackupDirectory\n"; syslog ('debug', "sub writeDateTime: \$localUsersBackupDirectory: $localUsersBackupDirectory"); print "Debug sub writeDateTime: \$localBackupPath: $localBackupPath\n"; syslog ('debug', "sub writeDateTime: \$localBackupPath: $localBackupPath"); print "Debug sub writeDateTime: \$localCurrentDateTime: $localCurrentDateTime\n"; syslog ('debug', "sub writeDateTime: \$localCurrentDateTime: $localCurrentDateTime"); } # End if if ($localDebug) opendir LOCALBACKUPPATH, $localBackupPath; foreach (readdir LOCALBACKUPPATH) { # Ignore . and .. if ($_ !~ /^\.{1,2}$/) { # Create a variable containing the full path and name of the file backupTime # /home/eichi/incoming/quito/.sst_backup/backupTime my $localBackupTimeFile = "$localBackupPath/$_$backupTimeFile"; if ($localDebug) { print "Debug sub writeDateTime: Working on $localBackupTimeFile\n"; } # End of if ($localDebug) syslog ('debug', "sub writeDateTime: Working on $localBackupTimeFile"); # Only update the file with the current date, if it really exists if (-e $localBackupTimeFile) { if ($localDebug) { print "Debug sub writeDateTime: $localBackupTimeFile exists\n"; } # End of if ($localDebug) syslog ('debug', "sub writeDateTime: $localBackupTimeFile exists"); # Now we can write the current date into the file backupTime in # the form of 2007-09-16 17:01 open BACKUPTIMEFILE, ">$localBackupTimeFile"; print BACKUPTIMEFILE $localCurrentDateTime; if ($localDebug) { print "Debug sub writeDateTime: wrote $localCurrentDateTime to $localBackupTimeFile\n"; } # End of if ($localDebug) syslog ('debug', "sub writeDateTime: wrote $localCurrentDateTime to $localBackupTimeFile"); close BACKUPTIMEFILE; } else { if ($localDebug) { print "Debug sub writeDateTime: $localBackupTimeFile didn't exist\n"; } # End of if ($localDebug) syslog ('debug', "sub writeDateTime: $localBackupTimeFile didn't exist"); } # End of if (-e $localBackupTimeFile) } # End of if ($_ !~ /^\.{1,2}$/) } # End of foreach (readdir LOCALBACKUPPATH) closedir LOCALBACKUPPATH; return 1; } # End if sub writeDateTime
Back to Backup server set up