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!

My Favorite Email Services

21 07 2006

(to be updated)













NameSpaceAttachment SizePOP/IMAP/SMTPComments
www.inbox.com5GB20MBYes/No/YesVery nice interface, strong filtering ability, fast navigation, yet slow to compose messages.
www.gawab.com2GB50MBYes/No/YesComments
www.lycos.com3GBUnlimitedNo/No/NoComments
www.aim.com2GB50MBYes/Yes/YesComments
www.walla.com5GB10MBNo/No/NoComments
www.goowy.com2GB50MBNo/No/NoComments
www.30gigs.com30GB50MBNo/No/NoComments
www.gmail.com2.8GB20MBYes/No/YesComments
www.jlmail.comUnlimited?MBYes/Yes/YesComments
www.icmail.net1GB10MBYes/Yes/YesComments


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!

Good Linux FTP Client

20 07 2006
I have forever been searching for a good Linux FTP client. I have been using GFTP for a while now, but get fed up with it each time. It's buggy, doesn't have all the features I'd like, and the interface isn't so great. I heard of Kasablanca and tried that out...it sucked. It looks nice, but that's about all. I couldn't even figure out how to CHMOD files. It seems very simplistic and only cares about uploading files. I remember using KBear back in the day, but for some reason I wasn't happy with it either. So I've been using GFTP all this time when using Linux (it can't compare to SmartFTP in Windows), at least glad that it supports SFTP as well (it can't compare to WinSCP either).

Well today while browsing the web, I stumbled upon a very useful web page. This is exactly what I was looking for. Based on looks and feature lists only, I'd say that FTPCube, KFTPGrabber, Scythia, and JFTP look quite promising. I think I'll be trying KFTPGrabber first.

You be the judge.

EDIT: Another useful link: http://www.kde-apps.org/?xcontentmode=236

Backing up MySQL Databases automatically

19 07 2006
The best MySQL backup system I have seen to date is phpMyBackupPro. It does pretty much EVERYTHING. I have it set up to backup my DBs to a local directory on this webserver, email the SQL dump to my Gmail account, and FTP the file over to another server. I think that's pretty redundant.

Later, I'll share with you how I installed it on my server.

Easy way to backup entire folders

18 07 2006
I learned the hard way that backing up files is VERY important. This blog was deleted at one point due to a misunderstanding with the webhost, and guess what? I didn't have a backup. I had to start from scratch, which was not cool. I made this script so I can backup directories on my webserver, but I, of course, am going to share it with you.

You can change the code around to use other compression technologies besides zip quite easily. You might only want to back up your public_html folder and not your entire home directory. Keep in mind that your backup can be large, and may cause you to fill up your quota quickly, so be sure to delete any old or unneeded backups.

To create this backup process as a cron job in cPanel:
1. Save this script as backupdir.sh
2. Upload it to your home folder (not in public_html)
3. In the Cron Jobs (simple) section of cPanel, type ~/backupdir.sh in the box, and set the backup time as you please.

These instructions are easily adjustable to other servers, so long as there is some way to run commands.

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

# 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 files...

# 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.

