Getting Ubuntu 9.10 Karmic Koala working in OpenVZ

30 12 2009

If you're an Ubuntu fan like me and use (or want to use) OpenVZ, you might not be too excited that the OpenVZ site doesn't yet have the latest edition of Ubuntu, 9.10 Karmic Koala, available as a precreated template.  Luckily, you can find them here.


For my purposes, I downloaded ubuntu-9.10-minimal_9.10_amd64.tar.gz.  After setting up a virtual instance with Proxmox and opening up a VNC session, I found that this template is way more minimal than I ever expected.  In all reality, that isn't so bad.  The lighter, the better.  Unfortunately, since I was setting up this new instance as a server, I had a good bit of work ahead of me.


Firstly, networking wasn't working at all with my bridged setup out of the box.  The solution:


edit /etc/network/interfaces and configure your NICs.  I had to comment out some properties of the venet0 interface to get things working.  Here's my final config file:


CODE:
auto lo
iface lo inet loopback
auto eth0
# iface eth0 inet dhcp
iface eth0 inet static
        address 10.x.x.x
        netmask 255.255.255.0
        gateway 10.x.x.y

auto venet0
iface venet0 inet static
        address 127.0.0.1
        netmask 255.255.255.255
 


Notice that I'm using a static IP address.  You may want to try DHCP starting out since it'll make things easier.  Confirm network connectivity by pinging a remote host.  Don't forget to edit /etc/resolv.conf if you need to.


Now, I need to get some goodies installed on this thing.  In the end, I'm looking to install Virtualmin and host some websites, so I need to prep for that.  Luckily, Ubuntu makes it easy to get things started quickly.  Start off by issuing:



apt-get update && apt-get install tasksel && tasksel


An ncurses dialog should appear.  Go ahead and select what you need.  I can tell you that I learned the very hard way by not initially selecting "Basic Ubuntu Server."  If you don't select that (don't worry, it's only a couple of small packages), none of your services will start at boot-time and you'll have to start each of them manually.


From there, you should be on your way.  I strongly recommend Webmin (add the apt repo) to ease server administration tasks.


Also, before I leave you be, if you're not using Proxmox to perform all of this OpenVZ magic, you're definitely missing out.  Hurry up and convert already!






Setting up Virtualmin on Ubuntu 9.10 Karmic Koala

19 12 2009

According to the Virtualmin website, the latest version of Ubuntu that is supported is 8.04 LTS.  That's probably a safe stance to take since non-LTS versions of Ubuntu have a six-month lifecycle, and most serious hosting companies would stick with LTS versions.  For those of us who live on the bleeding edge and want to try Virtualmin on the latest version of Ubuntu, 9.10 (Karmic Koala), you'll find that the standard method of installation (the install.sh script on their site) will fail.


Luckily, the solution is simple.  


Add the following repository to your sources.list file:


deb http://software.virtualmin.com/gpl/debian virtualmin-universal main


Then run the following as root:


cd /root
wget http://software.virtualmin.com/lib/RPM-GPG-KEY-virtualmin
wget http://software.virtualmin.com/lib/RPM-GPG-KEY-webmin
apt-key add RPM-GPG-KEY-virtualmin
apt-key add RPM-GPG-KEY-webmin
apt-get update ; apt-get install webmin webmin-virtual-server


Webmin and Virtualmin will now be installed.  Note that the repository may not be as up-to-date as the direct download link.  If that scenario arises, simply find the URL of the latest Virtualmin package from the link, and do:

wget http://download.webmin.com/download/virtualmin/webmin-virtual-server_3.77.gpl_all.deb ; dpkg -i webmin-virtual-server_3.77.gpl_all.deb



GWMover: WAN Failover Script for Linux

22 03 2009

This is a script I wrote a while back but never posted up.  It allows you to switch from one gateway to another by repeatedly pinging your ISP gateway (or another external IP) and--upon detection of failure--switching to another gateway.  If the primary line is up upon the next run, we switch over to it.

This isn't a very advanced script and doesn't factor in other considerations, such as the state of the physical interfaces.  It simply attempts a number of pings, and considers the link dead or unreliable if they don't all come back.

If you have mutt installed, you will get an email alert when the primary link is considered 'dead.'  You can easily change this behavior by modifying the script.

It will probably work on other POSIX/Unix-like OSes, but I've only tested it on Linux in a BASH environment.  With all that said, here are the goods:


