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!

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

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/