#!/usr/local/bin/expect -f # ## PROPERTY OF BRANT PUTTKAMMER, PLEASE CONTACT ME IF YOU HAVE QUESTIONS ## PLEASE USE CAUTION WHEN RUNNING THIS PROGRAM ON YOUR SYSTEM set force_conservative 1 ;# set to 1 to force conservative mode even if ;# script wasn't run conservatively originally if {$force_conservative} { set send_slow {1 .1} proc send {ignore arg} { sleep .1 exp_send -s -- $arg } } # BEGIN EXPECT SCRIPT BELOW #* PROPERTY OF BRANT PUTTKAMMER, PLEASE CONTACT ME IF YOU HAVE QUESTIONS # SET THE EMAIL COMMANDS FOR COMPLETION AND ERRORS set UECUEmail "bputtkammer@uecu.coop" set AlertUECUEmail "bputtkammer@uecu.coop,brant.puttkammer@gmail.com" set SMSUECUEmail "123456789@txt.att.net" set EmailCommand "echo 'Subject:Problem with Tape Backup\nTape Backup did not complete' | sendmail UECUEmail" set DoneCommand "echo 'Subject:Done with Tape Backup\nTape Backup completed successfully!' | sendmail UECUEmail" # DATEADD FUNCTIONS AND VARIABLES INCLUDED BELOW source /udadmin/expectscripts/DateFormats.exp source /udadmin/expectscripts/ExpectConfig.exp set DoneCommand "echo 'Subject:Done with Tape Backup\nTape Backup completed successfully on $SMSDate!' | sendmail UECUEmail" set SMSCommand "echo 'Tape Backup completed successfully on $SMSDate!' | sendmail SMSUECUEmail" # INSERT MONTHENDDATES SOURCE FILE - LAST BUSINESS DAY OF MONTH END source /udadmin/expectscripts/MonthEndDates.exp # APPEND ME DATES TO VARIABLE BELOW IN CASE YOU WANT TO RUN A TAPE BACKUP ON SPECIFIED DAY # MAKE SURE TO INCLUDE A SPACE BETWEEN EACH DATE - " MMDDYY MMDDYY" append MonthEndDates " 041610" # SET THE TIMEOUT TO 10 MINUTES set timeout 600 set EmailText "\n" set LineBreak "\n" set backupstarted "NONE" set backupended "NONE" set blocksout "0" set blocksin "0" set AccountDirectory "$DefaultLiveAccountPath" set ScriptName "tape_backup.exp" if {$AccountDirectory == "/eom/MEA.EOM"} { set EmailSubject "Tape Backup - TEST" set LoginScript "/udadmin/expectscripts/TrainUsers.exp" } else { set EmailSubject "Tape Backup - LIVE" set LoginScript "/udadmin/expectscripts/LiveUsers.exp" } # TURN ON LOGGING - SET LOG VARIABLE NAME set LogFileName "/udadmin/expectlogs/TapeBackup-$FileDate.log" log_file $LogFileName # CHANGE FILE PERMISSIONS AND OWNER ON LOG FILE system "chmod 777 $LogFileName" system "chown expect $LogFileName" # START THE TELNET SESSION spawn telnet $HOSTADDRESS # LOGIN expect { timeout { set EmailCommand "echo 'Subject:Problem with $EmailSubject\\nExpect Statements:$EmailText' | sendmail $AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "login:" { send -- "$USERNAME\r" append EmailText $expect_out(0,string)$LineBreak } } # SEND PASSWORD expect { timeout { set EmailCommand "echo 'Subject:Problem with $EmailSubject\\nExpect Statements:$EmailText' | sendmail $AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "Password:" { send -- "$PASSWORD\r" append EmailText $expect_out(0,string)$LineBreak } } # Systems Administration Main Menu expect { timeout { system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" system "echo \"Expect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "Systems Administration Main Menu*Selection:" { send -- "1\r" append EmailText $expect_out(0,string)$LineBreak } } # =============================================================================== # * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * # BEGIN OF TAPE BACKUP - COMMENT OUT WHEN IMPLEMENTING ONLINE ONLY BACKUPS # System Backups and Tools expect { timeout { system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" system "echo \"Expect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "System Backups and Tools*Selection:" { send -- "2\r" append EmailText $expect_out(0,string)$LineBreak } } # Interactive Backup expect { timeout { system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" system "echo \"Expect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "Select backup option" { send -- "1\r" append EmailText $expect_out(0,string)$LineBreak } } # Daily tape backup expect { timeout { system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" system "echo \"Expect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "Do you want to execute this backup to" { send -- "y\r" append EmailText $expect_out(0,string)$LineBreak } } # SET TIMEOUT TO 90 MINUTES FOR DURATION OF TAPE BACKUP set timeout 5400 # BACKUP STARTED expect { timeout { system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" system "echo \"Expect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "device is not ready for" { system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" set EmailCommand "echo 'Subject:Problem with $EmailSubject\\nDevice is Not Ready\nExpect Statements:$EmailText' | sendmail AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" exit } "rewinding the tape drive" { system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" set EmailCommand "echo 'Subject:Problem with $EmailSubject\\nError Rewinding Tape Drive\nExpect Statements:$EmailText' | sendmail AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" exit } -re "Backup Started at (\[a-zA-z0-9: ]+)$CurrentYear" { set backupstarted $expect_out(1,string)$CurrentYear set BackupStartCommand1 "echo 'Subject:Backup Started\nTape backup has begun!\nTape backup is running.\n\nBackup Started: $backupstarted' | sendmail UECUEmail" # set BackupStartCommand2 "echo 'Subject:Backup Started\nTape backup has begun!\nTape backup is running.\n\nBackup Started: $backupstarted' | sendmail SMSUECUEmail" system $BackupStartCommand1 # system $BackupStartCommand2 append EmailText $expect_out(0,string)$LineBreak } } # BACKUP ENDED expect { timeout { system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" system "echo \"Expect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } -re "Backup Ended at (\[a-zA-z0-9: ]+)$CurrentYear" { set backupended $expect_out(1,string)$CurrentYear append EmailText $expect_out(0,string)$LineBreak } } # BLOCKS READ IN expect { timeout { set blocksout 0 } -re "Blocks read out to tape: (\[0-9]+) blocks" { set blocksout $expect_out(1,string) append EmailText $expect_out(0,string)$LineBreak } } # BLOCKS READ OUT expect { timeout { set blocksout 0 } -re "Blocks read in from tape: (\[0-9]+) blocks" { set blocksin $expect_out(1,string) append EmailText $expect_out(0,string)$LineBreak } } # Backup Started at Wed Dec 26 18:36:49 CST 2007 # Backup Ended at Wed Dec 26 18:57:04 CST 2007 # # Verifying tape # Blocks read out to tape: 10337088 blocks\r # Blocks read in from tape: 10337088 blocks\r expect { timeout { system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" system "echo \"Expect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "Press Enter when ready" { send -- "\r" append EmailText $expect_out(0,string)$LineBreak } } # SET TIMEOUT TO 5 MINUTES AFTER THE TAPE BACKUP set timeout 300 # ENDING OF TAPE BACKUP - COMMENT OUT WHEN IMPLEMENTING ONLINE ONLY BACKUPS # * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * # =============================================================================== # System Backups and Tools # Exit Back to Previous Menu expect { timeout { system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" system "echo \"Expect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "Exit to previous menu" { send -- "e\r" append EmailText $expect_out(0,string)$LineBreak } "Interactive Backup" { send -- "e\r" append EmailText $expect_out(0,string)$LineBreak } "System Backups and Tools*Selection:" { send -- "e\r" append EmailText $expect_out(0,string)$LineBreak } } # System Backups and Tools # Exit Back to Previous Menu expect { timeout { system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" system "echo \"Expect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "System Backups and Tools*Selection:" { send -- "e\r" append EmailText $expect_out(0,string)$LineBreak } } # Systems Administration Main Menu # Exit Program expect { timeout { send -- "e\r" } "Systems Administration Main Menu*Selection:" { send -- "e\r" append EmailText $expect_out(0,string)$LineBreak } } # ENDING SESSION expect { timeout { exit } eof } system "/udadmin/expectscripts/enable_users.exp" # system "echo COMPLETE > /udadmin/expectfiles/EOP-Status.txt" # CALCULATE TOTAL TIME OF RUNNING SCRIPT source /udadmin/expectscripts/total_time.exp source /udadmin/expectscripts/server_time.exp if {$TextString2 == "TRUE"} { set DoneCommand "echo 'Subject:Done with Backup\nBackup completed!\n\nBLOCKS READ FROM TAPE\nBlocks read out:$blocksout\nBlocks read in:$blocksin\n\nBackup Started: $backupstarted\n\nBackup Ended: $backupended\n$EmailSubject took, $TotalTime\nStart Time:$StartTimeDisplay\nEnd Time:$EndTimeDisplay\n\n$ServerTime\n\n$EmailNotes' | sendmail UECUEmail" # set SMSCommand "echo 'Subject:Done with Backup\nBlocks read out:$blocksout\nBlocks read in:$blocksin\nStarted: $backupstarted\nEnded: $backupended\n$EmailSubject took, $TotalTime\nStart Time:$StartTimeDisplay\nEnd Time:$EndTimeDisplay\n\n$ServerTime\n\n$EmailNotes' | sendmail SMSUECUEmail" # system $SMSCommand } else { set DoneCommand "echo 'Subject:Done with $EmailSubject\n$EmailSubject completed successfully on $SMSDate!\n\nPlease make sure to eject the tape and take off site or store in the vault!\n\n$EmailSubject took, $TotalTime\nStart Time:$StartTimeDisplay\nEnd Time:$EndTimeDisplay\n\n$ServerTime\n\n$EmailNotes' | sendmail UECUEmail" } system $DoneCommand # system $SMSCommand # UPDATE DAILY VERIFY FILE WITH SCRIPT COMPLETED system "echo 'COMPLETED, $ScriptName, $FileDate, $TotalTime' >> /udadmin/expectlogs/DailyVerify$FileDate.txt"