Member Avatar for Pityu

Hey there everyone!

I have an issue with a script with which I had sweated much, and I can't suspend it even if I find another solution for what I want.
What I want would be the following:
A script that creates a full backup of a directory and all of its subdirectories and the mysql, uploads it to an external ftp server, and sends an email to me when it's done.

It says that it has error in the last line (88 : unexpected operator )

[: 88: 0: unexpected operator

It also has an error with the tar.gz, when I try to unpack with unrar gives this error:

!   D:\_server_backup\incremental\04-08-2011\fs-i-04-08-2011-00h44m22s.tar.gz: Cannot open \opt\lampp\lib\VERSION (opt\lampp\htdocs\xampp\.version --> \opt\lampp\lib\VERSION)
!   D:\_server_backup\incremental\04-08-2011\fs-i-04-08-2011-00h44m22s.tar.gz: Symbolic link points to missing file

I guess this is a permission issue, so I tried to run it in command line with sudo su:
I no longer have this error:

tar: Exiting with failure status due to previous errors

but I still have this one with the mysql:
(DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')")

/home/pityu/Desktop/ftpbackup.sh: 51: -u: not found

The ftp successfully uploads all the files to my ftp server from the server from which I run the backup, can open the tar.gz file, just when I unpack it I have this strange error with .version :S

It just won't send me emails. I have Ubuntu server edition, the newest, which had not mail command, so I installed sendmail and some stuff.

I dont have mysql preinstalled, maybe thats the line 55 problem, I have Lampp (Xampp).

Thanks in advance for any help or suggestion.


Here is the full code:

#!/bin/sh
# System + MySQL backup script
# Full backup day - Sat (rest of the day do incremental backup)
# Copyright (c) 2005-2006 nixCraft <http://www.cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# Automatically generated by http://bash.cyberciti.biz/backup/wizard-ftp-script.php
# ---------------------------------------------------------------------

### System Setup ###
DIRS="/opt/lampp/htdocs"
BACKUP=/tmp/backup.$$
NOW=$(date +"%d-%m-%Y")
INCFILE="/root/tar-inc-backup.dat"
DAY=$(date +"%a")
FULLBACKUP="Sat"

### MySQL Setup ###
MUSER="myuser"
MPASS="mypass"
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"

### FTP server Setup ###
FTPD="/_server_backup/incremental"
FTPU="ftpuser"
FTPP="ftppass"
FTPS="ftpip"
NCFTP="$(which ncftpput)"

### Other stuff ###
EMAILID="me@emailprovider.com"

### Start Backup for file system ###
[ ! -d $BACKUP ] && mkdir -p $BACKUP || :

### See if we want to make a full backup ###
if [ "$DAY" == "$FULLBACKUP" ]; then
  FTPD="/_server_backup/full"
  FILE="fs-full-$NOW.tar.gz"
  tar -zcvf $BACKUP/$FILE $DIRS
else
  i=$(date +"%Hh%Mm%Ss")
  FILE="fs-i-$NOW-$i.tar.gz"
  tar -c $INCFILE -zcvf $BACKUP/$FILE $DIRS
fi

### Start MySQL Backup ###
# Get all databases name
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
do
 FILE=$BACKUP/mysql-$db.$NOW-$(date +"%T").gz
 $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done

### Dump backup using FTP ###
#Start FTP backup using ncftp
ncftp -u"$FTPU" -p"$FTPP" $FTPS<<EOF
mkdir $FTPD
mkdir $FTPD/$NOW
cd $FTPD/$NOW
lcd $BACKUP
mput *
quit
EOF

### Find out if ftp backup failed or not ###
if [ "$?" == "0" ]; then
 rm -f $BACKUP/*
else
 T=/tmp/backup.fail
 echo "Date: $(date)">$T
 echo "Hostname: $(hostname)" >>$T
 echo "" >>$T
 echo "This email is to inform you that the latest backup was a success" >>$T
 mail  -s "[dev.bestudion.net] - Backup Successful" "$EMAILID" $T
 echo "Hostname: $(hostname)" >>$T
 echo "" >>$T
 echo "This email is to inform you that the latest backup has failed" >>$T
 echo "" >>$T
 echo "Please investigate why the backup failed. The backup files are still available at $BACKUP" >>$T
 echo "" >>$T
 echo "Don't forget to delete this directory when done" >>$T
 mail -s "[dev.bestudion.net] - Backup Failed" "$EMAILID" <$T
 rm -f $T
fi
  • The '|| :' in line 36 is a bit odd, but is probably valid syntax.
  • Does the script behave any differently when you run it with bash (or change the first line to '#! /bin/bash'? You might be using bash-specific syntax and /bin/sh might not be a symlink to bash.
  • Be sure no vars need to be quoted.
  • Run the script with 'bash -x' to see if anything unusual is happening.

Otherwise, I don't see anything egregious. There are some things that could be done 'neater' but what you have looks OK.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.