WebFaction
Community site: login faq

Hi I am wondering why this script fails:

#!/bin/bash
# set env variables for this script
 DATESTAMP=$(date +%Y-%m-%d)
 USER="fakeuser"
 PASSWORD="helloXRATEDpassword"
 OUTPUTDIR="~/backupscripts/test/sql"  
 MYSQLDUMP="/usr/bin/mysqldump"
 MYSQL="/usr/bin/mysql"
# get a list of databases
 databases=`$MYSQL --user=$USER --password=$PASSWORD \
 -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
# dump each database in turn and compress it
 for db in $databases; do
 echo $db
 $MYSQLDUMP --force --opt --user=$USER --password=$PASSWORD \
 --databases $db > "$OUTPUTDIR/$db-$DATESTAMP.bak"
 $BZIP  "$OUTPUTDIR/$db-$DATESTAMP.bak"
done

the errors are:

database_txp

backup.sh: line 15: ~/backupscripts/test/sql/database_txp-2014-07-23.bak: No such file or directory
backup.sh: line 17: ~/backupscripts/test/sql/database_txp-2014-07-23.bak: No such file or directory

asked 23 Jul '14, 19:58

baba
47724
accept rate: 0%

edited 24 Jul '14, 03:06

ryans ♦♦
5.0k93260


Two problems:

  • Tilde (~) expansion doesn't work in bash variables, and...
  • You didn't define the value of BZIP.

The following script should work, and has a better approach to output formatting when you grab the DB list:

#!/bin/bash
# set env variables for this script
 DATESTAMP=$(date +%Y-%m-%d)
 USER="fakeuser"
 PASSWORD="helloXRATEDpassword"
 OUTPUTDIR="$HOME/backupscripts/test/sql"
 MYSQLDUMP="/usr/bin/mysqldump"
 MYSQL="/usr/bin/mysql"
 BZIP="/usr/bin/bzip2"
# get a list of databases
 databases=`$MYSQL --user=$USER --password=$PASSWORD \
 -e "SHOW DATABASES;" -B --skip-column-names | grep -v information_schema`
# dump each database in turn and compress it
 for db in $databases; do
 echo $db
 $MYSQLDUMP --force --opt --user=$USER --password=$PASSWORD \
 --databases $db > "$OUTPUTDIR/$db-$DATESTAMP.bak"
 $BZIP  "$OUTPUTDIR/$db-$DATESTAMP.bak"
done

Hope that helps!

permanent link

answered 24 Jul '14, 17:30

seanf
12.2k41836
accept rate: 37%

Thanks!! worked like a charm! cheers

(24 Jul '14, 19:00) baba
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×36
×7
×1

question asked: 23 Jul '14, 19:58

question was seen: 1,566 times

last updated: 24 Jul '14, 19:00

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM