Revision: 49063
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at July 15, 2011 02:19 by eristoddle
Initial Code
#!/bin/bash
#
# Version 0.04 - JF Nutbroek 2008
#
# Various Paths
SCRIPT="/root/tools"
HTFOLDER="/opt/lampp/htdocs"
MYSQL="/opt/lampp/bin/mysql"
MYSQLDUMP="/opt/lampp/bin/mysqldump"
BACKUPLOC="/backups"
# temporary file
TEMP=/tmp/answer$$
# set default values
MIRROR="" ; USERNAME="root" ; PASSWORD=""
EXCLUDEDB="" ; EXCLUDEHTDOCS="" ; FTP="yes" ; FTPDUMP="no"
FTPUSERNAME="anonymous" ; FTPPASSWORD="root@localhost" ; FTPIP=""
FTPDIR="/backups" ; LASTWEEK="yes" ; SAVE="no" ; ONEDAY="no"
# clean up and exit
clean_up() {
setterm -background black
clear
rm -f $TEMP >/dev/null 2>&1
exit
}
# Note: The functions startftp, stopftp, enable and disable
# need to be modified according to the Linux system in use
# Code starts here
if [ -f "$SCRIPT/.backup" ]; then
. $SCRIPT/.backup
fi
startftp() {
SERVICE="vsftpd"
RUNNING=`ps ax | grep -v grep | grep $SERVICE`
if [ "$RUNNING" = "" ]; then
sh /etc/rc.d/rc.vsftpd start >/dev/null 2>&1
fi
}
stopftp() {
SERVICE="vsftpd"
RUNNING=`ps ax | grep -v grep | grep $SERVICE`
if [ ! "$RUNNING" = "" ]; then
sh /etc/rc.d/rc.vsftpd stop >/dev/null 2>&1
fi
}
disable() {
if [ -f "/etc/cron.daily/startbackup.sh" ]; then
rm /etc/cron.daily/startbackup.sh
fi
echo "Backup has been disabled"
}
enable() {
echo "#!/bin/bash" > /etc/cron.daily/startbackup.sh
echo "sh $SCRIPT/backup.sh start" >> /etc/cron.daily/startbackup.sh
chmod 0755 /etc/cron.daily/startbackup.sh
echo "Backup has been enabled"
}
check() {
OUT=$?
if [ ! $OUT -eq 0 ]; then
echo "Error! - Please check your configuration"
exit 2
fi
}
start() {
# Get todays date
DAYOFWEEK=`date +%a`
TODAY=`date +%F%t%R`
# Check if configurationfile is available
if [ ! -f "$SCRIPT/.backup" ]; then
echo "Please configure the backup first!"
exit 2
fi
# Stop FTP service
if [ "$FTP" = "yes" ]; then
stopftp
fi
# Create required directories
if [ ! -d $BACKUPLOC ]; then
mkdir $BACKUPLOC
fi
cd $BACKUPLOC
check
if [ "$ONEDAY" = "yes" ]; then
rm -r * >/dev/null 2>&1
fi
if [ ! -d daily ]; then
mkdir daily
check
fi
if [ "$LASTWEEK" = "yes" ]; then
if [ ! -d lastweek ]; then
mkdir lastweek
check
fi
fi
cd daily
check
if [ ! -d $DAYOFWEEK ]; then
mkdir $DAYOFWEEK
check
fi
# Redirect I/O
if [ -f $BACKUPLOC/daily/$DAYOFWEEK/.logfile ]; then
rm $BACKUPLOC/daily/$DAYOFWEEK/.logfile
else
touch $BACKUPLOC/daily/$DAYOFWEEK/.logfile
fi
exec 6>&1
exec > $BACKUPLOC/daily/$DAYOFWEEK/.logfile
exec 7>&2
exec 2> $BACKUPLOC/daily/$DAYOFWEEK/.logfile
# Start backup of HTDOCS directory
cd $HTFOLDER
check
HTDOCSNAMES="`find -type d -maxdepth 1 | sed 's/\.//g;' | sed 's/\///g;' | sed -e '1,1d'`"
for exclude in $EXCLUDEHTDOCS
do
HTDOCSNAMES=`echo $HTDOCSNAMES | sed "s/\b$exclude\b//g"`
done
echo "$TODAY" > $BACKUPLOC/daily/$DAYOFWEEK/.mailmessage
echo "" >> $BACKUPLOC/daily/$DAYOFWEEK/.mailmessage
rm $BACKUPLOC/daily/$DAYOFWEEK/htdocs_*.tar.gz >/dev/null 2>&1
for include in $HTDOCSNAMES
do
HT="`echo $include | sed 's/%/ /g'`"
tar cfz htdocs_${HT}_$DAYOFWEEK.tar.gz $HT
mv htdocs_${HT}_$DAYOFWEEK.tar.gz $BACKUPLOC/daily/$DAYOFWEEK/
done
# Backup all htdocs root files
tar cfz htdocs_rootfiles_$DAYOFWEEK.tar.gz *.*
mv htdocs_rootfiles_$DAYOFWEEK.tar.gz $BACKUPLOC/daily/$DAYOFWEEK/
# Start backup of MySQL directory
cd $BACKUPLOC/daily/$DAYOFWEEK/
DBNAMES="`$MYSQL --user=$USERNAME --password=$PASSWORD --host=localhost --batch --skip-column-names -e "show databases" | sed 's/ /%/g'`"
for exclude in $EXCLUDEDB
do
DBNAMES=`echo $DBNAMES | sed "s/\b$exclude\b//g"`
done
rm mysql_*.gz >/dev/null 2>&1
for include in $DBNAMES
do
DB="`echo $include | sed 's/%/ /g'`"
$MYSQLDUMP --user=$USERNAME --password=$PASSWORD --host=localhost --quote-names --opt $DB > mysql_${DB}_$DAYOFWEEK.sql
gzip -f mysql_${DB}_$DAYOFWEEK.sql
done
# Start FTP
if [ "$FTP" = "yes" ]; then
startftp
fi
echo "Created the following archives:" >> .mailmessage
echo "" >> .mailmessage
du -hl *.gz >> .mailmessage
echo "" >> .mailmessage
# Rotate the backups to the lastweek directory
DAYNUMBER=`date +%u`
if [ "$LASTWEEK" = "yes" ]; then
if [ "$DAYNUMBER" = "7" ]; then
cp -rf $BACKUPLOC/daily $BACKUPLOC/lastweek/
echo "- Rotated last weeks backups" >> .mailmessage
echo "" >> .mailmessage
fi
fi
# Create a mirror copy on another drive
if [ ! "$MIRROR" = "" ]; then
rsync -av $BACKUPLOC $MIRROR >/dev/null 2>&1
echo "- Created mirror to $MIRROR" >> .mailmessage
echo "" >> .mailmessage
fi
# Create FTP dump
if [ "$FTPDUMP" = "yes" ]; then
TODAY=`date +%R`
echo "- Starting FTP dump at $TODAY" >> .mailmessage
/usr/local/bin/ncftpput -u "$FTPUSERNAME" -p "$FTPPASSWORD" -R -V "$FTPIP" "$FTPDIR" "$BACKUPLOC/daily/$DAYOFWEEK/"
TODAY=`date +%R`
echo "- Ended FTP dump on $TODAY" >> .mailmessage
echo "" >> .mailmessage
fi
# Add filesystem details to the log and send it by email
echo "Current filesystem usage:" >> .mailmessage
echo "" >> .mailmessage
df -h >> .mailmessage
echo "" >> .mailmessage
# Add logfile
if [ -s .logfile ]; then
echo "Error messages:" >> .mailmessage
cat .logfile >> .mailmessage
else
echo "No error messages" >> .mailmessage
fi
echo "" >> .mailmessage
# Clean up I/O redirection
exec 1>&6 6>&-
exec 1>&7 7>&-
TODAY=`date +%F%t%R`
echo "$TODAY" >> .mailmessage
if [ ! "$EMAILADDRESS" = "" ]; then
cat .mailmessage | mail -s "Backup report" $EMAILADDRESS
fi
}
restore() {
# Restore MySQL or HTDOCS
echo "##### WARNING - RESTORING OLD ARCHIVE #####"
echo ""
PS3='Please make your selection: '
select type in "Restore MySQL Database" "Restore HTDOCS folder" ; do
break
done
echo ""
# Restore MySQL
if [ "$type" = "Restore MySQL Database" ]; then
echo "Restoring MySQL Database"
echo ""
DBNAMES="`$MYSQL --user=$USERNAME --password=$PASSWORD --host=localhost --batch --skip-column-names -e "show databases" | sed 's/ /%/g'`"
for exclude in $EXCLUDEDB
do
DBNAMES=`echo $DBNAMES | sed "s/\b$exclude\b//g"`
done
PS3='Please select the database you would like to restore:'
select DB in $DBNAMES "Not listed" ; do
break
done
echo ""
if [ "$DB" = "Not listed" ]; then
echo "Please enter the database name"
read DB
echo ""
fi
archives="`find $BACKUPLOC -iname mysql_${DB}*.gz`"
PS3='Please select the backup to restore: '
select archive in $archives "Cancel" ; do
break
done
echo ""
if [ "$archive" = "Cancel" ]; then
echo ""
echo "Restore database has been cancelled"
exit 0
fi
echo "Restore: $archive (yes or no)?"
read answer
if [ "$answer" = "yes" ]; then
gunzip $archive
archive="`echo $archive | sed 's/\.gz//g'`"
$MYSQL --user=$USERNAME --pass=$PASSWORD --host=localhost $DB < $archive
gzip -f $archive
echo ""
echo "Database restored"
else
echo ""
echo "Restore database has been cancelled"
fi
fi
# Restore HTDOCS
if [ "$type" = "Restore HTDOCS archive" ]; then
echo "Restoring HTDOCS archive"
echo ""
cd $HTFOLDER
HTDOCSNAMES="`find -type d -maxdepth 1 | sed 's/\.//g;' | sed 's/\///g;' | sed -e '1,1d'`"
for exclude in $EXCLUDEHTDOCS
do
HTDOCSNAMES=`echo $HTDOCSNAMES | sed "s/\b$exclude\b//g"`
done
PS3='Please select the HTDOCS archive you would like to restore:'
select HT in $HTDOCSNAMES "Not listed" ; do
break
done
echo ""
if [ "$HT" = "Not listed" ]; then
echo "Please enter the HTDOCS archive name"
read HT
echo ""
fi
archives="`find $BACKUPLOC -iname htdocs_${HT}*.gz`"
PS3='Please select the backup to restore: '
select archive in $archives "Cancel" ; do
break
done
echo ""
if [ "$archive" = "Cancel" ]; then
echo ""
echo "Restore HTDOCS archive has been cancelled"
exit 0
fi
echo "Restore: $archive (yes or no)?"
read answer
if [ "$answer" = "yes" ]; then
cp $archive $HTFOLDER/
archive="`basename $archive`"
gunzip $archive
archive="`echo $archive | sed 's/\.gz//g'`"
tar -xf $archive
rm $archive
echo ""
echo "HTDOCS archive restored"
else
echo ""
echo "HTDOCS restore cancelled"
fi
fi
}
main_menu() {
dialog \
--backtitle "by JF Nutbroek - www.mywebmymail.com" \
--title "Configure Backup" \
--menu "Select a function:" 0 0 0 \
1 "EDIT backup configuration" \
2 "SHOW backup configuration" \
3 "SAVE backup configuration" \
4 "ENABLE daily backup" \
5 "DISABLE daily backup" \
6 "TEST backup now!" \
7 "EXIT" 2>$TEMP
if [ "$?" != "0" ] ; then clean_up ; fi
choice=`cat $TEMP`
case $choice in
1) edit_backup;;
2) view_backup;;
3) save;;
4)
if [ "$SAVE" = "yes" ]; then
save
enable
else
enable
fi
dialog \
--sleep 1 \
--infobox "Enabling.." 3 30
;;
5)
disable
dialog \
--sleep 1 \
--infobox "Disabling.." 3 30
;;
6)
dialog \
--sleep 1 \
--infobox "Starting backup.." 3 30
start
if [ "$EMAILADDRESS" = "" ]; then
dialog \
--sleep 3 \
--infobox "Completed!\nCheck $BACKUPLOC for the result" 5 45
else
dialog \
--sleep 3 \
--infobox "Completed!\nCheck your email for the result" 5 45
fi
;;
7) clean_up;;
esac
main_menu
}
save() {
dialog \
--sleep 1 \
--infobox "Saving..." 3 13
echo "#!/bin/bash" > $SCRIPT/.backup
echo "# Backup configuration file" >> $SCRIPT/.backup
echo "BACKUPLOC=\"$BACKUPLOC\"" >> $SCRIPT/.backup
echo "MIRROR=\"$MIRROR\"" >> $SCRIPT/.backup
echo "USERNAME=\"$USERNAME\"" >> $SCRIPT/.backup
echo "PASSWORD=\"$PASSWORD\"" >> $SCRIPT/.backup
echo "EXCLUDEDB=\"$EXCLUDEDB\"" >> $SCRIPT/.backup
echo "EXCLUDEHTDOCS=\"$EXCLUDEHTDOCS\"" >> $SCRIPT/.backup
echo "FTP=\"$FTP\"" >> $SCRIPT/.backup
echo "FTPDUMP=\"$FTPDUMP\"" >> $SCRIPT/.backup
echo "FTPUSERNAME=\"$FTPUSERNAME\"" >> $SCRIPT/.backup
echo "FTPPASSWORD=\"$FTPPASSWORD\"" >> $SCRIPT/.backup
echo "FTPIP=\"$FTPIP\"" >> $SCRIPT/.backup
echo "FTPDIR=\"$FTPDIR\"" >> $SCRIPT/.backup
echo "LASTWEEK=\"$LASTWEEK\"" >> $SCRIPT/.backup
echo "ONEDAY=\"$ONEDAY\"" >> $SCRIPT/.backup
echo "EMAILADDRESS=\"$EMAILADDRESS\"" >> $SCRIPT/.backup
SAVE="no"
main_menu
}
view_backup() {
echo "Your configuration:" >$TEMP.summary
echo "" >>$TEMP.summary
echo "Main backup location: $BACKUPLOC" >>$TEMP.summary
if [ ! "$MIRROR" = "" ]; then
echo "Mirror location: $MIRROR" >>$TEMP.summary
else
echo "Mirror: disabled" >>$TEMP.summary
fi
echo "" >>$TEMP.summary
if [ ! "$EXCLUDEDB" = "" ]; then
echo "Excluded databases:" >>$TEMP.summary
echo "$EXCLUDEDB" >>$TEMP.summary
else
echo "Exclude databases: disabled" >>$TEMP.summary
fi
DBNAMES="`$MYSQL --user=$USERNAME --password=$PASSWORD --host=localhost --batch --skip-column-names -e "show databases" | sed 's/ /%/g'`"
for exclude in $EXCLUDEDB
do
DBNAMES=`echo $DBNAMES | sed "s/\b$exclude\b//g"`
done
echo "Databases included in the backup:" >>$TEMP.summary
echo "$DBNAMES" >>$TEMP.summary
echo "" >>$TEMP.summary
if [ "$FTP" = "yes" ]; then
echo "FTP Service: disabled during backup" >>$TEMP.summary
else
echo "FTP Service: enabled during backup" >>$TEMP.summary
fi
echo "" >>$TEMP.summary
if [ ! "$EXCLUDEHTDOCS" = "" ]; then
echo "Excluded directories:" >>$TEMP.summary
echo "$EXCLUDEHTDOCS" >>$TEMP.summary
else
echo "Exclude directories: disabled" >>$TEMP.summary
fi
cd $HTFOLDER
HTDOCSNAMES="`find -type d -maxdepth 1 | sed 's/\.//g;' | sed 's/\///g;' | sed -e '1,1d'`"
for exclude in $EXCLUDEHTDOCS
do
HTDOCSNAMES=`echo $HTDOCSNAMES | sed "s/\b$exclude\b//g"`
done
echo "Directories included in the backup:" >>$TEMP.summary
echo "$HTDOCSNAMES" >>$TEMP.summary
echo "" >>$TEMP.summary
if [ "$FTPDUMP" = "yes" ]; then
echo "FTP dump: enabled" >>$TEMP.summary
echo "FTP server IP: $FTPIP" >>$TEMP.summary
echo "FTP directory: $FTPDIR" >>$TEMP.summary
else
echo "FTP dump: disabled" >>$TEMP.summary
fi
echo "" >>$TEMP.summary
if [ "$LASTWEEK" = "yes" ]; then
echo "Backup period: 14 days" >>$TEMP.summary
else
if [ "$ONEDAY" = "yes" ]; then
echo "Backup period: 1 day" >>$TEMP.summary
else
echo "Backup period: 7 days" >>$TEMP.summary
fi
fi
echo "" >>$TEMP.summary
if [ "$EMAILADDRESS" = "" ]; then
echo "Email confirmation: disabled" >>$TEMP.summary
else
echo "Email confirmation to: $EMAILADDRESS" >>$TEMP.summary
fi
echo "" >>$TEMP.summary
dialog \
--title "Configure Backup" \
--textbox $TEMP.summary 20 60 2>/dev/null
rm $TEMP.summary
main_menu
}
edit_backup() {
dialog \
--backtitle "by JF Nutbroek - www.mywebmymail.com" \
--title "Configure Backup" \
--menu "Select a function:" 0 0 0 \
1 "Enter main backup location" \
2 "Enter mirror location" \
3 "Database details" \
4 "Webserver details" \
5 "FTP dump" \
6 "Backup period" \
7 "Email confirmation" \
8 "Return to main menu" 2>$TEMP
if [ "$?" != "0" ] ; then main_menu ; fi
choice=`cat $TEMP`
case $choice in
1) enter_mainbackup;;
2) enter_mirror;;
3) mysql_config;;
4) apache_config;;
5) ftp_config;;
6) backup_period;;
7) enter_email;;
8) main_menu;;
esac
}
backup_period() {
dialog \
--backtitle "by JF Nutbroek - www.mywebmymail.com" \
--title "Configure Backup" \
--radiolist "Select backup period\nUse SPACE to select" 0 0 0 \
1 "1 day" off \
2 "7 days" off \
3 "14 days" ON \
2>$TEMP
if [ "$?" != "0" ] ; then edit_network ; fi
choice=`cat $TEMP`
case $choice in
1)
ONEDAY="yes"
LASTWEEK="no"
;;
2)
ONEDAY="no"
LASTWEEK="no"
;;
3)
ONEDAY="no"
LASTWEEK="yes"
;;
esac
save="yes"
edit_backup
}
enter_mainbackup() {
dialog \
--backtitle "by JF Nutbroek - www.mywebmymail.com" \
--title "Configure Backup" \
--inputbox "Please enter the main backup location:" 10 45 "$BACKUPLOC" 2>$TEMP
if [ "$?" != "0" ] ; then edit_backup ; fi
BACKUPLOC=`cat $TEMP`
SAVE="yes"
enter_mirror
}
enter_mirror() {
dialog \
--backtitle "by JF Nutbroek - www.mywebmymail.com" \
--title "Configure Backup" \
--inputbox "Please enter the mirror location:\nExample /mnt/hda2\n(leave empty to disable)" 10 45 "$MIRROR" 2>$TEMP
if [ "$?" != "0" ] ; then edit_backup ; fi
MIRROR=`cat $TEMP`
SAVE="yes"
mysql_config
}
mysql_config() {
dialog \
--backtitle "by JF Nutbroek - www.mywebmymail.com" \
--title "Configure Backup" \
--inputbox "Please enter your MySQL username" 10 45 "$USERNAME" 2>$TEMP
if [ "$?" != "0" ] ; then edit_backup ; fi
USERNAME=`cat $TEMP`
dialog \
--backtitle "by JF Nutbroek - www.mywebmymail.com" \
--title "Configure Backup" \
--insecure \
--passwordbox "Please enter your MySQL password" 10 45 2>$TEMP
if [ "$?" != "0" ] ; then edit_backup ; fi
PASSWORD=`cat $TEMP`
DBNAMES="`$MYSQL --user=$USERNAME --password=$PASSWORD --host=localhost --batch --skip-column-names -e "show databases" | sed 's/ /%/g'`"
if [ "$DBNAMES" = "" ]; then
dialog \
--sleep 2 \
--infobox "Wrong username/password!" 3 30
mysql_config
fi
dialog \
--backtitle "by JF Nutbroek - www.mywebmymail.com" \
--title "Configure Backup" \
--inputbox "Please enter databases (space separated) to EXCLUDE\nThe following databases are available:\n \n$DBNAMES" 15 60 "$EXCLUDEDB" 2>$TEMP
if [ "$?" != "0" ] ; then edit_backup ; fi
EXCLUDEDB=`cat $TEMP`
dialog \
--backtitle "by JF Nutbroek - www.mywebmymail.com" \
--title "Configure Backup" \
--yesno "Would you like to disable the FTP service during backup (to stop incoming data)?\nNote: backup might take a long time" 10 45
case $? in
0) FTP="yes";;
1) FTP="no";;
255) edit_backup;;
esac
SAVE="yes"
apache_config
}
apache_config() {
cd $HTFOLDER
HTDOCSNAMES="`find -type d -maxdepth 1 | sed 's/\.//g;' | sed 's/\///g;' | sed -e '1,1d'`"
dialog \
--backtitle "by JF Nutbroek - www.mywebmymail.com" \
--title "Configure Backup" \
--inputbox "Please enter directories (space separated) to EXCLUDE\nThe following directories are available:\n \n$HTDOCSNAMES" 15 60 "$EXCLUDEHTDOCS" 2>$TEMP
if [ "$?" != "0" ] ; then edit_backup ; fi
EXCLUDEHTDOCS=`cat $TEMP`
SAVE="yes"
edit_backup
}
ftp_config() {
dialog \
--backtitle "by JF Nutbroek - www.mywebmymail.com" \
--title "Configure Backup" \
--yesno "Would you like to enable the FTP dump?" 6 50
case $? in
0) FTPDUMP="yes";;
1) FTPDUMP="no";;
255) edit_backup;;
esac
if [ "$FTPDUMP" = "yes" ]; then
dialog \
--backtitle "by JF Nutbroek - www.mywebmymail.com" \
--title "Configure Backup" \
--inputbox "Please enter your FTP username" 10 50 "$FTPUSERNAME" 2>$TEMP
if [ "$?" != "0" ] ; then edit_backup ; fi
FTPUSERNAME=`cat $TEMP`
dialog \
--backtitle "by JF Nutbroek - www.mywebmymail.com" \
--title "Configure Backup" \
--insecure \
--passwordbox "Please enter your FTP password" 10 50 2>$TEMP
if [ "$?" != "0" ] ; then edit_backup ; fi
FTPPASSWORD=`cat $TEMP`
dialog \
--backtitle "by JF Nutbroek - www.mywebmymail.com" \
--title "Configure Backup" \
--inputbox "Please enter the FTP server IP address" 10 50 "$FTPIP" 2>$TEMP
if [ "$?" != "0" ] ; then edit_backup ; fi
FTPIP=`cat $TEMP`
dialog \
--backtitle "by JF Nutbroek - www.mywebmymail.com" \
--title "Configure Backup" \
--inputbox "Please enter the FTP server path" 10 45 "$FTPDIR" 2>$TEMP
if [ "$?" != "0" ] ; then edit_backup ; fi
FTPDIR=`cat $TEMP`
if [ "$FTPDIR" = "" ]; then
FTPDIR="/"
fi
fi
SAVE="yes"
edit_backup
}
enter_email() {
dialog \
--backtitle "by JF Nutbroek - www.mywebmymail.com" \
--title "Configure Backup" \
--yesno "Would you like to receive a daily email confirmation?" 8 45
case $? in
0) EMAIL="yes";;
1) EMAIL="no";;
255) edit_backup;;
esac
if [ "$EMAIL" = "yes" ]; then
dialog \
--backtitle "by JF Nutbroek - www.mywebmymail.com" \
--title "Configure Backup" \
--inputbox "Please enter your email address:" 10 45 "$EMAILADDRESS" 2>$TEMP
if [ "$?" != "0" ] ; then edit_backup ; fi
EMAILADDRESS=`cat $TEMP`
else
EMAILADDRESS=""
fi
SAVE="yes"
edit_backup
}
trap clean_up SIGHUP SIGINT SIGQUIT SIGKILL SIGTERM
case "$1" in
'start')
start
;;
'config')
setterm -background blue
clear
main_menu
setterm -background black
;;
'disable')
disable
;;
'enable')
enable
;;
'restore')
setterm -background blue
clear
restore
setterm -background black
;;
*)
echo "Usage: backup_software config | start | disable | enable | restore"
esac
exit 0
Initial URL
Initial Description
Initial Title
SH Backup Script
Initial Tags
Initial Language
Bash