zip -9 -r $BACKDIR/${PWD##*/}-backup-$DATE.zip ./

My Server Died :(

17 07 2006

Last week we had some really bad thunderstorms in our area. This led to power interruptions everywhere, including my house. The thing that sucks is that it kept messing with me. During thunderstorms, I usually don't bother to turn my computers off, because from experience, I've had no problems. I know many people think that it's very stupid of me to keep them on, but my computers have always come back to life when power is restored. Last week, this wasn't exactly the case. The only computer that got hurt during this storm was basically the most important computer in the house. I've been working on it for a year or so, gradually configuring some things. I had it perfectly setup for LDAP authentication from other clients, NFS, Samba using LDAP, network printing, Ravencore (a cool webhosting control panel), and a bunch of other things. All of that took a good bit of time. The thing that sucked about the storm is that power would go out...then on...then out...then on. My brothers depend on that computer for LDAP authentication and NFS for the computers in their rooms, so the computer has to stay on. Each time the power would go out, I'd turn that server back on when power was available. At the end of all that mess, the computer started acting funny. SSH no longer worked, the command "shutdown -h now" or "reboot" did nothing, and other weird things kept happening. To "fix" this, I decided to try fsck. A few things were apparently fixed, but this didn't solve any of my issues. I did a hard reboot and the problems persisted. My Debian machine has a grub entry for "recovery mode." I entered this mode and then tried fsck--it didn't look good. I got errors all over the place. I knew it wasn't a good sign, and I pretty much knew from then on that if I finished with fsck, my hard drive would be finished as well. Something made me continue on. After a million or so entries (I had to put a mass on the 'y' key), it was done. The ext3 filesystem was turned into ext2 because the journal was deleted. I tried a reboot, and that's all I got. Grub wouldn't load and everything from there on out is a mess. I have since formatted that hard drive and installed a fresh new copy of Debian Sarge. I'll have to spend countless hours bringing it back to what it was before. Luckily, no user folders were damaged because they were on a separate partition. I wonder if these fsck errors were because the root partition was mounted, but if so, why did fsck not produce so many errors when not in "recovery mode"?


Moral of the story: get a UPS and keep regular backups.

p.s. By my server "dying," it really didn't die. Only the data on the hard disk was hurt. The computer's hardware seems perfectly fine.

LDAP Authentication PAM/NSS Using Debian or Ubuntu Bash Script

17 07 2006

UPDATE: There is a new version of this script here



I had server issues which caused me to lose my database, so this script and howto will be put back up as soon as I can. Stay updated.

EDIT: Here's the script. I'll comment sometime soon.

CODE:
#! /bin/bash
# script to install LDAP authentication for Debian-based systems
# feel free to modify and distribute this file freely, so long
# as you leave the author's name and URL intact.
#
# (c) Ameir Abdeldayem
# http://www.ameir.net
########################################


a=$(date +%m%d%Y-%T)

sudo apt-get install libpam-ldap libnss-ldap

sudo mv /etc/pam.d/common-account /etc/pam.d/common-account.$a.bak
sudo mv /etc/pam.d/common-auth /etc/pam.d/common-auth.$a.bak
sudo mv /etc/pam.d/common-password /etc/pam.d/common-password.$a.bak
sudo mv /etc/pam.d/common-session /etc/pam.d/common-session.$a.bak
sudo mv /etc/nsswitch.conf /etc/nsswitch.conf.$a.bak

sudo echo account sufficient      pam_ldap.so >> /etc/pam.d/common-account
sudo echo account required        pam_unix.so \ 
try_first_pass >> /etc/pam.d/common-account

sudo echo auth    sufficient      pam_ldap.so >> /etc/pam.d/common-auth
sudo echo auth    required        pam_unix.so nullok_secure \
use_first_pass >> /etc/pam.d/common-auth

sudo echo password        sufficient       \ 
pam_ldap.so >> /etc/pam.d/common-password
sudo echo password      required   pam_unix.so \ 
nullok obscure md5 use_first_pass >> /etc/pam.d/common-password

sudo echo session       sufficient     \
 pam_ldap.so >> /etc/pam.d/common-session
sudo echo session       required        \
pam_unix.so >> /etc/pam.d/common-session

# downloads nsswitch.conf to proper directory
# (currently down)
sudo wget http://terpspot.com/nsswitch.conf -P /etc/

Website Back Up

11 07 2006

My host all of a sudden deleted my account "because of mp3 files" which I apparently had. I uploaded a 40kb ringtone to test a script, and my account gets deleted. Lesson learned: always make backups! I had just moved to this host so I didn't expect it to kill me so soon; I thought I had enough time before worrying about backups. Guess I was wrong. I'll be posting some stuff up as time passes on some things I use to make backups.