LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 12-03-2007, 07:37 PM   #1
nny0000
Member
 
Registered: Aug 2003
Distribution: Slackware, Ubuntu
Posts: 158

Rep: Reputation: 30
Need help with a script. MySQL and Sed


In an effort to ease some of my web development steps I plan on creating a script to automate everything. I run a drupal site and I need to set up a development environment quickly after I shut down the site for maintenance.

These are the steps that I need verified:

Steps to prepare the development environment
1. Drop all tables in my development Database (MySQL)
2. Remove all drupal files from development directory

Copying development site over to development
3. Copy main DB into dev DB
4. Copy drupal files to dev directory

Setting up the environment for testing
5. use sed (or a find and replace prog) to change $db_url = 'mysql://username: password@localhost/livedatabase'; to $db_url = 'mysql://username: password@localhost/devdatabase'; in drupal settings.php

6. Since the development site is accessible from the internet, I need to add some security to the new dev .htaccess file. I figured I could just add:

AuthUserFile /full/path/to/.htpasswd
AuthType Basic
AuthName "Development"
Require valid-user

to a .txt file and use cat to dump it at the end of .htaccess.

So this is what I have so far, any help would be greatly appreciated.

Also I have Dreamhost so localhost will not work with connecting to MySQL databases I have to use something like 'mysql.sitename.com'

#!/bin/bash

mysqldump -u[USERNAME] -p[PASSWORD] -h [HOSTNAME] --add-drop-table --no-data [DATABASE] | grep ^DROP | mysql -u[USERNAME] -p[PASSWORD] [DATABASE]
cd ~/devsite // safety measure for next line
rm -Rf ./*
$ mysqldump -u[USER] -p[PASSWORD] -h [HOSTNAME][DATABASE] | ssh user@remote.com mysql [DATABASE]
cp ~/livesite/* ~/devsite/

I have no idea how to use sed need advice

cat ~/devsite/.htaccess << ~/htaccess-password.txt


Any advice would be greatly appreciated. Thanks!

Last edited by nny0000; 12-03-2007 at 07:38 PM.
 
Old 12-03-2007, 08:20 PM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
For appending to ~/devsite/.htaccess, you could use
Code:
cat >> ~/devsite/.htaccess << EOF
AuthUserFile /full/path/to/.htpasswd
AuthType Basic
AuthName "Development"
Require valid-user
EOF
You don't need a separate file. With htaccess-password.txt, you could simply use:
cat htaccess-password.txt >> ~/devsite/.htaccess

For sed:
sed -i '/db_url = /s/livedatabase/devdatabase/' $file

You could have a list of files as well in the argument. This list might come from a find connand, and you could use xargs to add the files matching the search criteria to the end of the sed argument. The "-i" option to sed will cause each file to be edited in place instead of catenated together. Otherwise you would need to process each file in a loop and use a temporary file for the results, to prevent clobbering the original file. ( note: the -i option may not be available in some non-gnu versions of sed. )
 
Old 12-03-2007, 08:42 PM   #3
nny0000
Member
 
Registered: Aug 2003
Distribution: Slackware, Ubuntu
Posts: 158

Original Poster
Rep: Reputation: 30
Now that I think of it, I could just just copy the live sites settings.php file into another folder, mark it up the way I want and copy it into the development site via script. I would just have to check it on software upgrades. Much easier than using sed.

Thanks for all the help
 
Old 12-03-2007, 10:00 PM   #4
nny0000
Member
 
Registered: Aug 2003
Distribution: Slackware, Ubuntu
Posts: 158

Original Poster
Rep: Reputation: 30
Ok well that didnt work as planned. The only issue is emptying the devsite database; I get 'mysqldump:error 2002 (HY000)' and 'mysqldump:got errno 32 on write'. Everything else works to my knowledge except emptying the devsite database

Here is the command that isnt working:

mysqldump -u [USERNAME] -p[PASSWORD] -h [HOSTNAME] --add-drop-table --no-data [DATABASE] | grep ^DROP | mysql -u[USERNAME] -p[PASSWORD] [DATABASE]

Does anyone have a solution to get mysqldump to enter the DB and delete everything in it, while retaining the database?

Thanks!
 
Old 12-04-2007, 08:10 AM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Are you trying to use the output of grep ^DROP to the third command? If so use xargs.

However, I don't know if mysql will take multiple lines of input after the command.

Would 'DROP db_name.*;' work if you logged in without specifying a database?

If not, you could use:
mysqldump -u [USERNAME] -p[PASSWORD] -h [HOSTNAME] --add-drop-table --no-data [DATABASE] | grep ^DROP >temp
mysql -u[USERNAME] -p[PASSWORD] [DATABASE] <temp
 
  


Reply

Tags
bash, script, scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SED Script question Andrew_OC Linux - Newbie 1 11-24-2006 12:21 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
sed script cranium2004 Programming 4 06-21-2006 09:33 AM
sed script glam1 Programming 2 08-31-2004 11:28 AM
sed script help/ideas rjcrews Programming 2 07-30-2004 02:59 PM


All times are GMT -5. The time now is 10:36 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration