Hi Everyone

Hope this script can be helpful to someone. Basically it dumps the daily CDR records to a file, archive’s it with ZIP and then ftp’s it to an ftp server using NCFTP.

First install NCFTP client by following instructions in www.ncftp.com/download/

Then, create folder /tmp/backup and assign the appropriate write rights to the folder for your user.

Create the script below in the /usr/sbin folder, let’s call the script ftpDailyBackup.sh

Don’t forget to change the details applicable to your environment.

### Script Start

### System Setup ###
BACKUP=/tmp/backup
NOW=$(date +”%Y-%m-%d”)

### MySQL Setup ###
MUSER=”user”
MPASS=”password”

### FTP server Setup ###
FTPU=”user”
FTPP=”password”
FTPS=”192.168.0.1″

### Other stuff ###
EMAILID=” me@work.com

### Start MySQL Backup ###
FILE=$BACKUP/qcs-$NOW.txt

mysqldump -u $MUSER -p$MPASS –fields-enclosed-by=\” –fields-terminated-by=’,’ –tab=$BACKUP asteriskcdrdb cdr –where=”calldate > ‘$NOW 00:00:00′”

mv $BACKUP/cdr.txt $FILE

### Archive File
zip -r9 $FILE.zip $FILE

### Dump backup using FTP ###
ncftp -u”$FTPU” -p”$FTPP” $FTPS<<EOF
lcd $BACKUP
mput $FILE.zip
quit
EOF

### Find out if ftp backup failed or not ###
if [ &quot;$?” == “0” ]; then
rm -f $FILE.zip
rm -f $FILE
rm -f $BACKUP/cdr.sql
rm -f $BACKUP/cdr.txt
else
T=/tmp/backup.fail
echo “Date: $(date)”>$T
echo “Hostname: $(hostname)” >>$T
echo “Backup failed” >>$T
mail -s “BACKUP FAILED” “$EMAILID” <$T
rm -f $T
fi

### Script End

Finally schedule a cron job to execute after hours, lets make it 23:00 at night.
# crontab –e

Ener the line:
59 22 * * * sh /usr/sbin/ftpDailyBackup.sh

Regards
Quentin