Merge branch 'master' into new
[irreco.git] / database / create-irreco-database.sh
blobe756042eab746dcb8d363439a5492f13022528c7
1 #!/bin/bash
3 create_irreco_database_main()
5 COMMAND="$1"
6 ARGS=("$@")
7 case "$COMMAND" in
8 --create-db) create_irreco_database_create "${ARGS[@]:1}";;
9 --set-perms) create_irreco_database_set_perms "${ARGS[@]:1}";;
10 --help|help|-h) create_irreco_database_usage;;
11 *) echo "Error: Unknown command \"$COMMAND\"";
12 create_irreco_database_usage;
13 exit 1;;
14 esac
17 create_irreco_database_usage()
19 echo ""
20 echo "Usage: $SCRIPT_NAME COMMAND"
21 echo ""
22 echo "Commands:"
23 echo " --create-db"
24 echo " Creates Irreco Database if it does not exists already."
25 echo " --set-perms"
26 echo " Create user which can to modify Irreco Database."
27 echo ""
30 create_irreco_database_get_root()
32 echo
33 echo "Give user which has permissions to create new databases. "
34 echo "That is probably the MySQL root user."
35 echo -n "Username (root): "
36 read MYSQL_USERNAME
38 if [[ "$MYSQL_USERNAME" == "" ]]; then
39 MYSQL_USERNAME='root'
42 echo -n "Password: "
43 read -s MYSQL_PASSWORD
44 echo
45 echo
48 create_irreco_database_create()
50 create_irreco_database_get_root
51 echo "Creating database ..."
52 mysql --user="$MYSQL_USERNAME" --password="$MYSQL_PASSWORD" --batch \
53 < irreco-database.sql
54 if [[ "$?" == "0" ]]; then
55 echo "... Done"
59 create_irreco_database_set_perms()
61 create_irreco_database_get_root
62 echo "Give password for irreco user."
63 echo -n "Password: "
64 read IRRECO_PASSWORD
65 echo
66 echo "Creating irreco user ..."
67 create_irreco_database_set_perms_sql | \
68 mysql --user="$MYSQL_USERNAME" --password="$MYSQL_PASSWORD" --batch
71 create_irreco_database_set_perms_sql()
73 IRRECO_PERMS='INSERT,DELETE,UPDATE,SELECT,LOCK TABLES'
75 echo 'grant '"$IRRECO_PERMS"' on `irreco`.* to irreco@localhost;'
76 echo 'grant '"$IRRECO_PERMS"' on `irreco`.* to irreco@127.0.0.1;'
77 echo "set password for irreco@localhost = password('$IRRECO_PASSWORD');"
78 echo "set password for irreco@127.0.0.1 = password('$IRRECO_PASSWORD');"
81 create_irreco_database_main "$@"