This is a sample BASH script I made for work. We have a Linux
server running mySQL and Apache, every night the script copies
the database from a remote server to our local backup server
and rebuilds the database. The trouble I ran into was that
the database was almost 2 gigabytes and had to be broken down
into pieces, the perl script we used to do this had a habit of
naming the files without leading zeroes which was necessary for
sequential restoration of the database. A second issue issue
was the fact that the databases on the two servers had two
different database prefixes (matrixdo_blah to officeg_blah).
#!/bin/bash
set -e
SEQ=/usr/bin/seq
LOGFILE=/home/officeg/myscripts/logs/$(date +%m-%d-%y)
BCKFOLDER=/home/officeg/myscripts/backups
cd /home/officeg/myscripts
#CHECK IF LOGFILE FOR THE DAY EXISTS, IF NOT, CREATE ONE
if [ -e $LOGFILE ]; then
echo "******** NIGHTLYMOVE AND REBUILD RESTARTED AT `date +%H:%M` *********">>$LOGFILE
echo>>$LOGFILE
echo>>$LOGFILE
else
touch /home/officeg/myscripts/logs/$(date +%m-%d-%y)
echo ------------Database Rebuild started on `date +%m-%d-%y--%H:%M`------------>>$LOGFILE
fi
#THIS MOVES AND UNZIPS THE FILES FROM THE FTP FOLDER TO THE MSD BACKUP FOLDER
cp $BCKFOLDER/* /home/officeg/myscripts/backuparchive
gunzip $BCKFOLDER/*
#THIS LINE CREATES A FILE WITH THE SQL FILE NAMES IN IT
ls $BCKFOLDER > sortit
#THIS LINE REPLACES THE MATRIXDO PART WITH OFFICEG
sed s/matrixdo/officeg/
echo>>$LOGFILE
#THIS IS THE ORIGINAL FILE LISTING
origin=($(cat sortit))
#THIS IS THE RENAMED FILE LISTING
sortarray=($(cat sortbck))
#THIS ARRAY IS TRIMMED FOR NUMBERS
workarray=(${origin[@]})
if [ "$?" -eq 0 ]; then
echo Arrays have been created >> $LOGFILE
echo>>$LOGFILE
fi
cd $BCKFOLDER
for i in $($SEQ 0 $((${#sortarray[*]} - 1)))
do
workarray[$i]=${sortarray[$i]%%.*}
workarray[$i]=${workarray[$i]#*part_}
if [ "${workarray[$i]}" -lt 100 ]
then
sortarray[$i]=${sortarray[$i]/part_/part_0}
if [ "${workarray[$i]}" -lt 10 ]
then
sortarray[$i]=${sortarray[$i]/part_/part_0}
fi
fi
mv ${origin[$i]} ${sortarray[$i]}
done
if [ "$?" -eq 0 ]; then
echo Files have been correctly sorted and renamed >> $LOGFILE
echo >> $LOGFILE
fi
-This looks ugly (note: need to fix this up)