Skip to content

Commit

Permalink
more db and user removal support
Browse files Browse the repository at this point in the history
  • Loading branch information
pbredenberg committed Apr 10, 2016
1 parent 43c4f97 commit a525b0b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 23 deletions.
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,40 @@ ldash

LEMPDash must be run as sudo when executing processes on the system level, for instance, when creating or removing nginx server blocks.

As of version (0.0.1), you can only create or remove nginx server blocks.
As of version (0.0.2), you can create or remove nginx server blocks, and MySQL databses/users.

### cr (Create)
Create a thing:
Create a site:
```
ldash cr site mysite.com
```

Create a database:
```
ldash cr db databasename databaseuser databasepassword
```

### rm (Remove)
Remove a thing:
Remove a site:
```
ldash rm site mysite.com
```

Remove a database:
```
ldash rm db databasename
```

Remove a database user:
```
ldash rm dbuser username
```

Note: Remove wants to do its' thing safely, so it will back up your site files automatically using zip, and place the backup in var/www/archive/YOURSITENAME.

## Future Stuff

* Create/remove MySQL databases
* Support MariaDB, MongoDB
* Backups for sites/DBs
* Uninstall script
* Setup options for system specific configurations
Expand Down
34 changes: 28 additions & 6 deletions lempdash
Original file line number Diff line number Diff line change
@@ -1,38 +1,60 @@
#!/usr/bin/env bash
# Display colorized information output
ADDRESSEE="Hey boss, "
DONTF="don't forget to "
AVAIL_OPS="(site, db)"

echo "Hi! I'm LEMPDash 0.0.2!"

if [ $# -eq 0 ]; then
echo "Acceptable arguments: cr, rm"
# CREATION OPERATIONS
elif [ $1 = "cr" ]; then
if [ -z "$2" ]; then
echo "Hey boss, don't forget to specify creation operation: ldash cr site"
echo $ADDRESSEE$DONTF"specify creation operation: ldash cr OPERATION"$AVAIL_OPS
elif [ $2 = "site" ]; then
if [ $# = 3 ]; then
echo "Creating site..." $3
/opt/lempdash/tasks/nginx/createblock "$3"
else
echo "Hey boss, don't forget to specify a site name: ldash cr site SITENAME"
echo $ADDRESSEE$DONTF"specify a site name: ldash cr site SITENAME"
fi
elif [ $2 = "db" ]; then
if [ $# = 5 ]; then
echo "Creating database..." $3
/opt/lempdash/tasks/mysql/createdb "$3" "$4" "$5"
else
echo "Hey boss, don't forget to specify the database parameters: ldash cr db DBNAME DBUSER 'DBPASS"
echo $ADDRESSEE$DONTF"specify the database parameters: ldash cr db DBNAME DBUSER 'DBPASS"
fi
fi
# REMOVAL OPERATIONS
elif [ $1 = "rm" ]; then
if [ -z "$2" ]; then
echo "Hey boss, don't forget to specify a removal operation: ldash rm site"
echo $ADDRESSEE$DONTF"specify a removal operation: ldash rm OPERATION"$AVAIL_OPS
# REMOVE SITE
elif [ $2 = "site" ]; then
if [ $# = 3 ]; then
echo "Removing site..." $3
echo "Removing site: " $3
/opt/lempdash/tasks/nginx/removeblock "$3"
else
echo "Hey boss, don't forget to specify a site name: ldash rm site SITENAME"
echo $ADDRESSEE$DONTF"specify a site name: ldash rm site SITENAME"
fi
# REMOVE DATABASE
elif [ $2 = "db" ]; then
if [ $# = 3 ]; then
echo "Dropping database: " $3
/opt/lempdash/tasks/mysql/removedb "$3"
else
echo $ADDRESSEE$DONTF"specify a database name: ldash rm db SITENAME"
fi
# REMOVE DATABASE USER
elif [ $2 = "dbuser" ]; then
if [ $# = 3 ]; then
echo "Dropping user: $3" "$3"
/opt/lempdash/tasks/mysql/removeuser "$3"
else
echo $ADDRESSEE$DONTF"specify a user name: ldash rm dbuser USERNAME"
fi
fi
fi

Expand Down
23 changes: 10 additions & 13 deletions tasks/mysql/removedb
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
#!/bin/bash
#!/usr/bin/env bash
# from: http://jetpackweb.com/blog/2009/07/20/bash-script-to-create-mysql-database-and-user/
EXPECTED_ARGS=2
E_BADARGS=65

MYSQL=`which mysql`
CURRENT_TIME=$(date "+%Y%m%d-%H%M%S")
FILE_NAME=$CURRENT_TIME'.sql'

Q1="DROP DATABASE IF EXISTS $1;"
Q2="DROP USER '$2'@'localhost';"
Q3="FLUSH PRIVILEGES;"
SQL="${Q1}${Q2}"
SQL="${Q1}"

if [ $# -ne $EXPECTED_ARGS ]
then
echo "Usage: $0 dbname dbuser"
exit $E_BADARGS
fi
echo "First I'll need your MySQL root user password to backup your database."
read -sp 'Password: ' PASSVAR

echo "I'll need your MySQL root user password next."
mkdir -p /var/www/archive
mysqldump -uroot -p$PASSVAR $Q1 > /var/www/archive/$Q1$CURRENT_TIME'.sql'

$MYSQL -uroot -p -e "$SQL"
$MYSQL -uroot -p$PASSVAR -e "$SQL"
19 changes: 19 additions & 0 deletions tasks/mysql/removeuser
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
# from: http://jetpackweb.com/blog/2009/07/20/bash-script-to-create-mysql-database-and-user/
EXPECTED_ARGS=1
E_BADARGS=65
MYSQL=`which mysql`

Q1="DROP USER '$1'@'localhost';"
Q2="FLUSH PRIVILEGES;"
SQL="${Q1}${Q2}"

if [ $# -ne $EXPECTED_ARGS ]
then
echo "Usage: $0 dbuser"
exit $E_BADARGS
fi

echo "I'll need your MySQL root user password next."

$MYSQL -uroot -p -e "$SQL"

0 comments on commit a525b0b

Please sign in to comment.