CODE:
#!/bin/bash
# GWMover:  WAN/Gateway failover
#  Pings gateway and switches to backup line when it goes down.
#  Reverts to primary line when gateway becomes accessible.
# Modified version of script found at:
# http://www.howtoforge.com/forums/showthread.php?p=55402#post55402
#
# Last modified:  August 2, 2007
# Ameir Abdeldayem

PRIMARYPUB="66.92.162.1"
PRIMARYGW="10.0.5.1"
SECONDARYGW="10.0.0.1"

EMAILS="email@ddress1 email@ddress2"

function is_host_alive() # Returns success or failure as boolean
{
PACKETS=3
TRIES=5
IP=$1 # saving contents in $1 before is used
echo "Pinging $IP"
i=0
while [ $i -lt $TRIES ]; do
REQUEST=$(ping -qc $PACKETS $IP |grep packets| cut -d" " -f1,4)
set -- $REQUEST
echo -e "Try $i: $1 $2     "
if [ $1 != $2 ]; then
echo "Bad news. $1 sent, $2 received."
# exit
exitcode=1
else
echo "Great! $1 sent, $2 received."
exitcode=0
fi
i=$(expr $i + 1)
done
return $exitcode
}

CURRENTGW=`route | grep "default" | awk '{print $2}'`
function changegateway()
{

if [ $1 != $CURRENTGW ]; then
echo ".  We need to change the default route. After: $1 Before: $2"
route add default gw $1
route del default gw $2
echo "The routing table has been changed!"
route # view current routing table
echo -e "$4 \n\nHere is the updated routing table:\n$(route)" | mutt -s "$3 ($(hostname))" $EMAILS
else
echo ", and we are already pointing to it."
fi

}


function test()
{
echo $1 $EMAILS
}

##################
# main starts here
##################

if is_host_alive $PRIMARYPUB ; then
echo -n "The primary gateway is alive"
SUBJECT="$PRIMARYPUB IS NOW UP"; BODY="The server $PRIMARYPUB is now up and was pinged via the current gateway of $CURRENTGW."
changegateway $PRIMARYGW $SECONDARYGW "$SUBJECT" "$BODY"
# test $PRIMARYGW
else
echo -n "The primary gateway is dead <img src="/blog/templates/default/img/emoticons/sad.png" alt=":-(" style="display: inline; vertical-align: bottom;" class="emoticon" /> We should be on the backup line"
SUBJECT="$PRIMARYPUB IS DOWN"; BODY="The server $PRIMARYPUB could not be pinged via the current gateway of $CURRENTGW."
        changegateway $SECONDARYGW $PRIMARYGW "$SUBJECT" "$BODY"
fi

Download it here.



Sync SVN repository with remote FTP server

19 01 2009

This script was written for the company I work for well over a year ago, but I just decided to release it to the public now.  It has been in use all of this time and is still in use now, so that should tell you that it's a pretty reliable script.  Any bugs we encountered over the course of using this program have been fixed.  Hopefully you'll have the same luck as us.

Basics:

This script is run as a SVN hook (post-commit).  After committing changes to your subversion repository, this script is invoked and synchronization begins.  The script creates a local copy of the repository, and files just committed are sent via FTP.

Usage:

One copy of the script can be used for an unlimited number of repositories, so the script can be placed in a globally-available location.  In our scenario, we placed the script at /usr/local/bin/svn2ftp.sh .  There are four command-line arguments to the script that are required.  The first two arguments can be kept the same as below, as they're passed to the script by subversion.  The third parameter is the path between the repository and the trunk (e.g., if your SVN path is http://<host>/svn/repo/dir/trunk, then you'd put "dir" as the parameter; otherwise, you'd put "." if there's no directory in between).  The fourth parameter is the full path to the config file of the repo.  A sample to put in repo/hooks/post-commit:

CODE:
# Update this repo with remote FTP server
DATE=`date +'%m-%d-%Y'`
/usr/local/bin/svn2ftp.sh "$REPOS" "$REV" "." "/svn2ftp/conf/repo.conf" >> /svn2ftp/logs/repo-$DATE.log

We have our directory structure as follows, but you can change it as you please:

/svn2ftp/conf     #  Where config files are stored

/svn2ftp/logs     #  Where log files are stored

/svn2ftp/repos   #  Where raw repo contents are stored

A sample config file can be found below.

The code:

I initially wrote this program using the Linux 'ftp' program.  It works well and all, but if I encounter server or any other errors, the program still exits with a successful error code.  This was a bummer in our situation, so I wrote an FTP client in PHP that reports errors correctly.  When an error is encountered, files that were not uploaded successfully will be retried upon the next commit.  Links to both versions, as well as the config file, are below:

Version using Linux 'ftp' program

Version using PHP FTP program

Sample config file

PHP FTP Updater


Installing Camelia OCaml IDE on Ubuntu Gutsy Gibbon

03 11 2007

[will be filled in later; put up for immediate use]

CODE:
sudo apt-get update
sudo apt-get install g++
sudo apt-get install qt3-dev-tools


tar xvfz camelia-1.1.tar.gz
cd src
sudo ./config
sudo make
sudo make install


sudo nano console/console.h

look for the following block of code and insert the last line:

class OProcess;
#include "ocamlprocess.h"
#include "debugprocess.h"
#include "olineedit.h"
#include "color.h"
#include "debugger/debug.h"
class Debugger;
class OCamlProcess; // insert this line here

You'll get an error regarding the tips if you don't complete this step:
sudo chmod -R 755 /usr/local/lib/camelia/tips/

Finally, run Camelia:
/usr/local/bin/camelia
You can also simply type "camelia" at the command line, so long as /usr/local/bin/ is in your $PATH; it should be on a default Ubuntu install.

My Favorite Filesharing Services

18 11 2006

A good filehost is important for use in many different applications.  It can help you to share large files with friends and colleagues, and can really be useful in more life-saving applications, such as backups.  I use these services to accomplish both, although I do admit I rely on them heavily in parallel with my backup scripts; I get a nightly backup of each of my MySQL databases to multiple email accounts, as well as to FTP.  I know it's super-redundant to happen this way, but I learned my lesson from the past.  Plus, this level of redundancy is pretty nice.  Here is a list of some of the file hosts that I use, and will update the table later with more details.

NameFilesize LimitDownload SpeedUpload SpeedComments
www.oxyshare.com700Mb??Used to be my favorite file host until things started going wrong.
www.up-file.com2GB???
www.mediafire.com100MB???
www.gigasize.com????
www.mihd.net1GB???
www.files-upload.com1GB???

MySQL Backup to FTP and Email Shell Script for Cron v2.1

23 09 2006

I updated the script just a tad bit. Using the magic of sed, this version of the script makes a backup of each database individually when you choose to backup all of your databases. The previous version dumped all of the databases into a single file. When you run this script and have it emailed to you, you'll end up with multiple attachments, each attachment being a single database name. Why did I do this? Well, I myself needed to restore a database and I had all the databases in a single file. I ended up searching throughout the file to find where surrounding databases started or stopped and copied the info in between. It wasn't very fun, especially since the databases weren't very small. This script gets around that headache completely. If you still prefer to have all of your DB data dumped into a single file, search for "MySQL Backup to FTP and Email Shell Script for Cron v2". Both scripts are exactly the same besides this feature.


CODE:
#! /bin/bash

# Ameir Abdeldayem
# http://www.ameir.net
# You are free to modify and distribute this code,
# so long as you keep my name and URL in it.

# your MySQL server's name
SERVER=ameir.net

# directory to backup to
BACKDIR=~/backups

# date format that is appended to filename
DATE=`date +'%m-%d-%Y'`

#----------------------MySQL Settings--------------------#

# your MySQL server's location (IP address is best)
HOST=localhost

# MySQL username
USER=username

# MySQL password
PASS=password

# List all of the MySQL databases that you want to backup in here, 
# each separated by a space
DBS="db1 db2"

# set to 'y' if you want to backup all your databases. this will override
# the database selection above.
DUMPALL=y


#----------------------Mail Settings--------------------#

# set to 'y' if you'd like to be emailed the backup (requires mutt)
MAIL=y

# email addresses to send backups to, separated by a space
EMAILS="1@gmail.com 2@inbox.com 3@goowy.com"

SUBJECT="MySQL backup on $SERVER ($DATE)"

#----------------------FTP Settings--------------------#

# set "FTP=y" if you want to enable FTP backups
FTP=n

# FTP server settings; should be self-explanatory
FTPHOST="ftp.server.com"
FTPUSER="username"
FTPPASS="pass"

# directory to backup to. if it doesn't exist, file will be uploaded to 
# first logged-in directory
FTPDIR="backups"

#-------------------Deletion Settings-------------------#

# delete old files?
DELETE=y

# how many days of backups do you want to keep?
DAYS=3

#----------------------End of Settings------------------#

# check of the backup directory exists
# if not, create it
if  [ -e $BACKDIR ]
then
echo Backups directory already exists
else
mkdir $BACKDIR
fi

if  [ $DUMPALL = "y" ]
then
echo "Creating list of all your databases..."

mysql -h $HOST --user=$USER --password=$PASS -e "show databases;" > dbs_on_$SERVER.txt

# redefine list of databases to be backed up
DBS=`sed -e ':a;N;$!ba;s/\n/ /g' -e 's/Database //g' dbs_on_$SERVER.txt`
fi

echo "Backing up MySQL databases..."
for database in $DBS
do
mysqldump -h $HOST --user=$USER --password=$PASS $database > \
$BACKDIR/$SERVER-mysqlbackup-$database-$DATE.sql
gzip -f -9 $BACKDIR/$SERVER-mysqlbackup-$database-$DATE.sql
done

# if you have the mail program 'mutt' installed on
# your server, this script will have mutt attach the backup
# and send it to the email addresses in $EMAILS

if  [ $MAIL = "y" ]
then
BODY="Your backup is ready! Find more useful scripts and info at http://www.ameir.net"
ATTACH=`for file in $BACKDIR/*$DATE.sql.gz; do echo -n "-a ${file} ";  done`

echo "$BODY" | mutt -s "$SUBJECT" $ATTACH $EMAILS
        
echo -e "Your backup has been emailed to you! \n"
fi

if  [ $FTP = "y" ]
then
echo "Initiating FTP connection..."
cd $BACKDIR
ATTACH=`for file in *$DATE.sql.gz; do echo -n -e "put ${file}\n"; done`

ftp -nv <<EOF
open $FTPHOST
user $FTPUSER $FTPPASS
cd $FTPDIR
$ATTACH
quit
EOF
echo -e  "FTP transfer complete! \n"
fi

if  [ $DELETE = "y" ]
then
find $BACKDIR -name "*.sql.gz" -mtime $DAYS -exec rm {} \;

if  [ $DAYS = "1" ]
then
echo "Yesterday's backup has been deleted."
else
echo "The backup from $DAYS days ago has been deleted."
fi
fi

echo Your backup is complete!

Download backupmysql-2.sh

LDAP Authentication PAM/NSS Using Debian or Ubuntu Bash Script v2

02 09 2006
Okay, so the old script wasn't that great (well I don't think so), mainly because of how I dealt with nsswitch.conf. I had the old script download it from the internet. Sure, it worked for me when I made the script, but that method can lead to many problems, especially when servers go down (which did happen). With the help of God I discovered sed. It's like find/replace, but even better. It has features dripping out of places I'd rather not see. Well anyways, the script is below. I also added a function to see if you were root or not. When I ran the old script on DreamLinux as root, it made new files in the pam.d directory, but they were all empty! The new method hit the spot right. Let me know if it works for you or not ;-)

CODE:
#! /bin/bash

# This script will install an LDAP authentication client for 
# Debian-based systems.  It relies on apt-get for package
# installation.  If you are using Ubuntu or Mepis, make sure
# you have the 'universe" repository enabled.  The packages we
# need are in there.
#
# Suppose the script's filename is ldapconf.sh
# If you are running it as a sudo user, type:
# chmod +x filename && sudo ./ldapconf.sh
#
# If you are root, run it as:
# chmod +x filename && ./ldapconf.sh
#
# Feel free to modify and distribute this file freely, so long
# as you leave the author's name and URL intact.
#
# Â© Ameir Abdeldayem
# http://www.ameir.net
# Last modified: September 1, 2006
#---------------------------------------------------------------#


DATE=`date +'%m-%d-%Y-%T'`

# check if root, else run as sudo user
function root
{
if [ $(whoami) = "root" ]
then
  echo -n
else
  echo -n "sudo "
fi
}

$(root) apt-get install libpam-ldap libnss-ldap ldap-utils nscd

echo "Backing up and modifying files in pam.d/ ..."
$(root) mv /etc/pam.d/common-account /etc/pam.d/common-account.$DATE.bak
$(root) echo account sufficient      pam_ldap.so >> /etc/pam.d/common-account
$(root) echo account required        pam_unix.so try_first_pass >> /etc/pam.d/common-account

$(root) mv /etc/pam.d/common-auth /etc/pam.d/common-auth.$DATE.bak
$(root) echo auth    sufficient      pam_ldap.so >> /etc/pam.d/common-auth
$(root) echo auth    required        pam_unix.so try_first_pass >> /etc/pam.d/common-auth

$(root) mv /etc/pam.d/common-password /etc/pam.d/common-password.$DATE.bak
$(root) echo password        sufficient      pam_ldap.so >> /etc/pam.d/common-password
$(root) echo password      required   pam_unix.so nullok obscure min=4 max=8 md5 \
try_first_pass >> /etc/pam.d/common-password

# changes in common-session shouldn't be needed, but if so uncomment and (re)run
# $(root) mv /etc/pam.d/common-session /etc/pam.d/common-session.$DATE.bak
# $(root) echo session       sufficient      pam_ldap.so >> /etc/pam.d/common-session
# $(root) echo session       required        pam_unix.so >> /etc/pam.d/common-session

$(root) cp /etc/nsswitch.conf /etc/nsswitch.conf.$DATE.bak
$(root) cp /etc/nsswitch.conf /etc/nsswitch.conf.bak

echo "Editing your nsswitch.conf file..."
$(root) sed -e 's/compat/ldap files/g' /etc/nsswitch.conf.bak > /etc/nsswitch.conf

echo -e "Finished installing packages and modifying configuration files! \n"

echo -e "NOTES:\nYou may need to restart your computer before changes take effect."
echo "You can restart your computer by typing '$(root)reboot' in this very same window."
echo "If you are trying to login as a user that is local AND in LDAP and are getting\
 permission errors, type (write this down) '$(root)nscd --invalidate=passwd' in a terminal."


Download ldapconf.sh

Backup Scripts- README

20 08 2006
If you are using any of the backup scripts on my site, it is useful to know a few things to get them up and running, and to keep existing things on your server sane.

These scripts can be used together; they are friendly to each other and do not mess with one another. It is completely safe (and recommended) to have the same backup directory for all of the scripts.

How to use the scripts:

You can copy the code directly from the site and paste into a plain text editor. Make sure the editor saves files as plain text; i.e. it does no word processing. Save the file and upload it to your server (unless you're working on the server so you won't have to transfer anything), preferably in the root of your home directory (not a big deal, though). It is a good idea to save each of the scripts with similar names like backupmysql.sh, backupldap.sh, etc.

File permissions:
Once the file is on the server, make it executable. If you have ssh or other command-line access, type chmod +x [filename]. If you are using FTP, CHMOD the scripts to make them executable.

Testing the script:
If you have ssh, you can test the script by going to the script's directory and typing ./backupmysql.sh (or whatever the filename is). If the script works with no problems, you can now move forward and create a cron job.

Creating a cron job:
If you are using cPanel, the 'simple' cron creation method is simple enough to run without explanation. Just type the full script's path in the field and select the desired timings.

If you are using the 'advanced' method in cPanel or are entering the cron job using ssh, entering something like 0 0 * ~/backupmysql.sh should do the trick (will run the script every midnight). To edit your cron jobs in ssh, type crontab -e, which will open an editor with your current cron jobs listed. Update that file and save.

If you get the error bad interpreter: No such file or directory, then you probably saved the file in Windows and transferred it over to the server. Windows, Linux, and Macs save files differently, so the shell script can't run unless it is Linux-ready. cPanel is pretty good with this. If you have access to cPanel, all you have to do is open the script in the filemanager and click 'save'. cPanel will save the file in the correct format. If you don't have cPanel access, you can open the file in a text editor in ssh and save it.

[to be finished]

Folder Backup to FTP and Email Shell Script for Cron v2

01 08 2006
CODE:
#! /bin/bash

# Ameir Abdeldayem
# http://www.ameir.net
# You are free to modify and distribute this code,
# so long as you keep my name and URL in it.
# Last modified: August 1, 2006
#-------------------------------------------------------------

# your server's name
SERVER=ameir.net

# directory to backup to
BACKDIR=~/backups

# date format that is appended to filename
DATE=`date +'%m-%d-%Y'`

# directories to backup, separated by a space
# if this script is a level above a directory you want to backup,
# you can simply enter its name, otherwise, type the absolute path, 
# with or without a trailing slash.
DIRS="/home/user/public_html /home/user/directory2"

# set to 'y' if you want to backup all your folders. this will backup
# all files and folders in the script's parent directory
BACKALL=n

#----------------------Mail Settings--------------------#

# set to 'y' if you'd like to be emailed the backup (requires mutt)
MAIL=n

# email addresses to send backups to, separated by a space
EMAILS="user@gmail.com user@inbox.com user@goowy.com"

# email subject
SUBJECT="Directory Backup on $SERVER ($DATE)"

#----------------------FTP Settings--------------------#

# set "FTP=y" if you want to enable FTP backups
FTP=y

# FTP server settings; should be self-explanatory
FTPHOST="ftp.server.com"
FTPUSER="user"
FTPPASS="password"

# directory to backup to. if it doesn't exist, file will be uploaded to 
# first logged-in directory
FTPDIR="backups"

#-------------------Deletion Settings-------------------#

# delete old files?
DELETE=y

# how many days of backups do you want to keep?
DAYS=3

#----------------------End of Settings------------------#
# check of the backup directory exists
# if not, create it
if  [ -e $BACKDIR ]
then
 echo Backups directory already exists
else
mkdir $BACKDIR
fi


# This is a list of folders to be backed up.
# You can add more entries if you want more
# directories to be backed up. The ${PWD##*/}
# from the first entry gets the base name from
# the current directory and uses it in the filename.
# Format: zip -9 -r (where to save) (what to backup).
# Be sure to include $BACKDIR/ in the beginning
# so that the file is saved in the backup directory.

if  [ $BACKALL = "y" ]
then
echo Backing up entire parent directory...
zip -9 -r $BACKDIR/${PWD##*/}-backup-$SERVER-$DATE.zip ./
else
echo Backing up selected directories...
for directory in $DIRS
do
DIR=`echo $directory |  sed -e 's/^\///g'  -e 's/\/$//g' \
-e 's/~//g' -e 's/\.//g' -e 's/home\///g' -e 's/\//_/g'`
echo Backing up folder named $directory as $DIR...
zip -9 -r $BACKDIR/$DIR-backup-$SERVER-$DATE.zip $directory
done
fi

if  [ $MAIL = "y" ]
then
BODY="Your backup is ready! Find more useful scripts and info at http://www.ameir.net"
ATTACH=`for file in $BACKDIR/*$DATE.zip; do echo -n "-a ${file} "; done`

echo $BODY | mutt -s "$SUBJECT" $ATTACH $EMAILS
        
echo "Your backup has been emailed to you!"
fi

if  [ $FTP = "y" ]
then
cd $BACKDIR
ATTACH=`for file in *$DATE.zip; do echo -n -e "put ${file}\n"; done`

ftp -nv <<EOF
open $FTPHOST
user $FTPUSER $FTPPASS
cd $FTPDIR
$ATTACH
quit
EOF
fi

if  [ $DELETE = "y" ]
then
find $BACKDIR -name "*.zip" -mtime $DAYS -exec rm {} \;

if  [ $DAYS = "1" ]
then
echo "Yesterday's backup has been deleted"
else
echo "The backup from $DAYS days ago has been deleted"
fi
fi

echo Your backup is complete!


Download backupdir.sh

MySQL Backup to FTP and Email Shell Script for Cron v2

01 08 2006
Added ability to FTP multiple database backups over to a remote server, as well as with email.
CODE:
#! /bin/bash

# Ameir Abdeldayem
# http://www.ameir.net
# You are free to modify and distribute this code,
# so long as you keep my name and URL in it.
# Last modified: August 1, 2006
#-------------------------------------------------------------

# your MySQL server's name
SERVER=ameir.net

# directory to backup to
BACKDIR=~/backups

# date format that is appended to filename
DATE=`date +'%m-%d-%Y'`

#----------------------MySQL Settings--------------------#

# your MySQL server's location (IP address is best)
HOST=localhost

# MySQL username
USER=username

# MySQL password
PASS=password

# List all of the MySQL databases that you want to backup in here, 
# each seperated by a space
DBS="db1 db2"

# set to 'y' if you want to backup all your databases. this will override
# the database selection above.
DUMPALL=y

# MySQL dump options
OPTIONS=" --quick --add-drop-table --add-locks --extended-insert --lock-tables"



#----------------------Mail Settings--------------------#

# set to 'y' if you'd like to be emailed the backup (requires mutt)
MAIL=y

# email addresses to send backups to, separated by a space
EMAILS="user@gmail.com user@inbox.com user@goowy.com"

SUBJECT="MySQL backup on $SERVER ($DATE)"

#----------------------FTP Settings--------------------#

# set "FTP=y" if you want to enable FTP backups
FTP=y

# FTP server settings; should be self-explanatory
FTPHOST="ftp.server.com"
FTPUSER="user"
FTPPASS="password"

# directory to backup to. if it doesn't exist, file will be uploaded to 
# first logged-in directory
FTPDIR="backups"

#-------------------Deletion Settings-------------------#

# delete old files?
DELETE=y

# how many days of backups do you want to keep?
DAYS=5

#----------------------End of Settings------------------#

# check of the backup directory exists
# if not, create it
if  [ -e $BACKDIR ]
then
echo Backups directory already exists
else
mkdir $BACKDIR
fi

if  [ $DUMPALL = "y" ]
then
echo Dumping all your databases...
mysqldump -h $HOST --user=$USER --password=$PASS $OPTIONS --all-databases > \
$BACKDIR/$SERVER-mysqlbackup-ALL-$DATE.sql
gzip -f -9 $BACKDIR/$SERVER-mysqlbackup-ALL-$DATE.sql
else
echo Backing up MySQL databases...
for database in $DBS
do
mysqldump -h $HOST --user=$USER --password=$PASS $database > \
$BACKDIR/$SERVER-mysqlbackup-$database-$DATE.sql
gzip -f -9 $BACKDIR/$SERVER-mysqlbackup-$database-$DATE.sql
done
fi

# if you have the mail program 'mutt' installed on
# your server, this script will have mutt attach the backup
# and send it to the email addresses in $EMAILS

if  [ $MAIL = "y" ]
then
BODY="Your backup is ready! Find more useful scripts and info at http://www.ameir.net"
ATTACH=`for file in $BACKDIR/*$DATE.sql.gz; do echo -n "-a ${file} ";  done`

echo "$BODY" | mutt -s "$SUBJECT" $ATTACH $EMAILS
        
echo "Your backup has been emailed to you!"
fi

if  [ $FTP = "y" ]
then
cd $BACKDIR
ATTACH=`for file in *$DATE.sql.gz; do echo -n -e "put ${file}\n"; done`

ftp -nv <<EOF
open $FTPHOST
user $FTPUSER $FTPPASS
cd $FTPDIR
$ATTACH
quit
EOF
fi

if  [ $DELETE = "y" ]
then
find $BACKDIR -name "*.sql.gz" -mtime $DAYS -exec rm {} \;

if  [ $DAYS = "1" ]
then
echo "Yesterday's backup has been deleted"
else
echo "The backup from $DAYS days ago has been deleted"
fi
fi

echo Your backup is complete!


Download backupmysql.sh

LDAP Backup to FTP and Email Shell Script for Cron v2

01 08 2006
Added ability to FTP files to another server and to delete old backups from backup directory.

CODE:
#! /bin/bash

# Ameir Abdeldayem
# http://www.ameir.net
# You are free to modify and distribute this code,
# so long as you keep my name and URL in it.
# Last modified: August 1, 2006
#-------------------------------------------------------------

# directory to backup to
BACKDIR=~/backups

# your LDAP server's name
SERVER=ameir.net

# date format that is appended to filename
DATE=`date +'%m-%d-%Y'`


#----------------------Mail Settings--------------------#

# set to 'y' if you'd like to be emailed the backup (requires mutt)
MAIL=y

# email addresses to send backups to, separated by a space
EMAILS="user@gmail.com user@inbox.com user@walla.com user@goowy.com"

# email subject
SUBJECT="LDAP Backup on $SERVER ($DATE)"

#----------------------FTP Settings--------------------#

# set "FTP=y" if you want to enable FTP backups
FTP=y

# FTP server settings; should be self-explanatory
FTPHOST="ftp.server.com"
FTPUSER="user"
FTPPASS="password"

# directory to backup to. if it doesn't exist, file will be uploaded to 
# first logged-in directory
FTPDIR="backups"

#-------------------Deletion Settings-------------------#

# delete old files?
DELETE=y

# how many days of backups do you want to keep?
DAYS=5

#----------------------End of Settings------------------#

# make sure script is run as root
if [ $(whoami) != "root" ]
then
  echo "You must be root to run this script."
  exit 1
fi

# check of the backup directory exists
# if not, create it
if  [ -e $BACKDIR ]
then
 echo Backups directory already exists
else
mkdir $BACKDIR
fi

echo Backing up LDAP entries...

slapcat -l $BACKDIR/$SERVER-ldapbackup-$DATE.ldif

if  [ $MAIL = "y" ]
then
BODY="Your backup is ready! Find more useful scripts and info at http://www.ameir.net"
ATTACH=`for file in $BACKDIR/*$DATE.ldif; do echo -n "-a ${file} ";  done`

echo "$BODY" | mutt -s "$SUBJECT" $ATTACH $EMAILS
        
echo "Your backup has been emailed to you!"
fi

if  [ $FTP = "y" ]
then
cd $BACKDIR
ATTACH=`for file in *$DATE.ldif; do echo -n -e "put ${file}\n"; done`

ftp -nv <<EOF
open $FTPHOST
user $FTPUSER $FTPPASS
cd $FTPDIR
$ATTACH
quit
EOF
fi

if  [ $DELETE = "y" ]
then
find $BACKDIR -name "*.ldif" -mtime $DAYS -exec rm {} \;

if  [ $DAYS = "1" ]
then
echo "Yesterday's backup has been deleted"
else
echo "The backup from $DAYS days ago has been deleted"
fi
fi

echo Your backup is complete!


Download backupldap.sh

Updated Backup Scripts

31 07 2006
I have updated the MySQL, LDAP, and directory backup scripts and added more features. All of the scripts are able to email the backups, FTP them over to another server to a specified directory, and are able to delete old backups from the backup directory on the server (you can choose how many days of backups you want to keep). All of the scripts are now also more customizable, where email addresses and other options are not hardcoded, but put into variable format for easy editing. A couple of small bugs were also fixed, such as the emailing of multiple file attachments.

Backing up MySQL databases easily with cron

26 07 2006
CODE:
#! /bin/bash

# Ameir Abdeldayem
# http://www.ameir.net
# You are free to modify and distribute this code,
# so long as you keep my name and URL in it.

# your MySQL server's name
SERVER=abubakr

# your MySQL server's location (IP address is best)
HOST=localhost

# MySQL username
USER=root

# MySQL password
PASS=

# List all of the MySQL databases that you want to backup in here, 
# each seperated by a space
DBS="ameir"

# set to 'y' if you want to backup all your databases. this will override
# the database selection above.
DUMPALL=n

# MySQL dump options
OPTIONS=" --quick --add-drop-table --add-locks --extended-insert --lock-tables"

# directory to backup to
BACKDIR=~/backups

# date format that is appended to filename
DATE=`date +'%m-%d-%Y'`

# set to 'y' if you'd like to be emailed the backup (requires mutt)
MAIL=y

# email addresses to send backups to, separated by a space
EMAILS="email@gmail.com email@inbox.com email@walla.com email@goowy.com"

SUBJECT="MySQL backup on $SERVER ($DATE)"

#--------------------------------------------------------------


# check of the backup directory exists
# if not, create it
if  [ -e $BACKDIR ]
then
 echo Backups directory already exists
else
mkdir $BACKDIR
fi



if  [ $DUMPALL = "y" ]
then
echo Dumping all your databases...
mysqldump -h $HOST --user=$USER --password=$PASS $OPTIONS --all-databases > \
$BACKDIR/$SERVER-mysqlbackup-ALL-$DATE.sql
gzip -f -9 $BACKDIR/$SERVER-mysqlbackup-ALL-$DATE.sql
else
echo Backing up MySQL databases...
for database in $DBS
do
mysqldump -h $HOST --user=$USER --password=$PASS $database > \
$BACKDIR/$SERVER-mysqlbackup-$database-$DATE.sql
gzip -f -9 $BACKDIR/$SERVER-mysqlbackup-$database-$DATE.sql
done
fi


# if you have the mail program 'mutt' installed on
# your server, this script will have mutt attach the backup
# and send it to the email addresses in $EMAILS
BODY="Your backup is ready! Find more useful scripts and info at http://www.ameir.net"
if  [ $MAIL = "y" ]
then
echo "$BODY" | mutt -s "$SUBJECT" \
-a $BACKDIR/*$DATE.sql.gz $EMAILS
        
echo "Your backup has been emailed to you!"
fi

echo Your backup is complete!

Cron LDAP Backup to File and Email

21 07 2006
If you keep up with this site, you'll know that I lost my entire LDAP database once due to server errors from a power outage. I made this script below to counter that. This script must be run as root because the command "slapcat" won't work otherwise (AFAIK). I created a cron job to run this script every night and email me the ldif dump. This is where your unused Gmail account comes into play.


CODE:
#! /bin/bash

# Ameir Abdeldayem
# http://www.ameir.net
# You are free to modify and distribute this code,
# so long as you keep my name and URL in it.

# directory to backup to
BACKDIR=~/backups

# your LDAP server's name
SERVER=abubakr

# date format that is appended to filename
DATE=`date +'%m-%d-%Y'`

# check of the backup directory exists
# if not, create it
if  [ -e $BACKDIR ]
then
 echo Backups directory already exists
else
mkdir $BACKDIR
fi


echo Backing up LDAP entries...

slapcat -l $BACKDIR/$SERVER-ldapbackup-$DATE.ldif

# if you have the mail program 'mutt' installed on
# your server, uncomment this line to have the ldif
# dump emailed to you as an attachment.  You can add
# multiple email addresses at the end of the line to
# send this file to each of them.
echo Your backup is ready | mutt -a $BACKDIR/*$DATE.ldif \
        -s "LDAP backup on $SERVER ($DATE)" email1@server.com \
        email2@server.com

echo Your backup is complete!