BASH
Setting a Cron Job to Run at the End of the Month
This is a method to be able to run a cron job on the last day of month, as this is not easily supported. This method will support any month (from 28 days to 31 days), although you could run this every day with no consequence. It utilizes an expression that takes the current day (plus one day forward in the future) as a number minus the current day as a number. If the value is less than or greater than 0, then the command will run.
For example, if today is the 31st of the month, the expression will add one day in the future, so the first number would be “1”, the second number would be “31” – therefore the value is -le 0, so the command will run.
0 23 28-31 * * [ $(expr $(date +\%d -d '1 days') - $(date +\%d) ) -le 0 ] && echo true
comm – utility for easily comparing files (better than diff)
Comm is a very useful GNU program that is better than diff for finding unique lines between two given files. A good example would be one file with a big list of IP addresses, with another file that has a small list of IP addresses. In this example, the IP addresses in the small list are presumed to be present in the big list, but short of doing an advanced script using find, this is a quick and clean way to find the duplicates.
With no options, produce three-column output. Column one contains lines unique to file1, column two contains lines unique to file2, and column three contains lines common to both files.
-1 suppress lines unique to file1
-2 suppress lines unique to file2
-3 suppress lines that appear in both files
NOTE: The files MUST be sorted first, or the results will not be accurate.
##################################
contents of file1:
192.168.1.0
192.168.1.1
192.168.1.4
192.168.1.5
192.168.1.6
192.168.1.7
201.44.32.4
201.44.32.5
201.44.32.8
201.44.32.9
contents of file2:
192.168.1.1
192.168.1.5
201.44.32.5
201.44.32.8
#################################
Example (to find only the unique IP address in file1)
comm -3 file1 file2
# output
192.168.1.0
192.168.1.4
192.168.1.6
192.168.1.7
201.44.32.4
201.44.32.9
BASH redirection reference
#!/bin/bash # redirect stdout to file cmd > file # redirect stderr to file cmd 2> file # redirect both stderr and stdout to file cmd >& file # pipe cmd1's stdout to cmd2's stdin cmd1 | cmd2 # pipe cmd1's stdout and stderr to cmd2's stdin cmd1 2>&1 | cmd2 # print cmd1's stdout to screen and also write to file cmd1 | tee file # print stdout and stderr to screen while writing to file cmd1 2>&1 | tee file
Backup and Archive Nagios
This is a shell script that can be used to backup a Nagios configuration (or any group of files/directories in Linux or UNIX) locally, and to sync the backups to a remote location. This script works perfectly when there are two different Nagios instances in different locations, and this script can be used on both servers to backup and archive, and then rsync the files to the remote side – just by changing the three variables at the top of the script. Logging and emailing results of each job can be added in to the script as well.
A best practice that I implemented is to use SSH shared keys for the rsync. Use a non-root account and send the traffic along a trusted VLAN. This allows for the SSH to not prompt for a password every time the script is run, which should be automated through a cron job.
#!/bin/bash #################################### ###### Local System Variables ###### #################################### NAGIOS=usr/local/nagios LOCAL=/local/directory/path REMOTE=user@server:/local/directory/path #################################### ####### DO NOT CHANGE BELOW ######## #################################### BACKUP=$LOCAL/nagios-backup.tgz DATE=`date +"%F-%T"` export LOCAL export REMOTE ### check to see if current backup file exists ### if [ -f $BACKUP ] then echo "Backup file exists." mv $BACKUP $BACKUP-$DATE tar czf $BACKUP -C / $NAGIOS else echo "Backup file does not exist...creating." tar czf $BACKUP -C / $NAGIOS exit fi ### remove files older than seven days ### find $LOCAL -type f -mtime +7 -exec rm {} \; ### change the permissions of the file to the backups user ### chown -R backups:backups $LOCAL ### change to backups user to run the rsync script ### su backups -c /home/backups/rsync-files.sh ### rsync the files to the DR backup site ### rsync -avz --delete $LOCAL/ $REMOTE
BASH script to Email list of “new” files found
This is a quick-and-dirty script that I made to solve a real world scenario. I had to find a way to notify a data integration group of any new files that an outside vendor had sent up to our secure FTP SUSE Linux server. These were batched up into many, many files, all starting with the same few characters. This made it fairly easy to add a wildcard search, but the other parts deal with the fact that only NEW files needed to be identified, not any existing files that were already processed internally by the data integration group.
#!/bin/bash OLD=/root/filecheck/old.log NEW=/root/filecheck/new.log DIFF=/root/filecheck/diff.log RCPT=user@user.com # write an "old" file listing if first time run if [ -f $OLD ]; then ls -la /path/to/filetype/filetype* | awk '{print $9}' > $OLD fi # take a snapshot of the directory now, possibly capturing new files ls -la /path/to/filetype/filetype* | awk '{print $9}' > $NEW diff "$OLD" "$NEW" >> "$DIFF" # new file listing now becomes "old" for next run cat $NEW > $OLD # if new files are found, log and send out a message if [ -s $DIFF ]; then cat <<EOF | /usr/sbin/sendmail -t To: $RCPT From: sftp@sftp-server.company.net Subject: New Files Found `cat $DIFF` . EOF fi