Sep 28

Simple backup script with notification

Tag: UnixMichael Lind Mortensen @ 2:07 am

So I recently had to come up with a simple backup solution that didn’t involve any fancy system and basically just gave one the opportunity to choose what directories to backup, where to back them up and notify if something goes wrong.

So I came up with a solution which, in my own view, could be better and more “clean”, however this is how it is right now. You’re welcome to take the script, change and redistribute it - it’s released under the BSD license.

You might notice it’s refering to a bunch of files - these files are the error messages sent in e-mails and need to be placed somewhere, with corresponding changes in the script. If they don’t, e-mail notification won’t work.

Right now it’s sending e-mails when it’s succesful in making a backup. Depending on how often you want your cron job to do a backup, it might be irritating to get that many e-mails, so all you need to do is basically just remove the last part so that it only notifies you if something goes wrong.. That will however create the possibility that the e-mail functionality of the server will stop working and you won’t notice.

But yeah.. Here’s the script + all the error-messages zipped for download: backup_script

I’ll be the first to admit it’s not optimal in any way.. it’s a quick solution and has a bunch of DRY (Don’t Repeat Yourself) violations… but hey.. it works and given it’s low complexity I don’t wanna prioritize cleaning it up that much - but you are more than welcome to do so!

Here’s the code copied into the post:

#!/bin/bash
# Copyright (c) 2008, Michael Lind Mortensen
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of Michael Lind Mortensen nor the
#       names of his contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY MICHAEL LIND MORTENSEN ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL MICHAEL LIND MORTENSEN BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
# Define settings
DATA="/home/user1 /home/user2 /etc /var/www/virtual /var/log" #Location of backup files.
DISC="/mnt/backup" # Mount location of backup disc
DEVICE="/dev/sdb1" # Backup devicename
MAILREC="info@example.com" # Define message recipients. Comma-separate for more recipients.
SENDER="noreply@example.com" # Define sender e-mail for error-messaging.
LOG="logfile.txt" # Location of the error logfile
DBPASS="databasepassword" # The root password to the mysql server
 
set $(date)
 
#####################################################
#  Define email functionality for error messaging   #
#                                                   #
# Parameters: MESSAGE - E-mail content from file    #
#                                                   #
#####################################################
function error_mailer {
        MESSAGE=$1
        SUBJECT="Backup process encountered errors!"
        # Send the email using mail
    if mail -s "$SUBJECT" -r "$SENDER"  "$MAILREC" < $MESSAGE
    then
        :
    else
        echo "$3-$2-$6-$4 - ERROR: Mail functionality not working!" >> $LOG
    fi
}
 
#####################################################
#  Define email functionality for success messaging #
#                                                   #
# Parameters: MESSAGE - E-mail content from file    #
#                                                   #
#####################################################
function success_mailer {
        MESSAGE=$1
        SUBJECT="Backup created succesfully!"
        # Send the email using mail
        if mail -s "$SUBJECT" -r "$SENDER" "$MAILREC" < $MESSAGE
        then
                :
        else
                echo "$3-$2-$6-$4 - ERROR: Mail functionality not working!" >> $LOG
        fi
} 
 
# Mount backup disc
if mount $DEVICE $DISC
then
        :
else
        echo "$3-$2-$6-$4 - ERROR: error while mounting disc!" >> $LOG
    error_mailer "/etc/backup_inc/errors/mount_error.txt"
fi
 
# Perform daily full backup
if tar cfz "$DISC/data/data_$3-$2-$6.tgz" $DATA
then
        echo "Data saved successfully!"
else
        echo "$3-$2-$6-$4 - ERROR: error while saving data!" >> $LOG
    error_mailer "/etc/backup_inc/errors/data_error.txt"
fi
 
# Create SQL dumps of databases
if mysqldump -u root --password=$DBPASS --all-databases > "$DISC/database/db_$3-$2-$6.sql"
then
        :
else
        echo "$3-$2-$6-$4 - ERROR: error while making mysqldump!" >> $LOG
    error_mailer "/etc/backup_inc/errors/mysqldump_error.txt"
fi
 
if gzip "$DISC/database/db_$3-$2-$6.sql"
then
        echo "MySQL databases saved and compressed succesfully!"
else
        echo "$3-$2-$6-$4 - ERROR: error while saving mysql databases!" >> $LOG
    error_mailer "/etc/backup_inc/errors/db_error.txt"
fi
 
# Unmount backup disc
if umount $DISC
then
        success_mailer "/etc/backup_inc/success.txt"
else
        echo "$3-$2-$6-$4 - ERROR: error while unmounting disc!" >> $LOG
        error_mailer "/etc/backup_inc/error/umount_error.txt"
fi

The fast person will probably notice that Wordpress writes < and > as &lt; and &gt; .. that’s just the code plugin messing things up and I hope you can figure out how to fix that yourselves.. if not, the zipfile has the code without any odd layout errors.

All in all the code should be fairly self-explanatory.. if not, there are plenty of comments to help you use the script or change it to whatever you need.

Last but not least - Remember to add a cron job with the script.. If you don’t there won’t be much automatic backup about it.

That’s just about it.. Enjoy!




Sep 11

Energy saving hype

Tag: DebunkingMichael Lind Mortensen @ 9:17 am

I saw a new TV-add from “Elsparefonden” this morning. As many before it, it tried to get people to think about how much power they consumed and thereby consume less to save the environment. Before I go on, you can watch the add here from YouTube:

So what’s my problem with this? Well, basically it’s just plain bullshit! Sure, power consumption will fire up energy production demands and thereby harm the environment because the energy production harms the environment. However the only reason why energy production harms the environment is because of low-quality materials like coal! Why the hell try to get people to go back in technological development by using less power, instead of just making our energy production more effective and clean (which would probably also be cheaper than all does damn adds all the time).

But apparently the global warming hype has become almost a religion, with fanatics fighting to jump back in time on a lot of fronts like electronics, food, industrial production etc. For some reason they believe the world population was better off with the status we had 50 years ago! Like for example a Linux guy I heard at FOSDEM 08, whom claimed that the German government should not use Windows Vista and instead use Linux - what was his reason? Vista uses more power and Microsoft was therefore fighting to destroy the environment…. Now, there are many valid arguments for using Linux instead of Vista, however saving the environment is definitely not one of them! It really pisses me off a lot of organizations are using this global warming frenzy to push their agendas, just by coating it with some environmental bullshit.

So - My conclusion? Shift all energy production to wind power and nuclear power - and stop irritating us with demands of lower power consumption. The only real reason to consume less power is saving money - power is expensive and will probably keep on getting more expensive for a long while, so if one can develop machines that consume less power, these machines will obviously be cheaper - this however has nothing to do with saving the environment.




Sep 09

Funny Christian music

Tag: JunkMichael Lind Mortensen @ 7:10 pm

This is probably the funniest Christian music I have ever heard.. Very very odd!




Sep 04

Tribute to the American “Flag”

Tag: JunkMichael Lind Mortensen @ 9:27 pm

!!VOTE FOR BARACK OBAMA!!

Oh, say, can you see, by the dawn’s early light,
What so proudly we hailed at the twilight’s last gleaming?
Whose broad stripes and bright stars, thro’ the perilous fight’
O’er the ramparts we watched, were so gallantly streaming.
And the rockets red glare, the bombs bursting in air,
Gave proof through the night that our flag was still there.
Oh, say, does that star-spangled banner yet wave
O’er the land of the free and the home of the brave?

On the shore dimly seen, thro’ the mists of the deep,
Where the foe’s haughty host in dread silence reposes,
What is that which the breeze, o’er the towering steep,
As it fitfully blows, half conceals, half discloses?
Now it catches the gleam of the morning’s first beam,
In full glory reflected, now shines on the stream;
‘Tis the star-spangled banner: oh, long may it wave
O’er the land of the free and the home of the brave.

And where is that band who so vauntingly swore
That the havoc of war and the battle’s confusion
A home and a country should leave us no more?
Their blood has wash’d out their foul footstep’s pollution.
No refuge could save the hireling and slave
From the terror of flight or the gloom of the grave,
And the star-spangled banner in triumph doth wave
O’er the land of the free and the home of the brave.

Oh, thus be it ever when free men shall stand,
Between their loved homes and the war’s desolation;
Blest with vict’ry and peace, may the heav’n-rescued land
Praise the Power that has made and preserved us as a nation.
Then conquer we must, when our cause is just,
And this be our motto: “In God is our trust”;
And the star-spangled banner in triumph shall wave
O’er the land of the free and the home of the brave.