top of page
Search

Mail archive Backup

  • Writer: Puneeth K P
    Puneeth K P
  • Mar 30, 2022
  • 2 min read

ree

1)Backup archive script

#!/bin/sh

now="$(date +'%d_%m_%Y_%H_%M_%S')"

filename="ucs_mail_archive_$now".tar.gz

backupfolder="/home/manage/BACKUP/"

sharefolder="/mnt/BACKUP/"

errorlogfile="$backupfolder/"error_log_"$(date +'%d_%m_%Y')".txt

logfile="$backupfolder/"backup_log_"$(date +'%Y_%m')".txt

echo "Mail Archive Backup started at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"

echo "rsyncing piler to shared folder" >> "$logfile"

rsync-no-vanished --progress -avz --perms /var/piler/ "$sharefolder" 2>"$errorlogfile" || { echo "Exit Code: $?">>"$errorlogfile"; echo "Mail Archive Backup failed while running rsync" | mail -s "Mail Archive Backup Failed"admin@puneeth.com && exit 1; }

echo "Mail Archive Backup finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"

echo "Mail Archive Backup Successful" | mail -s "Backup completed successfully" senthil@unitedconsultancy.com

echo "-----------------------" >> "$logfile"

root@email:/home/manage#


2)Tar and incremental backup


#!/bin/bash


################### Initialize variables #######################


now="$(date +'%d_%m_%Y_%H_%M_%S')"

week_of_month="$((($(date +%-d)-1)/7+1))"

month_of_year="$(date +'%b')"

filename="_ucs_mail_archive"

snapshot_file="snapshot_file"

backupfolder="/home/manage/BACKUP/MAIL_ARCHIVE"

#sharefolder="/mnt/BACKUP/MAIL_ARCHIVE"

sharefolder="/mnt/BACKUP/MAIL_ARCHIVE/${month_of_year}_Week_${week_of_month}"

logfile="$backupfolder/"backup_log_"$(date +'%Y_%m')".txt

errorlogfile="$backupfolder/"error_log_"$(date +'%d_%m_%Y')".txt


################################################################


############ Create Directory if not existing ##################


if [ ! -d "/mnt/BACKUP/MAIL_ARCHIVE/${month_of_year}_Week_${week_of_month}" ]

then

mkdir "/mnt/BACKUP/MAIL_ARCHIVE/${month_of_year}_Week_${week_of_month}"

fi


################################################################


############## Set Incremental Backup value ####################


incremental_no=$(ls "$sharefolder" | grep -i ucs_mail_archive | sort -r | awk -F '_' '{print $1}' | head -n1)

let incremental_no++


################################################################


echo "Mail Archive Backup started at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"


#################### Take Full Backup ##########################


if [ $incremental_no -eq 1 ]

then

echo "Creating tar file based on /var/piler (Full Backup) " >> "$logfile"

# tar --warning=no-file-removed -czvg "${sharefolder}/${snapshot_file}" -f "$sharefolder/${incremental_no}${filename}.tar.gz" /var/piler/ 2>"$errorlogfile" || { echo "Exit Code: $?">>"$errorlogfile"; echo "Cleaning up after Backup failure (Full Backup)" >> "$logfile"; echo "Deleting Directory($sharefolder)" >> "$logfile"; rm -rf "$sharefolder"; echo "-----------------------" >> "$logfile"; echo "Backup failed while Creating a full backup" | mail -s "Backup Failed" radmin@puneeth.com; exit 1; }

tar --warning=no-file-removed -czvg "${sharefolder}/${snapshot_file}" -f "$sharefolder/${incremental_no}${filename}.tar.gz" /var/piler/ 2>"$errorlogfile"

exitcode=$?

if [ "$exitcode" != "1" ] && [ "$exitcode" != "0" ]; then

echo "Exit Code: $exitcode">>"$errorlogfile"; echo "Cleaning up after Backup failure (Full Backup)" >> "$logfile"; echo "Deleting Directory($sharefolder)" >> "$logfile"; rm -rf "$sharefolder"; echo "-----------------------" >> "$logfile"; echo "Backup failed while Creating a full backup" | mail -s "Backup Failed" radmin@puneeth.com; exit 1;

fi

echo "Creating a copy(${sharefolder}/${incremental_no}_${snapshot_file}) of the current snapshot file" >> "$logfile"

cp "${sharefolder}/${snapshot_file}" "${sharefolder}/${incremental_no}_${snapshot_file}"


################################################################


################## Take incremental Backup #####################


else

echo "Creating tar file based on /var/piler (Incremental Backup) " >> "$logfile"

# tar --warning=no-file-removed -czvg "${sharefolder}/${snapshot_file}" -f "$sharefolder/${incremental_no}${filename}.tar.gz" /var/piler/ 2>"$errorlogfile" || { echo "Exit Code: $?">>"$errorlogfile"; echo "Cleaning up after Backup Failure (Incremental Backup)" >> "$logfile"; echo "Deleting Snapshot file (${sharefolder}/${snapshot_file})" >> "$logfile"; rm -f "${sharefolder}/${snapshot_file}"; echo "Deleting Incremental Backup file ($sharefolder/${incremental_no}${filename}.tar.gz)" >> "$logfile"; rm -f "$sharefolder/${incremental_no}${filename}.tar.gz" ; let incremental_no--; echo "Replacing previous snapshot(${sharefolder}/${incremental_no}_${snapshot_file}) as the current one" >> "$logfile"; cp "${sharefolder}/${incremental_no}_${snapshot_file}" "${sharefolder}/${snapshot_file}"; echo "-----------------------" >> "$logfile"; echo "Backup failed while Creating an incremental backup" | mail -s "Backup Failed" radmin@puneeth.com; exit 1; }

tar --warning=no-file-removed -czvg "${sharefolder}/${snapshot_file}" -f "$sharefolder/${incremental_no}${filename}.tar.gz" /var/piler/ 2>"$errorlogfile"

exitcode=$?

if [ "$exitcode" != "1" ] && [ "$exitcode" != "0" ]; then

echo "Exit Code: $exitcode">>"$errorlogfile"; echo "Cleaning up after Backup Failure (Incremental Backup)" >> "$logfile"; echo "Deleting Snapshot file (${sharefolder}/${snapshot_file})" >> "$logfile"; rm -f "${sharefolder}/${snapshot_file}"; echo "Deleting Incremental Backup file ($sharefolder/${incremental_no}${filename}.tar.gz)" >> "$logfile"; rm -f "$sharefolder/${incremental_no}${filename}.tar.gz" ; let incremental_no--; echo "Replacing previous snapshot(${sharefolder}/${incremental_no}_${snapshot_file}) as the current one" >> "$logfile"; cp "${sharefolder}/${incremental_no}_${snapshot_file}" "${sharefolder}/${snapshot_file}"; echo "-----------------------" >> "$logfile"; echo "Backup failed while Creating an incremental backup" | mail -s "Backup Failed" radmin@unitedconsultancy.com; exit 1;

fi

echo "Creating a copy(${sharefolder}/${incremental_no}_${snapshot_file}) of the current snapshot file" >> "$logfile"

cp "${sharefolder}/${snapshot_file}" "${sharefolder}/${incremental_no}_${snapshot_file}"

fi


################################################################


echo "Mail Archive Backup finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"

echo "Mail Archive Backup Successful" | mail -s "Mail Archive Backup completed successfully" radmin@puneeth.com

echo "-----------------------" >> "$logfile"


################################################################



 
 
 

Comments


Puneeth K P

  • Facebook Black Round
  • Twitter Black Round
bottom of page