Results 1 to 9 of 9

Thread: [SOLVED] mysqldump within shell script, command not found

  1. #1
    Join Date
    Jun 2008
    Beans
    110

    [SOLVED] mysqldump within shell script, command not found

    Hi everyone

    Just trying to make a simple shell script to dump a MySQL database using the mysqldump command

    I've got the command all set:
    Code:
    mysqldump --host=localhost --user=USERNAME --pass=PASSWORD --comments --create-options --extended-insert --lock-tables --no-create-db --quick --result-file=FILENAME.sql --set-charset --skip-add-drop-table --verbose --databases DATABASE
    The bits in upper case are text that I'll change to the valid values.

    If I type that command into the command line, then it works perfectly.
    However when I copy and paste that into a shell script and run the shell script, it doesn't work. It repeats the whole mysqldump command, including all the options and just says command not found at the end

    So I'm thinking, do I need to enclose that command in quotes or something within a shell script to get it to execute?

    Thanks, B

  2. #2
    Join Date
    Oct 2007
    Location
    London, UK
    Beans
    118
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: mysqldump within shell script, command not found

    You can use Perl:
    Code:
    perl -e 'system("mysqldump --host=localhost --user=USERNAME --pass=PASSWORD --comments --create-options --extended-insert --lock-tables --no-create-db --quick --result-file=FILENAME.sql --set-charset --skip-add-drop-table --verbose --databases DATABASE");'
    At least I remember something like that from when I was programming using perl.

    This could be wrong. But I can't delete the post.
    Last edited by Crandom; October 24th, 2008 at 05:07 PM. Reason: Because it was wrong.
    Ubuntu 9.10 on an Aspire 5103WLMi | AMD Turion TL52 X2 (1.6Ghz @ 1.81 Ghz [Overclock]) | 120 Gb PATA IDE HDD | 2048 Mb DRR2-800 RAM | ATi Mobility Radeon X1300 (fglrx) | Broadcom BCM4318MPG (ndiswrapper + bcmwl5.sys) |

  3. #3
    Join Date
    Jun 2008
    Beans
    110

    Wink Re: mysqldump within shell script, command not found

    Ok I was using nano to edit the file and it seems it keeps breaking the command onto multiple lines.

    In shell scripting, is there anything I need to be aware of or anything I can do to make editing multi-line commands easier?

    Break the command into multiple variables, then concatenate them or something?

    Sorry for the questions, I'm just trying to get used to the basic syntax of shell scripting

    Thanks, B

  4. #4
    Join Date
    Jul 2008
    Beans
    788
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: mysqldump within shell script, command not found

    Quote Originally Posted by batfastad View Post
    Hi everyone

    Just trying to make a simple shell script to dump a MySQL database using the mysqldump command

    I've got the command all set:
    Code:
    mysqldump --host=localhost --user=USERNAME --pass=PASSWORD --comments --create-options --extended-insert --lock-tables --no-create-db --quick --result-file=FILENAME.sql --set-charset --skip-add-drop-table --verbose --databases DATABASE
    The bits in upper case are text that I'll change to the valid values.

    If I type that command into the command line, then it works perfectly.
    However when I copy and paste that into a shell script and run the shell script, it doesn't work. It repeats the whole mysqldump command, including all the options and just says command not found at the end

    So I'm thinking, do I need to enclose that command in quotes or something within a shell script to get it to execute?

    Thanks, B
    Try running the script with --user=mysql
    Any sufficiently advanced bug is indistinguishable from a feature.
    "Ubuntu" - an African word meaning "Gentoo is too hard for me"
    unexpected token <EOF> expected "WITTY ONE-LINER"

  5. #5
    Join Date
    Jun 2008
    Location
    Colombia
    Beans
    443

    Re: mysqldump within shell script, command not found

    You can try
    Code:
    mysqldump -option1=1 \
    -option2=2 \
    -option3=3

  6. #6
    Join Date
    Jul 2008
    Beans
    788
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: mysqldump within shell script, command not found

    Quote Originally Posted by batfastad View Post
    Ok I was using nano to edit the file and it seems it keeps breaking the command onto multiple lines.

    In shell scripting, is there anything I need to be aware of or anything I can do to make editing multi-line commands easier?

    Break the command into multiple variables, then concatenate them or something?

    Sorry for the questions, I'm just trying to get used to the basic syntax of shell scripting

    Thanks, B
    use "bash -c" to execute as a string...will stop it being broken into multiple lines
    Any sufficiently advanced bug is indistinguishable from a feature.
    "Ubuntu" - an African word meaning "Gentoo is too hard for me"
    unexpected token <EOF> expected "WITTY ONE-LINER"

  7. #7
    Join Date
    Jun 2008
    Beans
    110

    Re: mysqldump within shell script, command not found

    Quote Originally Posted by Sarmacid View Post
    You can try
    Code:
    mysqldump -option1=1 \
    -option2=2 \
    -option3=3
    Perfect!
    I can see myself using that syntax quite a bit!

    Thanks for all the help!

  8. #8
    Join Date
    Feb 2007
    Location
    Amsterdam
    Beans
    39
    Distro
    Ubuntu 10.04 Lucid Lynx

    Lightbulb Re: [SOLVED] mysqldump within shell script, command not found

    This is the way my company solved the mysqldump in a shell script:

    Code:
    #!/bin/bash
    #Script to make a regular copy of a mysql database and gzip it into the SAVEDIR.
    
    USER="authorized_user"
    PASSWORD="the_password"
    DATABASE="database_name"
    SAVEDIR="/backup"
    
    /usr/bin/nice -n 19 /usr/bin/mysqldump -u $USER --password=$PASSWORD --default-character-set=utf8 $DATABASE -c | /usr/bin/nice -n 19 /bin/gzip -9 > $SAVEDIR/$DATABASE-$(date '+%Y%m%d-%H').sql.gz
    edit the varibles, save it as .bkup.sh and run it in a crontab, then you have an automatic mysql backup.

    full explaination of the code in the next post
    Experiences flowing into dreams, into experiences.

  9. #9
    Join Date
    Feb 2007
    Location
    Amsterdam
    Beans
    39
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: [SOLVED] mysqldump within shell script, command not found

    Here is a full code explaination of .bkup.sh

    because it's easy to just copy and paste a code, but it's nice to know what the code is doing so you can use pieces in other scripts.


    instructs which shell (command line interpreter) to use (in the example it's the Bourne Again Shell)
    Code:
    #!/bin/bash
    a comment line to say what we are going to do. Comments are marked with a # in front of each line
    Code:
    #Script to make a regular copy of a mysql database and gzip it into the SAVEDIR.
    This section is where we set the variables to be used again in the script

    sets a variable named USER, which you set to the user who is authorized to perform mysqldump.
    Code:
    USER="authorized_user"
    the password of the mysqluser who is allowed to perform a mysqldump
    Code:
    PASSWORD="the_password"

    the name of the database you would like to dump

    Code:
    DATABASE="database_name"
    the directory where you would like to save the backups

    Code:
    SAVEDIR="/backup"
    tells the computer to play nice by letting all other processes use the processor before it gets a go. This will keep the backup from causing system slowdown.

    Code:
    /usr/bin/nice -n 19
    runs the program mysqldump
    Code:
    /usr/bin/mysqldump
    uses the variables that we set earlier for the user and password to provide username and password to mysqldump.
    Code:
     -u $USER --password=$PASSWORD
    Sets the default charachter set of the dumped database to UTF8 (unicode language independant),
    Code:
     --default-character-set=utf8
    uses the databasename from the variables above to tell mysqldump which database to use
    Code:
     $DATABASE
    (-c switch) writes to the output file complete insert statements that include the column names. so you can restore the database easily
    Code:
     -c

    the | means use the output of the first command for the second comand.
    Code:
     |
    nice this time keeps gzip friendly to the system resources
    Code:
    /usr/bin/nice -n 19
    squishes the file into a gzip archive with compression level 9 (highest)
    Code:
    /bin/gzip -9
    the > means write the output to a file
    Code:
    >

    the directory and file name are made from variables we set in the beginning.

    Code:
    $SAVEDIR/$DATABASE-
    part of the file name is a unique date in the ISO format plus the hour of day. (our backup runs once an hour during opening times so we like to know which hour the backup was made)

    Code:
    $(date '+%Y%m%d-%H')
    and of course the file extension .sql (the file is just sql statements)
    Code:
    .sql
    and .gz (to tell us that it's squished i.e. compressed into a gzip archive)
    Code:
    .gz
    that's all there is to it.
    enjoy scripting
    Experiences flowing into dreams, into experiences.

Bookmarks

Posting Permissions

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