Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is it possible to create a .sh shell or .command file with a configuration file?

I need to backup websites via Terminal and be able to put some data to configuration files, and have the shell execute it.

For example, the username, password, ssh id, et cetera.

#!/bin/bash
ssh admin@host.com "tar cjvf webfilesbackup-date-`date +%Y%m%d`.tar.bz2 public_html/"    
ssh admin@host.com "mysqldump -u user_admin -ppass database_1 > databasebackup-`date +%Y%m%d`-db.sql"
scp admin@host.com:webfilesbackup-date-`date +%Y%m%d`.tar.bz2 ~/backup/
scp admin@host.com:databasebackup-`date +%Y%m%d`-db.sql ~/backup/
ssh admin@host.com "rm -f webfilesbackup-date-`date +%Y%m%d`.tar.bz2"
ssh admin@host.com "rm -f databasebackup-`date +%Y%m%d`-db.sql"
share|improve this question

4 Answers 4

     # parse the ini like $0.hostname.conf and set the variables
     # place a $0.`hostname -s`.conf file in the same dir as your script
     # set here any VarName=VarValue
     # use ; for comments - you will get host independant conf file 
     # for your bash scripts - [examle usage :][1] 
     doParseIniFile(){
        eval `sed -e 's/[[:space:]]*\=[[:space:]]*/=/g' \
            -e 's/;.*$//' \
            -e 's/[[:space:]]*$//' \
            -e 's/^[[:space:]]*//' \
            -e "s/^\(.*\)=\([^\"']*\)$/\1=\"\2\"/" \
           < $IniFile \
            | sed -n -e "/^\[$IniSection\]/,/^\s*\[/{/^[^;].*\=.*/p;}"`
     }
     #eof function doParseIniFile
share|improve this answer
    
Would you mind expanding your code-only answer a little to explain what it does? –  slhck Nov 20 '12 at 20:09
mkdir -p Example.app/Contents/MacOS`
cp script.sh Example.app/Contents/MacOS/Example

...and that will work. This article has more detailed instructions, including how to get custom icons.

You might also look to Automator or AppleScript Studio as ways to build applications that can run sanely, without having to do it by hand.

share|improve this answer
    
yes but,i searching solution to manually writ "backup -s1" to terminal –  macman Feb 2 '12 at 18:43

Create a file (e.g. settings) with your settings as variables:

username='foo'
pass='bar'
website='baz'
database='yak'

Then source the file from your script and you will be able to access the variables:

. settings
echo $username     # <- this will print "foo"

Concerning your second "question", if you have multiple hosts you can just have multiple config files and source the one passed as an argument to your script. For example, create a config file called site1.

Call your script with ./backup.sh site1

. $1               # <- this will load the file "site1"
echo $username     # <- this will print "foo"

With your backup script, this would mean:

#!/bin/bash
. $1
ssh admin@host.com "tar cjvf webfilesbackup-date-`date +%Y%m%d`.tar.bz2 public_html/"
ssh admin@host.com "mysqldump -u $user -p$pass $database > databasebackup-`date +%Y%m%d`-db.sql"
# ... and so on
share|improve this answer
3  
I wouldn't append ".sh", since it's not meant to be executed. –  Ignacio Vazquez-Abrams Feb 2 '12 at 13:15
    
If open Terminal.app and write something like this backup -site1 i have lot of queries in config file(site2,site3) with different (ssh,pass) is it possible? –  macman Feb 2 '12 at 13:18
    
@macman -site1 would be an argument to your shell script. If you post the shell script in your question, it'd be easier to answer. –  slhck Feb 2 '12 at 13:50
    
here is a picture diagram Imgur –  macman Feb 2 '12 at 14:26
    
-site1 maybe the parameter –  macman Feb 2 '12 at 14:44

The other option is just to create a text file and use a while loop to access that file line by line.

share|improve this answer
    
it maybe works? {scp $user:webfilesbackup-date-date +%Y%m%d.tar.bz2 ~/backup/} in config file> {$user = 'admin@host.com'} config file is . site.sh how to add this parameter to .command to passing the config file backup -site1 –  macman Feb 2 '12 at 17:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.