#!/bin/bash # This script creates a copy of the /opt/zimbra folder and then compress # it localy before sending it over the nework so make sure there is enough space # left on your local hard drive ($parentfold variable). # A samba share is used in this script, fill the ## NETWORKING ## section # and the $pathfold variable with your info. # sendEmail is used in this script, if you wish to use it, fill the ## MAILING ## section # with your info. dateformat=$(date +\%d-\%m-\%Y) # Format of the date used in the script. datebegin=$(date) # Date when the backup begins its routine. beginjob=$(date +%s) # Second at which the routine starts. remotename=mremote # Name of the temporary local folder where to mount the Samba share. pathfold=//PATH/TO/SHARE/MYSQL # Network folder for the backup; **folder must exist remotely**. parentfold=/tmp # Where it all begins. basefold=$parentfold/.mysql # Local parent folder created for the script usage. remotefold=$basefold/$remotename # Path of local folder to mount the remote Samba share. muser=root # User that launch the backup routine (root). mpass='ROOT-PASSWORD' # Password of the user that launch the backup routine. mserver='localhost' # Mysql server being backed up. bakname=mysql_$dateformat.bak.db.sql # Actual backup name. archname=mysql_$dateformat.tgz # Archive name that will be created. logpath=$basefold/ # Log file path. logfile=mysql_backup_LOGS_$dateformat.txt # Log file name. logging=$parentfold/$logfile # Complete log path. returns='' # Simple line return for the logs. mysqlv=$(mysql -V) # MySQL version uname=$(uname -a) # Kernel version lsb=$(lsb_release -d | cut -f2) # OS version ## INFO ## echo "======================================================== MySql Backup Summary - $(date +%d-%m-%Y) ======================================================== $returns" > $logging # Declaring mysql server version. echo "MySQL version: --------------- $mysqlv $returns" >> $logging # Declaring Operating System version. echo "Operating system version: -------------------------- $lsb $returns Kernel version: ---------------- $uname $returns" >> $logging echo "** The backup job started on: $datebegin. $returns * Initializing... " >> $logging ## LOCAL WORKING FOLDER ## # Creating the backup structure. echo '* Creating the temporary directory structure for the backup...' >> $logging if [ ! -d "$remotefold" ]; then mkdir -p $remotefold echo " - Successfully created the local working directory under $remotefold." >> $logging else echo " - Directory $remotefold exist, attempting to continue." >> $logging fi ## NETWORKING ## # Mounting the network folder where the backup will be sent. echo $returns >> $logging echo '* Attempting to Mount the Network Backup Folder...' >> $logging mount -t cifs $pathfold $remotefold -o user='USERNAME',pass='PASSWORD',domain='WORKGROUP' >> $logging if [ $? -eq 0 ]; then echo " - Remote path $pathfold successfully mounted." >> $logging else echo " - Error encountered while mounting remote path $pathfold." >> $logging # Creating a subfolder with the date as name on the remote foler. cd $remotefold #mkdir $dateformat if [ ! -d "$dateformat" ]; then mkdir $dateformat echo " - Successfully created a subfolder called $dateformat in the remote folder" >> $logging else echo " - Could not create a sub-directory called $dateformat as it already exists, continuing...." >> $logging fi ## MYSQL Backup ## # Backup command echo $returns >> $logging echo '* Taking the backup of ALL databases...' >> $logging cd $basefold mysqldump -u $muser -h $mserver --password="$mpass" --all-databases > $bakname if [ $? -eq 0 ]; then echo " - Successful dump of ALL databases in $basefold as $bakname." >> $logging else echo " - Mysqldump task encountered an error, please review your server logs." >> $logging fi rawsize=$(du -sh $bakname | cut -f1) ## COMPRESSION ## # Compressing the backup into a tar.gz archive echo $returns >> $logging echo '* Compressing the backup into a .tgz archive...' >> $logging tar cfz $archname $bakname > /dev/null 2>&1 if [ $? -eq 0 ]; then echo " - Successfully compressed the backup as $archname." >> $logging else echo " - Compression task encountered an error, please review your server logs." >> $logging fi # Calculating the size of the backup once compressed. compsize=$(du -sh $archname | cut -f1) ## REMOTE COPY ## # Copying the MySql backup archive from the local backup folder to the remote backup folder. echo $returns >> $logging echo "* Copying the Backup to the Network folder..." >> $logging cd $basefold cp $archname $remotefold/$dateformat cd ~ if [ $? -eq 0 ]; then echo " - Backup successfully transfered to $pathfold/$dateformat." >> $logging else echo " - Error encountered while transfering the backup, review your server logs." >> $logging fi ## VERIFICATION ## # Checking that the MySql service is still running. chkserv=$(ps aux | grep /usr/sbin/mysqld | grep -v grep) echo " * Querying the MySql service to ensure it is running... $chkserv $returns" >> $logging echo "Native database(s) size: $rawsize Compressed database(s) size: $compsize $returns" >> $logging # Ending stopwatch, calculating time difference and announcing it. dateend=$(date) echo "** The backup job ended on: $dateend. $returns" >> $logging endjob=$(date +%s) elapsed=$(( $endjob - $beginjob )) hours=$(( $elapsed / 3600 )) elapsed=$(( $elapsed - $hours * 3600 )) minutes=$(( $elapsed / 60 )) seconds=$(( $elapsed - $minutes * 60 )) echo "The backup routine took: $hours hours $minutes minutes $seconds seconds to complete. $returns" >> $logging wpath=$(echo $pathfold | sed -e 's/\//\\/g') echo "A copy of the present log can be found along with the backup at: Linux: smb:$pathfold Windows: $wpath" >> $logging echo "$returns $returns Backup routine completed. " >> $logging ## MAILING ## # Once completed, the log can be emailed easily by installing the sendEmail package. Comment out the next line # if you do not want to use sendEmail or change information accordingly. esender=mysql-backups@testdomain.com erecipient=admin@testdomain.com esubject='MySQL backup logs' ebody='This is the Backup Log of the MySQL server. Please review the attached document for detailed information.' sendemail -q -f $esender -t $erecipient -u $esubject -m $ebody -a $logging # Copying the logs over the network folder. cp $logging $remotefold/$dateformat # Unmount the remote backup folder. umount $remotefold ## CLEANING AND EXITING ## # Cleaning the server of the backup files/folders we created. rm -rf $basefold rm $logging exit
Recent Comments