1

I am currently using MAMP pro and i want to create a simple terminal script (or a .bat file like on the PC) which allows me to create a sql dump (where i go in and preconfigure the database name) and it automatically creates me the .sql file.

This is what i know so far:

1: The script should first visit cd /applications/MAMP/library/bin

2: When it in that directory, it should run the following

./mysqldump -u root -p databaseName > /Applications/MAMP/htdocs/website/db.sql

Finally, in that directory, when i click the script, it should not replace the older file, but create a new one, maybe just add the date or a integer after it.

Is that possible?

Apologies, i have no idea the best way of doing this.

Thanks in advance.

3 Answers 3

2

You can have this:

#!/bin/sh
TS=$(exec date '+%s')
cd /applications/MAMP/library/bin && \
    ./mysqldump -u root -p databaseName > "/Applications/MAMP/htdocs/website/db.$TS.sql"

It would create files in the form of db.(timestamp).sql.

If you want to have other forms, just changhe the date command. '+%s' specifies the format to produce which is the timestamp. You can see other formats with date --help or man date.

Sign up to request clarification or add additional context in comments.

3 Comments

What's the advantage of $(exec date...) instead of $(date ...) ?
Just optional to prevent extra forking. Without exec the shell makes a process tree of shell->subshell->date while with exec it just becomes shell->date.
Thank you for your explanations! Isn't it a bash construct -- or is it available on plain old sh?
1

Just a variation on the other answer (vote for it!). You don't have to change directory to execute mysqldump :

#!/bin/bash
TS=$(exec date '+%s')
/applications/MAMP/library/bin/mysqldump -u root -p databaseName \
                  > "/Applications/MAMP/htdocs/website/db.$TS.sql"

Please beware that you have both /Application and /application directories. Depending your FS case sensitivity this might be an error.

Comments

1

Are you using the OSx?

#!/bin/bash
cd /applications/MAMP/library/bin && mysqldump -u root -p databaseName >   /Applications/MAMP/htdocs/website/db.sql

After this, you'll need set the permission to your script:

chmod +x your_script_file

Now, you can call...

./your_script_file

or

sh yout_script_file

If you want to replace the old file, this command will do this, because the new file name is the same the older file name

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.