Hello,
Thank you for you script.
I add to script:
- remove file
- Send Email.
!/bin/bash
###############################################################
BACKUP_FILE=backup_CPSMS_`date +%d-%m-%y_%H%M`.tgz
BACKUP_LOG_FILE=/var/CPbackup/log/backups/backup_log_CPSMS_`date +%d-%m-%y_%H%M`.log
BACKUP_PATH=/var/CPbackup/backups
BACKUP_COMMAND="/opt/CPsuite-R77/fw1/bin/upgrade_tools/migrate export -n"
BACKUP_SERVER='server_adress'
BACKUP_USER='ftp_user_name'
BACKUP_PASS='ftp_password'
###############################################################
MAILSERVER='mail_server_address'
SENDER_EMAIL_ADDRESS='sender_email'
RECEIVER_EMAIL_ADDRESS='receiver_email
###############################################################
#Create backup
#############################################################
source /opt/CPshrd-R77/tmp/.CPprofile.sh
{
echo `date +%d-%m-%yT%H%M` "INFO: Backup script started"
cd $BACKUP_PATH
# Run backup command
echo `date +%d-%m-%yT%H%M` "INFO: Running $BACKUP_COMMAND $BACKUP_FILE"
$BACKUP_COMMAND $BACKUP_FILE
if [ $? -ne 0 ]; then
echo `date +%d-%m-%yT%H%M` "FATAL: Backup command did not complete successfully"
exit 1
else
echo `date +%d-%m-%yT%H%M` "INFO: Backup command completed successfully"
fi
# Test backup file integrity
echo `date +%d-%m-%yT%H%M` "INFO: Testing archive integrity..."
/bin/tar -tzf $BACKUP_FILE > /dev/null
if [ $? -ne 0 ]; then
echo `date +%d-%m-%yT%H%M` "FATAL: Archive integrity test failed. Backup file is corrupt."
exit 1
else
echo `date +%d-%m-%yT%H%M` "INFO: Archive integrity test succeeded. Backup file is good."
# Creating checksum file
echo `date +%d-%m-%yT%H%M` "INFO: Creating checksum file $BACKUP_FILE.MD5SUM"
/usr/bin/md5sum $BACKUP_FILE > $BACKUP_FILE.MD5SUM
echo `date +%d-%m-%yT%H%M` "INFO: Uploading archive to FTP server..."
/usr/bin/ftp -n $BACKUP_SERVER <<END_SCRIPT
quote USER $BACKUP_USER
quote PASS $BACKUP_PASS
cd CPSMS
put $BACKUP_FILE.MD5SUM
binary
put $BACKUP_FILE
quit
END_SCRIPT
if [ $? -ne 0 ]; then
echo `date +%d-%m-%yT%H%M` "ERROR: FTP upload failed."
else
echo `date +%d-%m-%yT%H%M` "INFO: FTP upload completed."
fi
fi
wait
/bin/rm $BACKUP_FILE.MD5SUM
/bin/rm $BACKUP_FILE
if [ $? -ne 0 ]; then
echo `date +%d-%m-%yT%H%M` "ERROR: Delete backup file failed."
else
echo `date +%d-%m-%yT%H%M` "INFO: Delete backup file completed."
fi
echo `date +%d-%m-%yT%H%M` INFO: Backup script completed
} > $BACKUP_LOG_FILE
################################################################
#Send Email
################################################################
{
echo -e "from:"$SENDER_EMAIL_ADDRESS"\r\nto:"$RECEIVER_EMAIL_ADDRESS"\r\nSubject: Backup checkpoitn MGMT\r\n\r\n Backup completed. \r\n"
cat $BACKUP_LOG_FILE
} > $BACKUP_LOG_FILE.mail
sendmail --domain=domain.com -f $SENDER_EMAIL_ADDRESS -v $RECEIVER_EMAIL_ADDRESS --host=$MAILSERVER < $BACKUP_LOG_FILE.mail >> $BACKUP_LOG_FILE
{
if [ $? -ne 0 ]; then
echo `date +%d-%m-%yT%H%M` "ERROR: Send email failed."
else
echo `date +%d-%m-%yT%H%M` "INFO: Send email completed."
fi
} >> $BACKUP_LOG_FILE
/bin/rm $BACKUP_LOG_FILE.mail
if [ $? -ne 0 ]; then
echo `date +%d-%m-%yT%H%M` "ERROR: Delete mail file failed." >> $BACKUP_LOG_FILE
else
echo `date +%d-%m-%yT%H%M` "INFO: Delete mail file completed." >> $BACKUP_LOG_FILE
fi