MySQL Restoration Backup Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting MySQL Restoration Backup Script
# 1  
Old 06-04-2012
MySQL Restoration Backup Script

Hi there,

Alright I have this line that I'm working with (bash programming):

Code:
mysql -u username -pHASH ${args[1]} < /home/site/backups/site.${args[0]}.sql

I get this error on that line:

Code:
./restore.sh: line 51: syntax error near unexpected token `newline'
./restore.sh: line 51: `mysql -u username -pHASH ${args[1]} < '

Any ideas? Bash programming n00b here Smilie
# 2  
Old 06-04-2012
It might help to see the rest of the script... We have no idea what's in 'args' for instance.

Also, have you been editing this file with Microsoft Notepad? That will fill the file with carriage returns that UNIX can't use. Edit it in a UNIX editor like vi or nano instead.
# 3  
Old 06-04-2012
Here's the script. The problem line is in red (error in first post).

Since I'm a n00b, I welcome any security tips in writing this script as well.

Example usage: ./restore.sh 05-20-12 may20 whereby 05-20-12 is a datestamp of the DB file (i.e. backup.05-20-12.sql) and may20 is the database to-be-created in MySQL (that will hold the restored data).

I've been using TextEdit on my Mac (plaintext format, not rich-text) and copying the script into the file via the nano command. Would that be causing a problem, even if TextEdit is in plain-text editing mode?

Code:
#!/bin/sh

cat <<Header
--------------------------------------------------------------------------
-- Database Restoration Utility
-- v1.1
--------------------------------------------------------------------------

Header

######################################################################
# Restore a database all in one script
######################################################################

# Check args
args=("$@")
DBFILE="/home/site/backups/backup.${args[0]}.sql.gz"
echo Looking for file ${DBFILE}...

# Did we find it?
if [ -f $DBFILE ]; then
	echo "File found."
else
	echo "File does not exist.  Database cannot be restored.  Exiting."
	exit 1
fi

######################################################################
# Log in to MySQL, create a database
######################################################################

# mysql commands here
# [...]

######################################################################
# Do database restoration
######################################################################

echo Restoring and importing database...
gunzip /home/site/backups/backup.${args[0]}.sql.gz
mysql -u username -pHASH ${args[1]} < /home/site/backups/backup.${args[0]}.sql

echo Database successfully imported and restored
echo Backup data resides in database ${args[1]}
echo DONE!

exit 1

# 4  
Old 06-04-2012
That 'args' array is pointless -- especially since you're using /bin/sh, not /bin/bash, and hence are not even guaranteed to have arrays. Just use the already-existing special variables to do that $1 $2 (Note $0 is not the first in the list, $0 is the name the script was called by).

copy-pasting from texedit might cause problems for all I know. It may certainly destroy your formatting, especially tabs. Mostly I see no point doing that in the first place, nano's a perfectly serviceable text editor on its own.
# 5  
Old 06-04-2012
Thanks! Got it. What happened was... when I pasted the text in from TextEdit into the Nano editor, certain lines did indeed get jumbled. Just had to put them all back on the same line. Simple fix!

Thanks for the note on args, I'll be sure to change that.
# 6  
Old 06-08-2012
To avoid linewrapping inside nano, run it like nano -w filename.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX and Linux Applications

MySQL & OpenLDAP - file level backup

Hi All, The MySQL & OpenLDAP database on my Debian are very lightly updated. Usually I tar everything on / including the databases. The restore works OK on another machine. I want to know whether its safe to do file level backup of databases like this ? Do the MySQL & OpenLDAP databases... (0 Replies)
Discussion started by: coolatt
0 Replies

2. Web Development

Restore MySQL backup

Hello all! First posting here! So be patient with me. I made a clean install with MacOS 10.8 and need to restore my Databases from my external backup drive. Apparently it is not possible, to create the MySQL user and password as before, and simply drag the databases from... (12 Replies)
Discussion started by: marek
12 Replies

3. Shell Programming and Scripting

Script - Mysql backup and delete

Hello I have a production mysql server and archive server, unfortunitly its not possible to setup repliacation between the two, the reason is that the archive server is using some fancy storage engine which doesn't allow mysql replication. I'm trying to write a script using perl that does... (1 Reply)
Discussion started by: amlife
1 Replies

4. Shell Programming and Scripting

Need help in creating file restoration script from a backup script.

Hi all i am struggling in creating a restore of env files while doing applications clone. the first file i created for copying the important configurations file which is running perfect now for reverting the changes i mean when i am restoring these files to its original places i have to do... (7 Replies)
Discussion started by: javeedkaleem
7 Replies

5. Shell Programming and Scripting

Help with perl mysql backup script

Hi, im trying to make a script that backups mysql databases but apparently I am having trouble with the variables, or simply something I am missing. Would appreciate any help, here is the script #!/usr/bin/perl -w use strict; require File::Spec; #VARIABLES my $databasename =... (4 Replies)
Discussion started by: Fireline
4 Replies

6. Shell Programming and Scripting

How to backup a particular Database in MYSQL?

Hi All, Thanks in Advance!! How to backup a particular Databases..through Bash script!! For example i have 6 databases; Anish linux Software Questions Rhce Google these are the databases i have from that i want to take "Anish" and "questions" database backup regularly.... (4 Replies)
Discussion started by: anishkumarv
4 Replies

7. Solaris

question about restoration from backup tape (solaris 10)

I am trying to restore opt on my server. my issue is, all the partitions are saved into the same back up tape. what is the exact command to just restore /opt for example, supposing c0t0d0s7 is the partition for /opt ---------- Post updated at 01:16 PM ---------- Previous update was at... (7 Replies)
Discussion started by: feg
7 Replies
Login or Register to Ask a Question