#!/bin/bash ######################################## # Ryan Pisani mythdora@thepisanis.com # # February 9th, 2007 # # Desc: DB backup tool for mythdora # ######################################## VERSION="1.0" DATE="February 09th, 2007" AUTHOR="Ryan Pisani " PATH=/bin:/usr/bin:/usr/sbin:/usr/share/mythdora:$PATH COMM=`basename $0` BACKUPDIR="/storage/dorabackup" #Global restore point #Displays version/author versioninfo () { echo "$COMM" echo "Author: $AUTHOR" echo "Version: $VERSION" echo "Date: $DATE" sleep 2 } #Sleep timer sleeptimer() { SLEEP="$1" while [[ $SLEEP > 0 ]]; do sleep 1 echo -e ".$SLEEP\b\b\c" let SLEEP="$SLEEP - 1" done } #End of sleeptimer #Dumps the database to the default dir or to a user specified dir dumpdb() { #Check for custom backup type first if [[ $1 == "custom" ]]; then BACKUPDIR= read -a BACKUPDIR -p "Enter a path to backup the mythconverg database to: " if [[ -z "$BACKUPDIR" ]]; then echo -e "No input given. Setting default backup directory" BACKUPDIR="/storage/dorabackup" sleep 2 fi fi #Check to see if the directory exists if [[ ! -d "$BACKUPDIR" ]]; then mkdir -p $BACKUPDIR fi #Check again for directory existance, and error out or continue if [[ ! -d "$BACKUPDIR" ]]; then echo -e "$COMM: ERROR" echo -e "The backup directory doesn't exist and/or could not be created" echo -e "Ensure that user $USER has write permissions on `dirname $BACKUPDIR`" echo -e "This program will exit in 30 seconds." sleeptimer 30 exit 1 else #Let's check for an existing DB backup. If one exists, we'll rename it #and make a new one. We'll keep 2 backup copies. if [[ -e "$BACKUPDIR/mythconverg.sql.gz.1" ]]; then mv "$BACKUPDIR/mythconverg.sql.gz.1" "$BACKUPDIR/mythconverg.sql.gz.2" fi if [[ -e "$BACKUPDIR/mythconverg.sql.gz" ]]; then mv "$BACKUPDIR/mythconverg.sql.gz" "$BACKUPDIR/mythconverg.sql.gz.1" fi echo -e "\n\nBeginning dump of mythconverg.. this could take a few seconds.\n\n" sleep 3 #Dump the database out to a file mysqldump --lock-tables -u root mythconverg >$BACKUPDIR/mythconverg.sql if [[ $? -ne "0" ]]; then echo -e "$COMM: ERROR" echo -e "Something failed in the mysqldump command" echo -e "Verify the integrity of $BACKUPDIR/mythconverg.sql" echo -e "This program will exit in 30 seconds." sleeptimer 30 exit 1 fi #Now gzip the sql db to save space gzip $BACKUPDIR/mythconverg.sql if [[ $? -eq "0" ]]; then echo -e "$COMM: Database Dump Successful" echo -e "Your database backup is located in $BACKUPDIR" sleeptimer 20 exit 0 fi fi } #End of dumpdb #Restores DB from recent backup in /storage/dorabackup restoredb() { #First check to see if it's custom type if [[ $1 == "custom" ]]; then BACKUPDIR= read -a BACKUPDIR -p "Enter the path that contains mythconverg.sql.gz : " if [[ -z $BACKUPDIR ]]; then echo -e "No input given. Setting default backup directory" BACKUPDIR="/storage/dorabackup" sleep 2 fi fi #Now check to see if dir & sql backup exist if [[ -e "$BACKUPDIR/mythconverg.sql.gz" ]]; then echo -e "$COMM: WARNING.. WARNING.. WARNING.." echo -e "This process is destructive. It will take the latest copy of your" echo -e "mythconverg database and restore it over the active one." echo -e "Hopefully you meant to do this. You have 60 seconds to CTRL-C" echo -e "this terminal." sleeptimer 60 #Now let's do the restore #First let's decompress it, or we exit and don't continue gunzip $BACKUPDIR/mythconverg.sql.gz if [[ $? -ne "0" ]]; then echo -e "$COMM: ERROR" echo -e "The gunzip of $BACKUPDIR/mythconverg.sql.gz failed. This is very bad" echo -e "and we cannot continue with the restore. The script will exit in 30" echo -e "seconds" sleeptimer 30 fi #We're going to make the assumption that the DB exists, and it's being #removed. If it doesn't exist, then it'll be created. if !(echo create database mythconverg | mysql); then #Let's remove it if we see error from mysql echo drop database mythconverg | mysql #Let's readd it echo create database mythconverg | mysql fi #Now let's do the restore mysql -uroot mythconverg <$BACKUPDIR/mythconverg.sql if [[ $? -ne "0" ]]; then echo -e "$COMM: ERROR" echo -e "We caught an error somewhere in the restore process. Hopefully it is" echo -e "no big deal, but we can't be sure. Run a mysqlcheck on mythconverg" echo -e "to verify the integrity of your database. This program will exit in" echo -e "30 seconds." sleeptimer 30 else echo -e "$COMM: Restore Complete" echo -e "The database retore has been completed. It is recommended that you run" echo -e "a mysqlcheck on mythconverg before continuing. Good Luck!" echo -e "This program will exit in 30 seconds." sleeptimer 30 fi fi } #End of restoredb #Burns mythconverg backup to a specified media burndb() { #First check to see if it's custom type if [[ $1 == "custom" ]]; then BACKUPDIR= read -a BACKUPDIR -p "Enter the path that contains mythconverg.sql.gz : " if [[ -z $BACKUPDIR ]]; then echo -e "No input given. Setting default backup directory" BACKUPDIR="/storage/dorabackup" sleep 2 fi fi #Let's make sure there is a cd writer on the machine DRIVES=(`ls /dev/ | grep writer`) #Let's check for media first, if none found we'll use the first writer for i in ${DRIVES[@]}; do if (/usr/share/mythdora/cdrom-test /dev/$i| grep -q 'Disc present'); then TARGET="/dev/$i" break fi done #If a drive hasn't been assigned from media checker then we just select first #drive and eject it waiting for media if [[ -z $TARGET ]]; then TARGET="/dev/${DRIVES[1]}" echo -e "There was no media detected in the drive. Please put an empty" echo -e "disc in the tray when the drive opens. You have 30 seconds " echo -e "to load the disc." #Now eject and wait for media eject -t $TARGET sleep 30 fi #Now verify they media is present and continue if (/usr/share/cdrom-test $TARGET | grep -q 'Present'); then cdrecord dev=$TARGET $BACKUPDIR else echo -e "$COMM: ERROR" echo -e "Could not detect blank media in $TARGET" echo -e "Try again and load media when prompted" sleeptimer 20 exit 1 fi } case $1 in -dump) dumpdb $2 ;; -restore) restoredb $2 ;; -burn) burndb $2 ;; -v) versioninfo ;; *) echo -e "Usage: $COMM [-dump || -restore || -burn]\n" echo -e " $COMM -dump" echo -e " Backup mythconverg to default directory\n" echo -e " $COMM -dump custom" echo -e " Backup mythconverg to user specified directory\n" echo -e " $COMM -burn" echo -e " Burn mythconverg to cd from default directory\n" echo -e " $COMM -burn custom" echo -e " Burn mythconverg to cd from user specified directory\n" echo -e " $COMM -restore" echo -e " Restore mythconverg from default copy\n" echo -e " $COMM -restore custom" echo -e " Restore mythconverg from user specified directory\n" echo -e " $COMM -v" echo -e " Display version & author information" ;; esac