Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP Constants in Shell script

Options
  • 30-08-2010 10:57pm
    #1
    Registered Users Posts: 213 ✭✭


    I'm trying to set up and semi-automated back up script for WordPress, but I seem to be getting an error and I've no idea what's going wrong:
    dumping database… ./wp-backup: line 72: ${BACKUP_DIR}/${DUMP_NAME}: ambiguous redirect
    mysqldump: unknown option ‘-->′
    failed!


    Here is the Shell script:
    [COLOR=#666666][I]#!/bin/bash[/I][/COLOR]
     
    [COLOR=#666666][I]# Copyright 2008, 2010 Guy Rutenberg <http://www.guyrutenberg.com/contact-me>[/I][/COLOR]
    [COLOR=#666666][I]# WordPress FTP backup 2.0[/I][/COLOR]
    [COLOR=#666666][I]#[/I][/COLOR]
    [COLOR=#666666][I]# Easily backup wordpress instances via ftp.[/I][/COLOR]
    [COLOR=#666666][I]#[/I][/COLOR]
    [COLOR=#666666][I]# Change Log:[/I][/COLOR]
    [COLOR=#666666][I]# ===========[/I][/COLOR]
    [COLOR=#666666][I]# 2.0:[/I][/COLOR]
    [COLOR=#666666][I]#  - Auto-detect database settings.[/I][/COLOR]
    [COLOR=#666666][I]#  - Exclude cache data from backups.[/I][/COLOR]
     
    [COLOR=#007800]BACKUP_DIR[/COLOR]=
    [COLOR=#007800]FTP_HOST[/COLOR]=
    [COLOR=#007800]FTP_USER[/COLOR]=
    [COLOR=#007800]FTP_PASS[/COLOR]=
    [COLOR=#007800]FTP_BACKUP_DIR[/COLOR]=
     
    [COLOR=#666666][I]# end of configuration - you probably don't need to touch anything bellow[/I][/COLOR]
     
    [COLOR=#007800]PROG[/COLOR]=[COLOR=#000000][B]`[/B][/COLOR][COLOR=#C20CB9][B]basename[/B][/COLOR] [COLOR=#FF0000]"$0"[/COLOR][COLOR=#000000][B]`[/B][/COLOR]
    print_usage [COLOR=#7A0874][B]([/B][/COLOR][COLOR=#7A0874][B])[/B][/COLOR] [COLOR=#7A0874][B]{[/B][/COLOR]
        [COLOR=#7A0874][B]echo[/B][/COLOR] [COLOR=#FF0000]"USAGE: [COLOR=#007800]${PROG}[/COLOR] [options] BLOG_ROOT"[/COLOR]
        [COLOR=#7A0874][B]echo[/B][/COLOR] [COLOR=#FF0000]"Backup a WordPress blog"[/COLOR]
    [COLOR=#7A0874][B]}[/B][/COLOR]
     
    print_help [COLOR=#7A0874][B]([/B][/COLOR][COLOR=#7A0874][B])[/B][/COLOR]  [COLOR=#7A0874][B]{[/B][/COLOR]
        print_usage
        [COLOR=#C20CB9][B]cat[/B][/COLOR] [COLOR=#CC0000][I]<< EOF
     
    Options:
        -h, --help          show this help message and exit
     
    EOF[/I][/COLOR]
    [COLOR=#7A0874][B]}[/B][/COLOR]
     
    [COLOR=#007800]TEMP[/COLOR]=[COLOR=#000000][B]`[/B][/COLOR][COLOR=#C20CB9][B]getopt[/B][/COLOR] [COLOR=#660033]-o[/COLOR] h [COLOR=#660033]--long[/COLOR] [COLOR=#7A0874][B]help[/B][/COLOR] [COLOR=#660033]-n[/COLOR] [COLOR=#FF0000]"[COLOR=#007800]$PROG[/COLOR]"[/COLOR] [COLOR=#660033]--[/COLOR] [COLOR=#FF0000]"$@"[/COLOR][COLOR=#000000][B]`[/B][/COLOR]
    [COLOR=#000000][B]if[/B][/COLOR] [COLOR=#7A0874][B]([/B][/COLOR][COLOR=#7A0874][B]([/B][/COLOR][COLOR=#007800]$?[/COLOR][COLOR=#7A0874][B])[/B][/COLOR][COLOR=#7A0874][B])[/B][/COLOR]; [COLOR=#000000][B]then[/B][/COLOR]
        print_usage
        [COLOR=#7A0874][B]exit[/B][/COLOR] [COLOR=#000000]1[/COLOR]
    [COLOR=#000000][B]fi[/B][/COLOR]
     
    [COLOR=#7A0874][B]eval[/B][/COLOR] [COLOR=#000000][B]set[/B][/COLOR] [COLOR=#660033]--[/COLOR] [COLOR=#FF0000]"[COLOR=#007800]$TEMP[/COLOR]"[/COLOR]
     
    [COLOR=#000000][B]while[/B][/COLOR] [COLOR=#C20CB9][B]true[/B][/COLOR] ; [COLOR=#000000][B]do[/B][/COLOR]
        [COLOR=#000000][B]case[/B][/COLOR] [COLOR=#FF0000]"$1"[/COLOR] [COLOR=#000000][B]in[/B][/COLOR]
            -h[COLOR=#000000][B]|[/B][/COLOR]--help[COLOR=#7A0874][B])[/B][/COLOR] print_help; [COLOR=#7A0874][B]exit[/B][/COLOR] [COLOR=#000000][B];;[/B][/COLOR]
            --[COLOR=#7A0874][B])[/B][/COLOR] [COLOR=#7A0874][B]shift[/B][/COLOR]; [COLOR=#7A0874][B]break[/B][/COLOR][COLOR=#000000][B];;[/B][/COLOR]
        [COLOR=#000000][B]esac[/B][/COLOR]
    [COLOR=#000000][B]done[/B][/COLOR]
     
    [COLOR=#000000][B]if[/B][/COLOR] [COLOR=#7A0874][B][[/B][/COLOR] [COLOR=#660033]-z[/COLOR] [COLOR=#FF0000]"$1"[/COLOR] [COLOR=#7A0874][B]][/B][/COLOR]
    [COLOR=#000000][B]then[/B][/COLOR]
     print_usage [COLOR=#000000][B]>[/B][/COLOR] [COLOR=#000000][B]/[/B][/COLOR]dev[COLOR=#000000][B]/[/B][/COLOR]stderr
     [COLOR=#7A0874][B]exit[/B][/COLOR] [COLOR=#000000]1[/COLOR]
    [COLOR=#000000][B]fi[/B][/COLOR]
     
    [COLOR=#007800]BLOG_DIR[/COLOR]=$[COLOR=#000000]1[/COLOR]
    [COLOR=#007800]DB_NAME[/COLOR]=[COLOR=#000000][B]`[/B][/COLOR][COLOR=#7A0874][B]echo[/B][/COLOR] [COLOR=#FF0000]"<?php require_once([COLOR=#000099][B]\"[/B][/COLOR][COLOR=#007800]${BLOG_DIR}[/COLOR]/wp-config.php[COLOR=#000099][B]\"[/B][/COLOR]); echo DB_NAME;"[/COLOR] [COLOR=#000000][B]|[/B][/COLOR] php[COLOR=#000000][B]`[/B][/COLOR]
    [COLOR=#007800]DB_USER[/COLOR]=[COLOR=#000000][B]`[/B][/COLOR][COLOR=#7A0874][B]echo[/B][/COLOR] [COLOR=#FF0000]"<?php require_once([COLOR=#000099][B]\"[/B][/COLOR][COLOR=#007800]${BLOG_DIR}[/COLOR]/wp-config.php[COLOR=#000099][B]\"[/B][/COLOR]); echo DB_USER;"[/COLOR] [COLOR=#000000][B]|[/B][/COLOR] php[COLOR=#000000][B]`[/B][/COLOR]
    [COLOR=#007800]DB_PASS[/COLOR]=[COLOR=#000000][B]`[/B][/COLOR][COLOR=#7A0874][B]echo[/B][/COLOR] [COLOR=#FF0000]"<?php require_once([COLOR=#000099][B]\"[/B][/COLOR][COLOR=#007800]${BLOG_DIR}[/COLOR]/wp-config.php[COLOR=#000099][B]\"[/B][/COLOR]); echo DB_PASSWORD;"[/COLOR] [COLOR=#000000][B]|[/B][/COLOR] php[COLOR=#000000][B]`[/B][/COLOR]
    [COLOR=#007800]DB_HOST[/COLOR]=[COLOR=#000000][B]`[/B][/COLOR][COLOR=#7A0874][B]echo[/B][/COLOR] [COLOR=#FF0000]"<?php require_once([COLOR=#000099][B]\"[/B][/COLOR][COLOR=#007800]${BLOG_DIR}[/COLOR]/wp-config.php[COLOR=#000099][B]\"[/B][/COLOR]); echo DB_HOST;"[/COLOR] [COLOR=#000000][B]|[/B][/COLOR] php[COLOR=#000000][B]`[/B][/COLOR]
     
    [COLOR=#007800]BLOG_DIR[/COLOR]=[COLOR=#000000][B]`[/B][/COLOR][COLOR=#C20CB9][B]dirname[/B][/COLOR] [COLOR=#FF0000]"[COLOR=#007800]$BLOG_DIR[/COLOR]"[/COLOR][COLOR=#000000][B]`/`[/B][/COLOR][COLOR=#C20CB9][B]basename[/B][/COLOR] [COLOR=#FF0000]"[COLOR=#007800]$BLOG_DIR[/COLOR]"[/COLOR][COLOR=#000000][B]`[/B][/COLOR]
    [COLOR=#007800]BACKUP_DIR[/COLOR]=[COLOR=#000000][B]`[/B][/COLOR][COLOR=#C20CB9][B]dirname[/B][/COLOR] [COLOR=#FF0000]"[COLOR=#007800]$BACKUP_DIR[/COLOR]"[/COLOR][COLOR=#000000][B]`/`[/B][/COLOR][COLOR=#C20CB9][B]basename[/B][/COLOR] [COLOR=#FF0000]"[COLOR=#007800]$BACKUP_DIR[/COLOR]"[/COLOR][COLOR=#000000][B]`[/B][/COLOR]
     
    [COLOR=#7A0874][B]echo[/B][/COLOR] [COLOR=#660033]-n[/COLOR] [COLOR=#FF0000]"dumping database... "[/COLOR]
    [COLOR=#007800]DUMP_NAME[/COLOR]=[COLOR=#800000]${DB_NAME}[/COLOR]-$[COLOR=#7A0874][B]([/B][/COLOR][COLOR=#C20CB9][B]date[/B][/COLOR] +[COLOR=#000000][B]%[/B][/COLOR]Y[COLOR=#000000][B]%[/B][/COLOR]m[COLOR=#000000][B]%[/B][/COLOR]d[COLOR=#7A0874][B])[/B][/COLOR].sql.bz2
    mysqldump [COLOR=#660033]--user[/COLOR]=[COLOR=#800000]${DB_USER}[/COLOR] [COLOR=#660033]--password[/COLOR]=[COLOR=#800000]${DB_PASS}[/COLOR] [COLOR=#660033]--host[/COLOR]=[COLOR=#800000]${DB_HOST}[/COLOR] \
     [COLOR=#660033]--databases[/COLOR] [COLOR=#800000]${DB_NAME}[/COLOR] \
     [COLOR=#000000][B]|[/B][/COLOR] [COLOR=#C20CB9][B]bzip2[/B][/COLOR] [COLOR=#660033]-c[/COLOR] [COLOR=#000000][B]>[/B][/COLOR] [COLOR=#800000]${BACKUP_DIR}[/COLOR][COLOR=#000000][B]/[/B][/COLOR][COLOR=#800000]${DUMP_NAME}[/COLOR]
    [COLOR=#000000][B]if[/B][/COLOR] [COLOR=#7A0874][B][[/B][/COLOR] [COLOR=#FF0000]"$?"[/COLOR] [COLOR=#660033]-ne[/COLOR] [COLOR=#FF0000]"0"[/COLOR] [COLOR=#7A0874][B]][/B][/COLOR]; [COLOR=#000000][B]then[/B][/COLOR]
    	[COLOR=#7A0874][B]echo[/B][/COLOR] [COLOR=#FF0000]"failed!"[/COLOR]
    	[COLOR=#7A0874][B]exit[/B][/COLOR] [COLOR=#000000]1[/COLOR]
    [COLOR=#000000][B]fi[/B][/COLOR]
    [COLOR=#7A0874][B]echo[/B][/COLOR] [COLOR=#FF0000]"done"[/COLOR]
     
    [COLOR=#7A0874][B]echo[/B][/COLOR] [COLOR=#660033]-n[/COLOR] [COLOR=#FF0000]"Creating tarball... "[/COLOR]
    [COLOR=#007800]TAR_NAME[/COLOR]=[COLOR=#800000]${BLOG_DIR##*/}[/COLOR]-$[COLOR=#7A0874][B]([/B][/COLOR][COLOR=#C20CB9][B]date[/B][/COLOR] +[COLOR=#000000][B]%[/B][/COLOR]Y[COLOR=#000000][B]%[/B][/COLOR]m[COLOR=#000000][B]%[/B][/COLOR]d[COLOR=#7A0874][B])[/B][/COLOR].tar.bz2
    [COLOR=#C20CB9][B]tar[/B][/COLOR] [COLOR=#660033]-cjf[/COLOR] [COLOR=#800000]${BACKUP_DIR}[/COLOR][COLOR=#000000][B]/[/B][/COLOR][COLOR=#800000]${BLOG_DIR##*/}[/COLOR]-$[COLOR=#7A0874][B]([/B][/COLOR][COLOR=#C20CB9][B]date[/B][/COLOR] +[COLOR=#000000][B]%[/B][/COLOR]Y[COLOR=#000000][B]%[/B][/COLOR]m[COLOR=#000000][B]%[/B][/COLOR]d[COLOR=#7A0874][B])[/B][/COLOR].tar.bz2 [COLOR=#660033]--exclude[/COLOR] cache [COLOR=#800000]${BLOG_DIR}[/COLOR]
    [COLOR=#000000][B]if[/B][/COLOR] [COLOR=#7A0874][B][[/B][/COLOR] [COLOR=#FF0000]"$?"[/COLOR] [COLOR=#660033]-ne[/COLOR] [COLOR=#FF0000]"0"[/COLOR] [COLOR=#7A0874][B]][/B][/COLOR]; [COLOR=#000000][B]then[/B][/COLOR]
    	[COLOR=#7A0874][B]echo[/B][/COLOR] [COLOR=#FF0000]"failed!"[/COLOR]
    	[COLOR=#7A0874][B]exit[/B][/COLOR] [COLOR=#000000]2[/COLOR]
    [COLOR=#000000][B]fi[/B][/COLOR]
    [COLOR=#7A0874][B]echo[/B][/COLOR] [COLOR=#FF0000]"done"[/COLOR]
     
    [COLOR=#7A0874][B]echo[/B][/COLOR] [COLOR=#660033]-n[/COLOR] [COLOR=#FF0000]"Uploading SQL dump and tarball to FTP... "[/COLOR]
    lftp [COLOR=#660033]-u[/COLOR] [COLOR=#800000]${FTP_USER}[/COLOR],[COLOR=#800000]${FTP_PASS}[/COLOR] [COLOR=#800000]${FTP_HOST}[/COLOR] [COLOR=#CC0000][I]<<EOF
    cd "${FTP_BACKUP_DIR}"
    put "${BACKUP_DIR}/${DUMP_NAME}"
    put "${BACKUP_DIR}/${TAR_NAME}"
     
    EOF[/I][/COLOR]
    [COLOR=#000000][B]if[/B][/COLOR] [COLOR=#7A0874][B][[/B][/COLOR] [COLOR=#FF0000]"$?"[/COLOR] [COLOR=#660033]-ne[/COLOR] [COLOR=#FF0000]"0"[/COLOR] [COLOR=#7A0874][B]][/B][/COLOR]; [COLOR=#000000][B]then[/B][/COLOR]
    	[COLOR=#7A0874][B]echo[/B][/COLOR] [COLOR=#FF0000]"failed!"[/COLOR]
    	[COLOR=#7A0874][B]exit[/B][/COLOR] [COLOR=#000000]3[/COLOR]
    [COLOR=#000000][B]fi[/B][/COLOR]
    [COLOR=#7A0874][B]echo[/B][/COLOR] [COLOR=#FF0000]"done"[/COLOR]
    
    

    I tried supressing the headers by using `php -q` instead of just `php`, but it didn't help at all...
    Thought it might be because of } being a symbol in the password, but using credentials without any symbols didn't help either, so I'm a little lost.

    Any help would be appreciated.

    Thanks!


Comments

  • Registered Users Posts: 3,721 ✭✭✭E39MSport


    This looks dodgy to me: -

    BACKUP_DIR=
    BACKUP_DIR=`dirname "$BACKUP_DIR"`/`basename "$BACKUP_DIR"`

    mysqldump --user=${DB_USER} --password=${DB_PASS} --host=${DB_HOST} \
    --databases ${DB_NAME} \
    | bzip2 -c > ${BACKUP_DIR}/${DUMP_NAME}

    BACKUP_DIR is empty and you're doing a dirname and basename against it then using this for your redirect.

    Comment out the last line and see if it works.
    #\ | bzip2 -c > ${BACKUP_DIR}/${DUMP_NAME}


  • Registered Users Posts: 213 ✭✭Hoku


    Sorry, that would be filled in when I actually run the script.
    I copied the script without filling any values for this post, but on the server BACKUP_DIR and the FTP_* are filled in.

    Commenting out that line didn't help. Still:
    dumping database... mysqldump: unknown option '-->'
    failed!


  • Registered Users Posts: 6,509 ✭✭✭daymobrew


    Maybe you could write the entire backup code in php instead of a mix of shell script and php.

    If this is not possible, invoke debugging by changing the script's first line to:
    #!/bin/bash -x
    
    You should be able to see where the problems start.


  • Registered Users Posts: 213 ✭✭Hoku


    Debugging worked :)
    Turns out there was an out-of-date cache plugin, at least one of the files wasn't updated properly, so it threw out an error.

    Script works fine now!
    Thanks!


Advertisement