Results 1 to 9 of 9
  1. #1

    Please review my backup script

    I would appreciate if you could take a look at my database backup script and give me some feedback on it. The script does a mysql dump of a 500 meg database, gzips it and FTPs the database to another host. The script takes about 3-4 minutes to run, with the backup taking 2 minutes and the FTP taking 1 minute. I have been running the script through SSH and will have the script run everyday with a cron. When running it through SSH, I get the following output from the script:

    Code:
    -jailshell-2.05b$ ./bkscript
    AUTH not understood
    AUTH not understood
    KERBEROS_V4 rejected as an authentication type
    output to local-file: remote.txt? AUTH not understood
    AUTH not understood
    KERBEROS_V4 rejected as an authentication type
    output to local-file: remote.txt? -jailshell-2.05b$
    Is this OK? This script is run on a shared hosting account. Please give me some comments or suggestions on how to improve it. Thank you.

    Code:
    #!/bin/bash
    
    
    # Script parameters
    #backup file prefix
    BKNAME='backup'
    #number of backups to keep
    NUMTOKEEP=2
    #list of local backups
    BKLOG=bk.log
    #script logfile
    MSGLOG=message.log
    
    #local DB parameters
    # MySQL Hostname
    DBHOST='localhost'
    # MySQL Username
    DBUSER='user'
    # MySQL Password
    DBPASSWD='pass'
    # MySQL Database
    DBNAME='db'
    
    
    #remote host parameters
    # FTP Hostname
    FTPHOST='host.com'
    # FTP Username
    FTPUSER='root' 
    # FTP Password
    FTPPASSWD='pass'
    # Remote Directory for Offsite Backup
    REMOTEDIR='backups'
    
    #Start script
    DATE=`date "+%Y-%m-%d-%T"`
    echo "Start Backup Script - $DATE"   > $MSGLOG
    
    #Delete old backup if necessary
    ls -tr $BKNAME* > $BKLOG
    
    LINECNT=`cat $BKLOG | wc -l`
    
    if [ $LINECNT -ge $NUMTOKEEP ]; then
    
        echo "Backups in directory" 		>> $MSGLOG
        echo "---------------------------------"  	>> $MSGLOG
        more $BKLOG  				>> $MSGLOG
        echo "---------------------------------" 	>> $MSGLOG
        echo "Backup to delete:"			>> $MSGLOG
        BKDEL=`head -n1 $BKLOG`
        echo $BKDEL  				>> $MSGLOG
        rm -f $BKDEL
    
    else
        echo "No backups to delete"  		>> $MSGLOG
        echo "Backups in directory"  		>> $MSGLOG
        echo "---------------------------------"  	>> $MSGLOG
        more $BKLOG  				>> $MSGLOG
        echo "---------------------------------"  	>> $MSGLOG
    fi
    
    #Start mysql dump
    DATE=`date "+%Y-%m-%d-%T"`
    echo "Start mysqldump - $DATE"   		>> $MSGLOG
    
    FILENAME="${BKNAME}-${DATE}.sql.gz"
    mysqldump -u$DBUSER -p$DBPASSWD --add-locks --all --extended-insert --quick --compress $DBNAME | gzip > $FILENAME
    
    echo "New Backup: $FILENAME"			>> $MSGLOG
    
    
    DATE=`date "+%Y-%m-%d-%T"`
    echo "End mysqldump - $DATE" 			>> $MSGLOG
    
    #get file listing on remote host
    echo "FTP transfer start"   			>> $MSGLOG
    
    ftp -n $FTPHOST <<END_SCRIPT
    quote USER $FTPUSER
    quote PASS $FTPPASSWD
    cd $REMOTEDIR
    mls *.* remote.txt
    quit
    END_SCRIPT
    
    #delete backup on remote host and FTP new backup from localhost
    REMOTEDEL=`head -n1 remote.txt`
    
    ftp -n $FTPHOST <<END_SCRIPT
    quote USER $FTPUSER
    quote PASS $FTPPASSWD
    cd $REMOTEDIR
    delete $REMOTEDEL
    put $FILENAME
    mls *.* remote.txt
    quit
    END_SCRIPT
    
    FTPFILE=`cat remote.txt`
    
    ls -al $BKNAME* > $BKLOG
    
    echo "Backups in directory"  			>> $MSGLOG
    echo "---------------------------------"  	>> $MSGLOG
    more $BKLOG  					>> $MSGLOG
    echo "---------------------------------" 	>> $MSGLOG
    
    
    DATE=`date "+%Y-%m-%d-%T"`
    echo "End Script - $DATE"   			>> $MSGLOG
    
    #email script logfile
    mail -s "backup" [email protected] < message.log
    
    exit 0

  2. #2
    Join Date
    Dec 2004
    Location
    New York City, NY, USA
    Posts
    735
    Try changing the first line of the program to:

    #!/bin/bash -x

    This will print out the commands as they are executed (proceeded with a "+" character), and you can see what command is causing the problem.

    By the way, you've probably been told this before, but FTP sucks. If you had another server running SSH it'd be more secure (and your script would be shorter, sinc you'd not have to script the FTP comments) to copy using the "scp" command.

  3. #3
    Join Date
    Oct 2002
    Location
    York, United Kingdom
    Posts
    260
    I like it. Do you mine if i use it to make a few back ups on my server? It seems to run fine

  4. #4
    Originally posted by tamasrepus
    Try changing the first line of the program to:

    #!/bin/bash -x

    This will print out the commands as they are executed (proceeded with a "+" character), and you can see what command is causing the problem.

    By the way, you've probably been told this before, but FTP sucks. If you had another server running SSH it'd be more secure (and your script would be shorter, sinc you'd not have to script the FTP comments) to copy using the "scp" command.
    Thanks for taking a look.

  5. #5
    Originally posted by adaml
    I like it. Do you mine if i use it to make a few back ups on my server? It seems to run fine

    Go ahead! If you can see a way to improve it, please update this thread.

  6. #6
    Join Date
    Oct 2002
    Location
    York, United Kingdom
    Posts
    260
    I will see what i come up with Thanks alot Stefan!

  7. #7
    Join Date
    Mar 2004
    Location
    New Zealand
    Posts
    533
    Sorry, did you say that you dump a 500 MEG database, and it takes 2 MINUTES to do that, and more, 1 MINUTE to ftp it to another host?!

    You might want to check that, cause otherwise that's some **** hot server and connection you got there.

  8. #8
    Join Date
    Dec 2002
    Location
    NY, NY
    Posts
    3,975
    it is probably on the same network..
    hopefully.. lol

  9. #9
    Yes, for now its on the same network. Im going to get another account with a different host.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •