msgbartop
A chronological documentation test project, nothing serious, really!
msgbarbottom

04 Jun 2007 Shell script to backup mySQL database

Shell script to backup MySql database

mysql-backup.bash

#!/bin/bash
# Shell script to backup MySql database
# To backup Nysql databases file to /backup dir and later pick up by your
# script. You can skip few databases from backup too.
# For more info please see (Installation info):
# http://www.cyberciti.biz/nixcraft/vivek/blogger/2005/01/mysql-backup-script.html
# Last updated: Aug - 2005
# --------------------------------------------------------------------
# This is a free shell script under GNU GPL version 2.0 or above
# Copyright (C) 2004, 2005 nixCraft project
# Feedback/comment/suggestions : http://cyberciti.biz/fb/
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------

MyUSER="SET-MYSQL-USER-NAME"     # USERNAME
MyPASS="SET-PASSWORD"       # PASSWORD
MyHOST="localhost"          # Hostname

# Linux bin paths, change this if it can't be autodetected via which command
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
CHOWN="$(which chown)"
CHMOD="$(which chmod)"
GZIP="$(which gzip)"

# Backup Dest directory, change this if you have someother location
DEST="/backup"

# Main directory where backup will be stored
MBD="$DEST/mysql"

# Get hostname
HOST="$(hostname)"

# Get data in dd-mm-yyyy format
NOW="$(date +"%d-%m-%Y")"

# File to store current backup file
FILE=""
# Store list of databases
DBS=""

# DO NOT BACKUP these databases
IGGY="test"

[ ! -d $MBD ] && mkdir -p $MBD || :

# Only root can access it!
$CHOWN 0.0 -R $DEST
$CHMOD 0600 $DEST

# Get all database list first
DBS="$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -Bse 'show databases')"

for db in $DBS
do
    skipdb=-1
    if [ "$IGGY" != "" ];
    then
	for i in $IGGY
	do
	    [ "$db" == "$i" ] && skipdb=1 || :
	done
    fi

    if [ "$skipdb" == "-1" ] ; then
	FILE="$MBD/$db.$HOST.$NOW.gz"
	# do all inone job in pipe,
	# connect to mysql using mysqldump for select mysql database
	# and pipe it out to gz file in backup dir :)
        $MYSQLDUMP -u $MyUSER -h $MyHOST -p$MyPASS $db | $GZIP -9 > $FILE
    fi
done

Tags: , , , ,

Posted by

15 May 2007 Shell scripts for archiving digital photos in directories by month

This is my version of the shell script “Shell scripts for archiving digital photos in directories by date“. It utilizes  the exiftags command available in most linux distributions like Gentoo and debian Linux. Though the metacam program is also usefull since it can read Nikon NEF-files.Here’s an example of a directory tree they create:

2006  2006-10       IMG_48324.JPG       IMG_48325.JPG       IMG_48326.JPG       IMG_48331.JPG       IMG_48333.JPG       IMG_48334.JPG       IMG_48337.JPG  2006-11       IMG_48338.JPG  ...etc...

move-digiphotosThis bash script (move-digifotos) scans EXIF tags from .JPG files in current directory with metacam, creates necessary directories under $BASEDIR and moves the files in them:

#!/bin/bash# Reads EXIF creation date from all .JPG files in the# current directory and moves them carefully under##   $BASEDIR/YYYY/YYYY-MM/YYYY-MM-DD/## ...where 'carefully' means that it does not overwrite# differing files if they already exist and will not delete# the original file if copying fails for some reason.## It DOES overwrite identical files in the destination directory# with the ones in current, however.## This script was originally written and put into# Public Domain by Jarno Elonen ; in June 2003.# Feel free to do whatever you like with it.BASEDIR=/home/backup/testfind -maxdepth 1 -name "*.JPG" | while read x; doDATE=`exiftags "$x" | \   egrep "^[ t]*Image Created:" | \   sed -r "s/Image Created: ([0-9:]*).*/\1/"`if [ ! -z "$DATE" ];then    YEAR=`echo $DATE | sed -r "s/([0-9]*):([0-9]*):([0-9]*)/\\1/"`    MONTH=`echo $DATE | sed -r "s/([0-9]*):([0-9]*):([0-9]*)/\\2/"`    if [ "$YEAR" -gt 0 ] & [ "$MONTH" -gt 0 ]    then    INSTDIR=${BASEDIR}/${YEAR}/${YEAR}-${MONTH}    install -d "$INSTDIR"    INSTFILE="$INSTDIR/$x"    if [ -e "$INSTFILE" ] && ! cmp -s "$x" "$INSTFILE"    then        echo "WARNING: '$INSTFILE' exists already and is different from '$x'."    else        echo "Moving '$x'"        cp -ax "$x" "$INSTFILE"        if ! cmp -s "$x" "$INSTFILE"        then        echo "WARNING: copying failed somehow, will not delete original '$x'"        else        rm -f "$x"        fi    fi    else    echo "WARNING: '$x' doesn't contain date."    fielse    echo "WARNING: '$x' doesn't contain date."fidone

Tags: , , , , , , , , , ,

Posted by

10 Apr 2007 Recover a dead hard drive using dd

The Unix program dd is a disk copying util that you can use at the command line in order to make a disk image. It makes a bit-by-bit copy of the drive it’s copying, caring nothing about filesystem type, files, or anything else. It’s a great way to workaround the need for Norton Ghost.

Normally, in order to make a disk image, the disk you’re copying from has to be able to spin up and talk — in other words, it’s OK to make a copy if the disk is healthy. But what happens when your disk is becoming a doorstop? As long as it continues to spin, even with physical damage on the drive, dd will get you out of the fire.

A friend sent a disk to me that had hard physical errors on it. It would boot in Windows, but then it would hit one of these scratch marks and just die. We fired up dd, and it started OK, but stopped at the same physical error location — complaining about a Hard Error.

So the workaround was to designate the dd mode as noerror — which just slides over the hard stops, and to add the mode sync, which fills the image with nulls at that point.

dd bs=512 if=/dev/rXX# of=/some_dir/foo.dmg conv=noerror,sync

The bs=512 designates block size, and the if=/dev/rXX# is the UNIX path to the actual disk device. Make sure that the chosen directory (some_dir) has enough room to take the entire disk image — which will be equal to the size of the drive. Since dd doesn’t care about the contents of the drive, it copies every bit on the thing, so you get an image equal to the disk’s capacity. A really big file. One workaround is to put it on a RAID array.

Once you’ve established the disk image (in this example, foo.dmg), you’re almost home. Here’s where your Linux box is far and away the best thing to have. In this example, the dd output file is foo.dmg. You have to realize that this is an exact copy of a busted drive, but the “holes” are filled with nulls. As long as the damage isn’t to the boot sector, though, when you double-click on it, Linux mounts it without breathing hard … who cares if it’s FAT32, NTFS, whatever.

Due to the size of the image that we were copying, we put it on a RAID array, and had to access the image over the network — it still mounted fine. In straight UNIX, if you try to mount a disk image, it complains that there is “no block device” and fails. Once your image is mounted, it’s easy work to retrieve the critical files from the image — usually things like .doc files and .xls files and the lot.

Finally, since your disk is actually dying, once you have your image, you can drop it to tape or something and you’ve not only recovered your files, you’ve made a viable backup as well. Once again, that which destroys a Windows box becomes a play thing to a Linux box.

Tags: ,

Posted by

10 Apr 2007 rsync backup on a local pc

#!/bin/bash
echo "Starting Daily rsync backup on `date`"

for FSYS in /home/username/rsync
do
  /usr/bin/rsync -aE --delete $FSYS /home/username/rsync_bck
  if [ $? != 0 ]; then
    echo " ==> Error during rsync of $FSYS"
  else
    echo "rsync of $FSYS OK on `date`"
  fi
done

echo "Finished Daily rsync backup at `date`"

Tags: ,

Posted by