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!




Aug 26

Linus Torvalds - Living in a dream world?

Tag: SecurityMichael Lind Mortensen @ 12:36 am

When I originally decided to write this post I was a bit hesitant about attacking Linus too vigorously. I do have a huge amount of respect for what this man has achieved, however I’m not going to censor myself in any way and need to say what has been irritating me for some time.

About a month ago I saw a news feed related to Mr. Torvalds’ comments here: http://article.gmane.org/gmane.linux.kernel/706950. His comments has since then been on my mind quite a lot and have actual decreased my regard for this man’s opinion, as he seems completely ignorant to the state of things.

To be fair, I should state that I do work with computer security and I use FreeBSD on my Laptop and several of my servers, so I might be a bit bias. However I do also have a couple of Linux machines, a couple of Linux servers and two Microsoft Windows systems - no Mac however, but that’s another discussion. But even though his comments were somewhat directed at people like me, I still don’t fit his stereotype completely (and don’t get mad easily) and therefore believe I am objective enough to comment in an adult way.

So - let’s get started. Let’s just put in his quote in it’s complete form and I’ll begin explaining why he’s wrong:

Btw, and you may not like this, since you are so focused on security, one
reason I refuse to bother with the whole security circus is that I think
it glorifies - and thus encourages - the wrong behavior.

It makes "heroes" out of security people, as if the people who don't just
fix normal bugs aren't as important.

In fact, all the boring normal bugs are _way_ more important, just because
there's a lot more of them. I don't think some spectacular security hole
should be glorified or cared about as being any more "special" than a
random spectacular crash due to bad locking.

Security people are often the black-and-white kind of people that I can't
stand. I think the OpenBSD crowd is a bunch of masturbating monkeys, in
that they make such a big deal about concentrating on security to the
point where they pretty much admit that nothing else matters to them.

To me, security is important. But it's no less important than everything
*else* that is also important!

			Linus

So why is he wrong? Well, it’s really not that complicated - he neglects to recognize what’s important for the biggest users of Linux systems in the world - the businesses and universities! The most important issues for these users are and have always been: Stability, Security and Reliability. All these humongous users have systems that need to stay up and running 24-7 all year long - without incident! So to claim that security issues aren’t more important than “normal” bugs is just plain naive! Functionality isn’t important for anyone but the end users, as the companies and institutions have already made the functions they want available - they don’t want their users to be able to do a bunch of stuff they didn’t expect or plan, so for them it doesn’t matter at all. Stability is another issue however and I do agree there isn’t much difference between a stability issue and a security issue.

Also the stuff about OpenBSD users being black-and-white - he’s got to be kidding right? By simply stating this without proof, he makes himself seem untrustworthy to me, because in my experience, I’ve seen the exact opposite! Linux users are the people whom are black-and-white, the people whom hate Microsoft for no other reason than they’re not Linux, the people whom claim all software should be free and whom believe that enterprises are killing innovation. Even though I’ve seen these things, I still wouldn’t conclude “Linux people are often the black-and-white kind of people…”, as I had no way of knowing this unless I actually knew enough of the world population of Linux users to claim that the majority believe certain things.

Also “Masturbating monkeys” - Stop being a fucking child! I can respect using profanities to emphasize your points and it would be easier to respect you if you weren’t the fucking creator of a struggling OS - but seriously… self-pleasuring primates? - Grow up and show some respect! The OpenBSD project has come out with a lot of good stable software and talking about them in this way is basically just like spitting in their fucking faces! It’s no different than if I said Linux users were donkey-fucking scat-lovers - an equally retarded statement!

And concerning Mr. Torvalds’ last point:

To me, security is important. But it's no less important than everything
*else* that is also important!

Most will probably agree with this - as I also do - however we apparently have very different views of what is important and what isn’t important!




May 24

FreeBSD on Microsoft Hyper-V

Tag: UnixMichael Lind Mortensen @ 10:55 am

So I’ve been looking into the problem of getting FreeBSD running on Microsoft Hyper-V the last few days and then yesterday I finally got it working.

Apparently people have been quite upset that Hyper-V only offered support for Windows based systems and Linux SuSE systems, which is obviously not very bright or community friendly - to be quite honest I would rather use WMWare due to the very few *nix systems Hyper-V officially supports.

However you can’t always pick and choose, and in this particular situation - I couldn’t either! So I had to get it working, so first I tried FreeBSD 7.0 RELEASE with a bootonly .iso and tried to mount that in the virtual machine - however the only thing I got out of that was a disc trying to boot but failing almost immediately with the message:

“Can’t load kernel”

So I tried FreeBSD 6.3 STABLE instead since it was obviously due to the kernel loading with FreeBSD 7.0. But again - no luck and just the same message:

“Can’t load kernel”

So finally I tried FreeBSD 8.0 CURRENT and to my great surprise - this would actually boot and let me install. However there are a few quirks that I haven’t worked out yet. Like the fact that FreeBSD apparently doesn’t detect the network interface given to it by Hyper-V - something that is somewhat of a huge problem.

So anywho - FreeBSD 8.0 CURRENT works with Microsoft Hyper-V… so for all you people I’ve seen with this problem, use FreeBSD 8.0 for your install.

I’ll write more when I’ve done some more extensive benchmarking and configuration.