0

I have a strange problem with my backup script.

To begin with, this is the backup script:

###########################################################
# CODE BEGINS HERE
###########################################################
#!/bin/bash
# SERVER DATA BACKUP V1.0
#########################
# BACKUP CONFIG
#########################
#Configure the month, date, and day
YEAR=`date +"%Y"` # 2014
MONTH=`date +"%m"` # 10
TODAY=`date +%Y-%m-%d` # 2008-11-14
TODAY_DATE=`date +%d` # 14

# directories to backup to

DATA_BACKUP_PARENT_DIR=/home/user/backups

# Apache Document Root Directory
APACHE_DOCROOT=/var/www/

# MySQL Database config
DB_IP="localhost" 
DB_USER="auser" # 
DB_PASS="apass" # 

PUBLIC_HTML=/home/user/public_html

# Config Dir
CONFIG_DIR="/etc/" # <-- Change this to the path data directory

# Mail report address
EMAIL_ID="user@mail.com" 


###########################################################
# DATA BACKUP
###########################################################

# Create and go into backup directory
cd $DATA_BACKUP_PARENT_DIR
if [ ! -d "$TODAY" ]; then
  mkdir $TODAY
fi
cd $TODAY


# Database backup
databases=`mysql --user=$DB_USER --password=$DB_PASS -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
echo "Backing up databases"
for db in $databases; do
    if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != _* ]] ; then
    echo "Dumping database: $db"
    mysqldump --force --opt --user=$DB_USER --password=$DB_PASS $db > $db.db
    tar -zcvf $db.tar.gz $db.db
    rm $db.db
    fi
done

echo "Backing up public_html folder"
#Home folder backup
tar -zcf public_html.tar.gz ${PUBLIC_HTML} --exclude='.*' --exclude=/home/user/public_html/software


# /etc backup
#tar -zcf etc.tar.gz /etc

echo "Backing up Apache wwww doc root folder"
# Application and Websites backup
tar -zcf Apache_Doc_Root.tar.gz ${APACHE_DOCROOT}


###########################################################
# Send out mail notifications
###########################################################
# Mail notify

du -shc $DATA_BACKUP_PARENT_DIR/$TODAY/* | mail -s "Backup of server done " ${EMAIL_ID}

###########################################################
# CODE ENDS HERE
#

When i run the script manually from the console, the commands run smoothly and all appropriate folders are created. The confirmation mail says:

250M    /home/user/backups/2014-11-19/Apache_Doc_Root.tar.gz
152K    /home/user/backups/2014-11-19/mysql.tar.gz
487M    /home/user/backups/2014-11-19/public_html.tar.gz
201M    /home/user/backups/2014-11-19/mydb.tar.gz
240K    /home/user/backups/2014-11-19/my_site.tar.gz
76K     /home/user/backups/2014-11-19/teo_wordpress.tar.gz
937M    total

I have scheduled this script with cron. The scripts runs at the specified time, but backups only SOME of the folders. Specifically it backups only the apache doc root and the public_html. The corresponding Email says:

250M    /home/teohaik/backups/2014-11-29/Apache_Doc_Root.tar.gz
487M    /home/teohaik/backups/2014-11-29/public_html.tar.gz
737M    total

Any ideas about that?

Thank you in advance!

  • Check your cron environment (see here) and make sure cron is actually able to run mysql and mysqldump – Josh Jolly Dec 1 '14 at 11:48
  • Your script produces some output, so cron sends you a mail with the script's output. Read that output, it probably contains an indication of what the problem is. Edit your question to add the output if you need help figuring it out. – Gilles 'SO- stop being evil' Dec 2 '14 at 14:44
0

Have a look in your cron Configuration file.

Maybe your cron-job is run as an specific user which does not have access to everything.

Also its a clever idea to write log-files so you can see what exactly your script does when run by cron.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.