#!/bin/bash # Uncomment the following line for verbose output, useful for debugging purposes # set -x # # Make sure you are root if [ "$(id -u)" != "0" ]; then if [ -f /usr/bin/dialog ]; then dialog --title " You are not root " --msgbox "\nYou need to be root in order to use this installer.\n\nInstaller will now exit." 10 40 clear; exit else clear; echo -e "You need to be root in order to use this installer.\n\nInstaller will now exit." exit fi fi # Some software checks after we made sure we are root: clear echo -e "First a quick software check to make sure all dependencies are present on the server:" if [ -f /etc/debian_version ]; then echo -n "Dialog: " if [ -x /usr/bin/dialog ]; then echo -e "Present" else if apt-get -y install dialog > /dev/null 2> /dev/null then echo -e "Installed" else echo -e "There was an error installing dialog! Installer wil not start!" exit fi fi elif [ -f /etc/redhat-release ]; then echo -n "Dialog: " if [ -x /usr/bin/dialog ]; then echo -e "Present" else if yum install -q -y dialog > /dev/null 2> /dev/null then echo -e "Installed" else echo -e "There was an error installing dialog! Installer will not start" exit; clear fi fi fi # Now that we have confirmed if Dialog is present, start the other software checks in Dialog: if [ -f /etc/debian_version ]; then echo "10" | dialog --gauge "Checking presence of rsync: " 8 50 0 if [ -f /usr/bin/rsync ]; then echo "20" | dialog --gauge "Checking presense of rsync: OK" 8 50 0; sleep 1 else if apt-get -y install rsync > /dev/null 2> /dev/null then echo "20" | dialog --gauge "Checking presence of rsync: Installed" 8 50 0; sleep 1 else echo "20" | dialog --gauge "Rsync not installed due to errors, quitting..."; sleep 3 exit fi fi echo "30" | dialog --gauge "Checking presence of sendmail: " 8 50 0 if [ -f /usr/sbin/sendmail ]; then echo "40" | dialog --gauge "Checking presense of sendmail: OK" 8 50 0; sleep 1 else if apt-get -y install sendmail > /dev/null 2> /dev/null then echo "40" | dialog --gauge "Checking presence of sendmail: Installed" 8 50 0; sleep 1 else echo "40" | dialog --gauge "Sendmail not installed due to errors, mail options not available..."; sleep 3 fi fi echo "50" | dialog --gauge "Checking presence of mailx: " 8 50 0 if [ -f /usr/bin/mail ]; then echo "60" | dialog --gauge "Checking presense of mailx: OK" 8 50 0; sleep 1 else if apt-get -y install heirloom-mailx > /dev/null 2> /dev/null then echo "60" | dialog --gauge "Checking presence of mailx: Installed" 8 50 0; sleep 1 else echo "60" | dialog --gauge "Mailx not installed due to errors, mail options not available..."; sleep 3 fi fi echo "70" | dialog --gauge "Checking presence of bc: " 8 50 0 if [ -f /usr/bin/bc ]; then echo "80" | dialog --gauge "Checking presense of bc: OK" 8 50 0; sleep 1 else if apt-get -y install bc > /dev/null 2> /dev/null then echo "80" | dialog --gauge "Checking presence of bc: Installed" 8 50 0; sleep 1 else echo "80" | dialog --gauge "Bc not installed due to errors, calculations will not be available..."; sleep 3 fi fi echo "90" | dialog --gauge "Checking presence of expect: " 8 50 0 if [ -f /usr/bin/expect ]; then echo "100" | dialog --gauge "Checking presense of expect: OK" 8 50 0; sleep 1 else if apt-get -y install expect > /dev/null 2> /dev/null then echo "100" | dialog --gauge "Checking presence of expect: Installed" 8 50 0; sleep 1 else echo "100" | dialog --gauge "Expect not installed due to errors, quitting..."; sleep 3 exit fi fi fi if [ -f /etc/redhat-release ]; then echo "10" | dialog --gauge "Checking presence of rsync: " 8 50 0 if [ -f /usr/bin/rsync ]; then echo "20" | dialog --gauge "Checking presense of rsync: OK" 8 50 0; sleep 1 else if wget http://packages.sw.be/rsync/rsync-3.0.8-1.el5.rfx.x86_64.rpm > /dev/null 2> /dev/null; rpm -i --quiet rsync-3.0.8-1.el5.rfx.x86_64.rpm > /dev/null 2> /dev/null then echo "20" | dialog --gauge "Checking presence of rsync: Installed" 8 50 0; sleep 1 else echo "20" | dialog --gauge "Rsync not installed due to errors, quitting..."; sleep 3 exit fi fi echo "30" | dialog --gauge "Checking presence of sendmail: " 8 50 0 if [ -f /usr/sbin/sendmail ]; then echo "40" | dialog --gauge "Checking presense of sendmail: OK" 8 50 0; sleep 1 else if yum install -q -y sendmail > /dev/null 2> /dev/null then echo "40" | dialog --gauge "Checking presence of sendmail: Installed" 8 50 0; sleep 1 else echo "40" | dialog --gauge "Sendmail not installed due to errors, mail options not available..."; sleep 3 fi fi echo "50" | dialog --gauge "Checking presence of mailx: " 8 50 0 if [ -f /usr/bin/mail ]; then echo "60" | dialog --gauge "Checking presense of mailx: OK" 8 50 0; sleep 1 else if yum install -q -y mailx > /dev/null 2> /dev/null then echo "60" | dialog --gauge "Checking presence of mailx: Installed" 8 50 0; sleep 1 else echo "60" | dialog --gauge "Mailx not installed due to errors, mail options not available..."; sleep 3 fi fi echo "70" | dialog --gauge "Checking presence of bc: " 8 50 0 if [ -f /usr/bin/bc ]; then echo "80" | dialog --gauge "Checking presense of bc: OK" 8 50 0; sleep 1 else if yum install -q -y bc > /dev/null 2> /dev/null then echo "80" | dialog --gauge "Checking presence of bc: Installed" 8 50 0; sleep 1 else echo "80" | dialog --gauge "Bc not installed due to errors, calculations will not be available..."; sleep 3 fi fi echo "90" | dialog --gauge "Checking presence of expect: " 8 50 0 if [ -f /usr/bin/expect ]; then echo "100" | dialog --gauge "Checking presense of expect: OK" 8 50 0; sleep 1 else if yum install -q -y install expect > /dev/null 2> /dev/null then echo "100" | dialog --gauge "Checking presence of expect: Installed" 8 50 0; sleep 1 else echo "100" | dialog --gauge "Expect not installed due to errors, quitting..."; sleep 3 exit fi fi fi # You can change the branding options below. This is the name which will be used # in the backup mails and installation brandname="XLS Hosting" shortbrandname=xls # Algemene instellingen version="2.0" defaultsshport=22 defaultbackuppath=/ sqlbackupdefault=/var/sqlbackups privkeydefault=/root/.ssh/id_rsa."$shortbrandname"backup currentsize=$(df -hT --exclude-type="tmpfs" --exclude-type="devtmpfs" | grep "/" | awk '{print $4}' | tr -d G) clear dialog --title " $brandname backup installation script V$version " --msgbox "\nWelcome to the $brandname backup installation script.\n\nDuring this setup, the installer will ask several questions regarding your backupaccount and it's options.\n\nWhen you have answered all questions, the installer will install the backupscript for you.\n\nNote that some input fields already contain a value, in this case you do not need to change this in regular cases." 17 60 dialog --title " Backup details " --inputbox "\nWhat's your accountname?" 10 40 2> buser buser=$(cat buser) while [ -z $buser ] do dialog --title " Backup details " --inputbox "\nEmpty entry not allowed!\n\nWhat's your accountname?" 12 40 2> buser buser=$(cat buser) done dialog --title " Backup details " --insecure --passwordbox "\nWhat's your backup password?\n(Output is masked)\n" 12 40 2> bpass bpass=$(cat bpass) while [ -z $bpass ] do dialog --title " Backup details " --insecure --passwordbox "\nEmpty entry not allowed!\n\nWhat's your backup password?\n(Output is masked)\n" 14 40 2> bpass bpass=$(cat bpass) done dialog --title " Backup details " --inputbox "\nWhat's your backupserver?" 10 40 2> bserver bserver=$(cat bserver) while [ -z $bserver ] do dialog --title " Backup details " --inputbox "\nEmpty entry not allowd!\n\nWhat's your backupserver?" 12 40 2> bserver bserver=$(cat bserver) done dialog --title " Backup details " --inputbox "\nSSH Port?" 10 40 $defaultsshport 2> sshport sshport=$(cat sshport) while [ -z $sshport ] do dialog --title " Backup details " --inputbox "\nEmpty entry not allowed!\n\nSSH port?" 12 40 $defaultsshport 2> sshport sshport=$(cat sshport) done dialog --title " Backup details " --inputbox "\nPrivate key to use?" 10 50 $privkeydefault 2> privkey privkey=$(cat privkey) while [ -z $privkey ] do dialog --title " Backup details " --inputbox "\nEmpty entry not allowed!\n\nPrivate key to use?" 12 40 $privkeydefault 2> privkey privkey=$(cat privkey) done dialog --title " Folder settings " --inputbox "\nWhat's the absolute location as from where the backup should start?" 11 40 $defaultbackuppath 2> backuppath backuppath=$(cat backuppath) while [ -z $backuppath ] do dialog --title " Folder settings " --inputbox "\nEmpty entry not allowed!\n\nWhat's the absolute location ad from where the backup should start?" 13 40 $defaultbackuppath 2> backuppath backuppath=$(cat backuppath) done dialog --title " Folder settings " --inputbox "\nTo what absolute location should the backup be written to?" 11 40 /home/$buser/ 2> remotepath remotepath=$(cat remotepath) while [ -z $remotepath ] do dialog --title " Folder settings " --inputbox "\nEmpty entry not allowed!\n\nTo what absolute location should the backup be written to?" 13 40 /home/$buser/ 2> remotepath remotepath=$(cat remotepath) done dialog --title " Retention settings " --inputbox "\nGive the amount of extra weeks of retention needed:\n\n(Every extra option will cost an additional $currentsize Gb of backupspace)\n" 15 50 0 2> weken weken=$(cat weken) while ! [ $weken -ge 0 -a $weken -le 52 ] do dialog --title " Retention settings " --inputbox "\nWrong entry!\n\nGive the amount of extra weeks of retention needed:\n\n(Every extra option will cost an additional $currentsize Gb of backupspace)\n" 16 50 $weken 2> weken weken=$(cat weken) done dialog --title " Retention settings " --inputbox "\nGive the amount of extra month of retention needed:\n\n(Every extra option will cost an additional $currentsize Gb of backupspace)\n" 15 50 0 2> maanden maanden=$(cat maanden) while ! [ $maanden -ge 0 -a $maanden -le 12 ] do dialog --title " Retention settings " --inputbox "\nWrong entry!\n\nGive the amount of extra months of retention needed:i\n\n(Every extra option will cost an additional $currentsize Gb of backupspace)\n" 16 50 $maanden 2> maanden maanden=$(cat maanden) done dialog --title " MySQL backup settings " --yesno "\nDo you want to enable the MySQL dump feature?" 8 40 mysqldumps=$? case $mysqldumps in 0) if [ -d /etc/openpanel ]; then dialog --title " MySQL backup settings " --inputbox "\nOpenPanel found.\n\nWhich folder do you want to use for the dumps?" 12 50 $sqlbackupdefault 2> sqlbackups sqlbackups=$(cat sqlbackups) elif [ -d /usr/local/directadmin ]; then dialog --title " MySQL backup settings " --inputbox "\nDirectAdmin found.\n\nWhich folder do you want to use for the dumps?" 12 50 $sqlbackupdefault 2> sqlbackups sqlbackups=$(cat sqlbackups) elif [ -d /usr/local/psa ]; then dialog --title " MySQL backup settings " --inputbox "\nParallels Plesk found.\n\nWhich folder do you want to use for the dumps?" 12 50 $sqlbackupdefault 2> sqlbackups sqlbackups=$(cat sqlbackups) elif [ -d /usr/local/cpanel ]; then dialog --title " MySQL backup settings " --inputbox "\ncPanel/WHM found.\n\nWhich folder do you want to use for the dumps?" 12 50 $sqlbackupdefault 2> sqlbackups sqlbackups=$(cat sqlbackups) elif [ -f /etc/mysql/debian.cnf ]; then dialog --title " MySQL backup settings " --inputbox "\nDebian found.\n\nWhich folder do you want to use for the dumps?" 12 50 $sqlbackupdefault 2> sqlbackups sqlbackups=$(cat sqlbackups) else dialog --title " MySQL backup settings " --inputbox "\nNo ControlPanel found.\n\nYou need to enter the MySQL details.\n\nWhat is the MySQL user?" 14 50 root 2> sqlbackupuser dialog --title " MySQL backup settings " --inputbox "\nWhat is the MySQL password?" 10 50 2> sqlbackuppass dialog --title " MySQL backup settings " --inputbox "\nWhich folder do you want to use for the dumps?" 10 50 $sqlbackupdefault 2> sqlbackups sqlbackups=$(cat sqlbackups) fi ;; 1) sqlbackups="Not applicable" ;; 255) exit; clear; echo "[ESC] key pressed. Will now exit.";; esac dialog --title " Job settings " --inputbox "\nHour to run at?\n\nValid options are 0-23, or cron specific like */4 for every 4 hours." 13 50 2> uren uren=$(cat uren) while ! [ $uren -ge 0 -a $uren -le 24 ] do dialog --title " Job settings " --inputbox "\nWrong entry!\n\nHour to run at?\n\nValid options are 0-23, or cron specific like */4 for every 4 hours." 15 50 $uren 2> uren uren=$(cat uren) done dialog --title " Job settings " --inputbox "\nMinute(s) to run at?\n\nValid options are 0-59, or cron specific like */30 for every 30 minutes." 13 50 2> minuten minuten=$(cat minuten) while ! [ $minuten -ge 0 -a $minuten -le 59 ] do dialog --title " Job settings " --inputbox "\nWrong entry!\n\nMinute(s) to run at?\n\nValid options are 0-59, or cron specific options like */30 for every 30 minutes." 15 50 $minuten 2> minuten minuten=$(cat minuten) done dialog --title " Job settings " --inputbox "\nEmailaddress to send backup mails to?" 11 40 2> email email=$(cat email) while [ -z $email ] do dialog --title " Job settings " --inputbox "\nEmpty entry not allowed!\n\nEmailaddress to send backup reports to?" 13 40 2> email email=$(cat email) done dialog --title " Log settings " --yesno "\nDo you want to enable the backup logging feature?" 8 50 logging=$? case $logging in 0) dialog --title " Log settings " --yesno "\nIn addition, do you want this log to be emailed to the given emailaddress at $email?" 9 50 maillog=$? ;; 1) maillog=1 ;; esac # Coverting some variables into more human readable output: if [ "$mysqldumps" = "0" ]; then hrmysqldumps="Yes" else hrmysqldumps="No" fi if [ "$logging" = "0" ]; then hrlogging="Yes" else hrlogging="No" fi if [ "$maillog" = "0" ]; then hrmaillog="Yes" else hrmaillog="No" fi if ! [ "$weken" = "0" ]; then wtotal=$(echo $weken*$currentsize | bc) fi if ! [ "$maanden" = "0" ]; then mtotal=$(echo $maanden*$currentsize | bc) fi dailysize=$(echo $currentsize*2 | bc) if [ "$maanden" = "0" -a "$weken" -gt "0" ]; then totalsize=$(echo $wtotal+$dailysize | bc) elif [ "$weken" = "0" -a "$maanden" -gt "0" ]; then totalsize=$(echo $mtotal+$dailysize | bc) elif [ "$weken" -gt "0" -a "$maanden" -gt "0" ]; then totalsize=$(echo $mtotal+$wtotal+$dailysize | bc) else totalsize=$dailysize fi # Showing last dialog box containing values given: dialog --title " Setup details " --yesno "\nYou have given the following details:\n\nUsername....: $buser\nBackupserver: $bserver\nSSH Port....: $sshport\nKey.........: $privkey\nLocal path..: $backuppath\nRemote path.: $remotepath\nEmailaddress: $email\nSQL dumps...: $hrmysqldumps\nDump path...: $sqlbackups\nLogging.....: $hrlogging\nEmail log...: $hrmaillog\nSchedule....: $uren:$minuten\nRetention...: $weken week(s) and $maanden month(s)\nBackup size.: $totalsize Gb\n\nIs the following correct?" 24 50 accept=$? case $accept in 0) # Doing some magic and installing the script, showing status in a gauge bar. if [ -f $privkey ]; then echo "10" | dialog --gauge "Key found, not generating a new one" 8 50 0; sleep 1 else echo "10" | dialog --gauge "Generating key" 8 50 0; sleep 1;ssh-keygen -t rsa -N '' -f $privkey > /dev/null 2> /dev/null fi echo "20" | dialog --gauge "Checking for keyfile on the backupserver" 8 50 0; sleep 1 if /usr/bin/expect > /dev/null 2> /dev/null << EOF spawn sftp -oStrictHostKeyChecking=no -oPort=$sshport $buser@$bserver expect "password:" send "$bpass\r" expect "sftp>" send "get .ssh/authorized_keys .\r" expect "sftp>" send "quit\r" EOF then if [ -f authorized_keys ] then echo "20" | dialog --gauge "Keyfile found, key added and uploading new keyfile" 8 50 0; sleep 1; cat "$privkey.pub" >> authorized_keys; /usr/bin/expect > /dev/null 2> /dev/null << EOF spawn sftp -oStrictHostKeyChecking=no -oPort=$sshport $buser@$bserver expect "password:" send "$bpass\r" expect "sftp>" send "put authorized_keys .ssh/authorized_keys\r" expect "sftp>" send "quit\r" EOF rm authorized_keys else echo "20" | dialog --gauge "Keyfile not found, creating and uploading new file" 8 50 0; sleep 1; /usr/bin/expect > /dev/null 2> /dev/null << EOF spawn sftp -oStrictHostKeyChecking=no -oPort=$sshport $buser@$bserver expect "password:" send "$bpass\r" expect "sftp>" send "mkdir .ssh\r" expect "sftp>" send "cd .ssh\r" expect "sftp>" send "put $privkey.pub authorized_keys\r" expect "sftp>" send "quit\r" EOF fi fi echo "30" | dialog --gauge "Downloading script" 8 50 0; sleep 1; wget -O /usr/local/bin/"$shortbrandname"backup opensource.xlshosting.nl/scripts/xlsbackup.txt > /dev/null 2> /dev/null if wget -O /usr/local/bin/"$shortbrandname"backup http://opensource.xlshosting.com/scripts/xlsbackup.txt > /dev/null 2> /dev/null; then echo "40" | dialog --gauge "Download of the script succeeded" 8 50 0; sleep 1 else echo "40" | dialog --gauge "Download of the script failed!\nPlease check your networksettings" 8 50 0 exit fi echo "50" | dialog --gauge "Setting permissions on script" 8 50 0; sleep 1; chmod +x /usr/local/bin/"$shortbrandname"backup > /dev/null 2> /dev/null echo "60" | dialog --gauge "Creating configfile and setting options" 8 50 0; sleep 1; mkdir -p /etc/backups/; touch /etc/backups/"$shortbrandname"backup.conf; chmod 600 /etc/backups/"$shortbrandname"backup.conf; touch /etc/backups/brand.conf; chmod 600 /etc/backups/brand.conf echo -e "BUSER=$buser" >> /etc/backups/"$shortbrandname"backup.conf echo -e "BSERVER=$bserver" >> /etc/backups/"$shortbrandname"backup.conf echo -e "NOTIFICATIONS=Y" >> /etc/backups/"$shortbrandname"backup.conf echo -e "WEKEN=$weken" >> /etc/backups/"$shortbrandname"backup.conf echo -e "MAANDEN=$maanden" >> /etc/backups/"$shortbrandname"backup.conf echo -e "PRIVKEY=$privkey" >> /etc/backups/"$shortbrandname"backup.conf echo -e "OLDRSYNCMAIL=$email" >> /etc/backups/"$shortbrandname"backup.conf echo -e "BACKUPROOT=$backuppath" >> /etc/backups/"$shortbrandname"backup.conf echo -e "EXCLUDES=\"--exclude /proc --exclude /sys --exclude /var/log --exclude /tmp --exclude /dev\"" >> /etc/backups/"$shortbrandname"backup.conf echo -e "XFERSPEED=5000" >> /etc/backups/"$shortbrandname"backup.conf echo -e "SSHPORT=$sshport" >> /etc/backups/"$shortbrandname"backup.conf echo -e "REMOTEPATH=$remotepath" >> /etc/backups/"$shortbrandname"backup.conf echo -e "BRANDNAME=\"$brandname\"" >> /etc/backups/brand.conf echo -e "SHORTBRANDNAME=$shortbrandname" >> /etc/backups/brand.conf echo -e "H=$(hostname)" >> /etc/backups/"$shortbrandname"backup.conf if [ "$logging" = 0 ]; then echo -e "LOGGING=Y" >> /etc/backups/"$shortbrandname"backup.conf echo -e "LOGPATH=--log-file=/var/log/"$shortbrandname"backup/" >> /etc/backups/"$shortbrandname"backup.conf else echo -e "LOGGING=N" >> /etc/backups/"$shortbrandname"backup.conf echo -e "LOGPATH=--log-file=/var/log/"$shortbrandname"backup/" >> /etc/backups/"$shortbrandname"backup.conf fi if [ "$logmail" = 0 ]; then echo -e "MAILLOG=Y" >> /etc/backups/"$shortbrandname"backup.conf else echo -e "MAILLOG=N" >> /etc/backups/"$shortbrandname"backup.conf fi if [ "$mysqldumps" = 0 ] ; then echo -e "MYSQLBACKUP=0" >> /etc/backups/"$shortbrandname"backup.conf echo -e "MYSQLUSER=$sqlbackupuser" >> /etc/backups/"$shortbrandname"backup.conf echo -e "MYSQLPASS=$sqlbackuppass" >> /etc/backups/"$shortbrandname"backup.conf echo -e "MYSQLBACKUPDIR=$sqlbackups" >> /etc/backups/"$shortbrandname"backup.conf else echo -e "MYSQLBACKUP=1" >> /etc/backups/"$shortbrandname"backup.conf echo -e "MYSQLUSER=" >> /etc/backups/"$shortbrandname"backup.conf echo -e "MYSQLPASS=" >> /etc/backups/"$shortbrandname"backup.conf echo -e "MYSQLBACKUPDIR=$sqlbackdefault" >> /etc/backups/"$shortbrandname"backup.conf fi echo "70" | dialog --gauge "Locking configfile" 8 50 0; sleep 1; chattr +i /etc/backups/"$shortbrandname"backup.conf echo "80" | dialog --gauge "Setting up the cronjob" 8 50 0; sleep 1; cat <(crontab -l) <(echo "$minuten $uren * * * /usr/local/bin/"$shortbrandname"backup 2>&1 | mail -s 'Backup van `hostname`' $email") | crontab - echo "80" | dialog --gauge "Cronjob successfully set up" 8 50 0; sleep 1 echo "90" | dialog --gauge "Removing temporary files" 8 50 0; sleep 1 rm bserver > /dev/null 2> /dev/null rm sshport > /dev/null 2> /dev/null rm privkey > /dev/null 2> /dev/null rm backuppath > /dev/null 2> /dev/null rm remotepath > /dev/null 2> /dev/null rm email > /dev/null 2> /dev/null rm buser > /dev/null 2> /dev/null rm maanden > /dev/null 2> /dev/null rm minuten > /dev/null 2> /dev/null rm uren > /dev/null 2> /dev/null rm weken > /dev/null 2> /dev/null rm bpass > /dev/null 2> /dev/null if [ -f sqlbackupuser ]; then rm sqlbackupuser > /dev/null 2> /dev/null fi if [ -f sqlbackuppass ]; then rm sqlbackuppass > /dev/null 2> /dev/null fi rm sqlbackups > /dev/null 2> /dev/null echo "100" | dialog --gauge "The $brandname backupscript is now successfully set up!" 9 50 0;sleep 2 dialog --title " Report " --yesno "\nDo you want to have a short installation report emailed to $email?" 10 50 sendreport=$? if [ $sendreport = 0 ]; then mail -s 'Backupscript installed successfully!' $email <