Skip to content

Commit

Permalink
Merge pull request #7 from tangoslee/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
tangoslee authored Oct 13, 2022
2 parents c8a920f + c43a39a commit 2bc1313
Show file tree
Hide file tree
Showing 23 changed files with 357 additions and 2,041 deletions.
4 changes: 3 additions & 1 deletion .bashrc_docker
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
[ -f ~/.bashrc ] && . ~/.bashrc
PS1=$(printf "docker>$PS1")
#PS1=$(printf "docker>$PS1")
export PS1="docker>$PS1"
export PATH=$HOME/bin:$PATH
41 changes: 0 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,6 @@
# How to build local environment

## Local wild card domain

Work as root

### Set *.local to 127.0.0.1

```
echo 'address=/.local/127.0.0.1' > /etc/NetworkManager/dnsmasq.d/local-wildcard.conf
```

#### Add 'dns=dnsmasq' to /etc/NetworkManager/NetworkManager.conf

```
[main]
plugins=ifupdown,keyfile
dns=dnsmasq
[ifupdown]
managed=false
[device]
wifi.scan-rand-mac-address=no
```

#### Replace /etc/resolve.conf
```bash
mv /etc/resolve.conf /etc/resolv.conf.dist
ln -s /var/run/NetworkManager/resolv.conf /etc/resolv.conf
```

#### Reload NetworkManager

```
systemctl reload NetworkManager
```

#### Test

```
dig test.local +short
127.0.0.1
```

#### /etc/nsswitch.conf

Expand Down
15 changes: 15 additions & 0 deletions attach.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@echo off
SETLOCAL EnableDelayedExpansion

if "%1" == "" (
SET CONTAINER="php"
) else (
SET CONTAINER="%1"
)

if "%2" == "" (
docker exec -it %CONTAINER% /bin/bash --init-file ~/.bashrc_docker
) else (
SET USER="--user %2"
docker exec -it %USER% %CONTAINER% /bin/bash --init-file ~/.bashrc_docker
)
8 changes: 5 additions & 3 deletions attach.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/bin/bash
# https://opensource.com/article/19/9/linux-terminal-colors

CONTAINER=workspace
CONTAINER=php
OPT="-it"

[ "$1" != "" ] && CONTAINER=$1
[ "$1" != "" ] && CONTAINER="$1"
[ "$2" != "" ] && OPT="$OPT --user $2"

docker exec -it $CONTAINER /bin/bash --init-file ~/.bashrc_docker
docker exec -it $OPT $CONTAINER /bin/bash --init-file ~/.bashrc_docker
28 changes: 17 additions & 11 deletions bin/create_db.sh
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
#!/bin/bash
# https://dev.mysql.com/doc/refman/8.0/en/creating-accounts.html
__DIR__=$(dirname $(readlink -f $0))
CWD=$(dirname $(readlink -f "$0"))
ENV=${CWD}/../.env

config=$__DIR__/../.env
[ -f "$ENV" ] && {
. "$ENV"
}

DBNAME="$1"

[ "$DBNAME" = "" ] && {
echo "usage: `basename $0` dbname"
echo "usage: $(basename "$0") dbname"
exit 1
}
#export MYSQL_PWD="$DB_PASSWORD"

echo "Create database $DBNAME"

CMD="mysql -h db.local -u root mysql"
TMP=.user.sql

[ -f "$TMP" ] && rm -f "$TMP"

HOST="%"
USER=$(grep DB_USERNAME $config | cut -d'=' -f2)
PASS=$(grep DB_PASSWORD $config | cut -d'=' -f2)
USER=${DB_USERNAME}
PASS=${DB_PASSWORD}

[ "$USER" = "" -o "$PASS" = "" ] && {
echo "Invalid format: user or password"
exit
}

CMD="mysql -h db.localhost -u root mysql"
TMP=.user.sql

[ -f "$TMP" ] && rm -f "$TMP"

cat<<EOL > $TMP
CREATE DATABASE IF NOT EXISTS $DBNAME;
CREATE USER IF NOT EXISTS '$USER'@'$HOST' IDENTIFIED BY '$PASS';
Expand All @@ -35,8 +39,10 @@ FLUSH PRIVILEGES;
EOL

{
$CMD < "$TMP"
$CMD < "$TMP"
} && {
[ -f "$TMP" ] && rm -f "$TMP"
} && {
echo "$DBNAME has been successfully created"
}

File renamed without changes.
31 changes: 31 additions & 0 deletions bin/drop_db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash
# https://dev.mysql.com/doc/refman/8.0/en/creating-accounts.html
__DIR__=$(dirname $(readlink -f $0))

config=$__DIR__/../.env

DBNAME="$1"

[ "$DBNAME" = "" ] && {
echo "usage: `basename $0` dbname"
exit 1
}

echo "Drop database $DBNAME"

#CMD="mysql -h db.localhost -u root -p$PASS mysql"
CMD="mysql -h db.localhost -u root mysql"
TMP=.user.sql

[ -f "$TMP" ] && rm -f "$TMP"

cat<<EOL > $TMP
DROP DATABASE IF EXISTS $DBNAME;
EOL

{
$CMD < "$TMP"
} && {
[ -f "$TMP" ] && rm -f "$TMP"
}

36 changes: 36 additions & 0 deletions bin/dump_db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/sh
#It is best that --tab be used only for dumping a local server. If you use it with a remote server, the --tab directory must exist on both the local and remote hosts, and the .txt files are written by the server in the remote directory (on the server host), whereas the .sql files are written by mysqldump in the local directory (on the client host).
# https://dev.mysql.com/doc/refman/8.0/en/mysqldump-delimited-text.html
CWD=$(dirname $(readlink -f "$0"))
ENV=${CWD}/../.env

[ -f "$ENV" ] && {
. "$ENV"
}


DATABASE="$1"
CMD="docker exec mysql"

[ "$DATABASE" = "" ] && {
cat<<EOL
usage: $(basename $0) database_to_restore
EOL
exit 1
}

SECURE_DIR=$($CMD mysql -N -e "SHOW VARIABLES LIKE 'secure_file_priv';" | awk '{ print $2 }')
[ "$SECURE_DIR" = "" ] && SECURE_DIR=/var/lib/mysql-files

[ -d "${DB_DUMP_DIR}" ] &&
{
echo "Cleaning ${DB_DUMP_DIR} before dump"
find "${DB_DUMP_DIR}" -type f -exec rm -f {} \;
} && {
echo "Dump Started to ${DB_DUMP_DIR}"
$CMD mysqldump "$DATABASE" --tab=$SECURE_DIR --fields-terminated-by=0x1e --single-transaction --order-by-primary
} && {
echo "The job has been successfully done!"
}


56 changes: 56 additions & 0 deletions bin/restore_db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/bash
#It is best that --tab be used only for dumping a local server. If you use it with a remote server, the --tab directory must exist on both the local and remote hosts, and the .txt files are written by the server in the remote directory (on the server host), whereas the .sql files are written by mysqldump in the local directory (on the client host).
# https://dev.mysql.com/doc/refman/8.0/en/mysqldump-delimited-text.html
CWD=$(dirname $(readlink -f "$0"))
ENV=${CWD}/../.env

[ -f "$ENV" ] && {
. "$ENV"
}

SRC=${DB_DUMP_DIR}
DATABASE="$1"
CMD="docker exec mysql"

[ "$DATABASE" = "" ] && {
cat<<EOL
usage: $(basename "$0") database_to_restore
EOL
exit 1
}

SECURE_DIR=$($CMD mysql -N -e "SHOW VARIABLES LIKE 'secure_file_priv';" | awk '{ print $2 }')
[ "$SECURE_DIR" = "" ] && SECURE_DIR=/var/lib/mysql-files

echo "Start to restore database from ${SRC}"
{
echo "Drop database: ${DATABASE}"
$CMD mysql -e "DROP DATABASE IF EXISTS $DATABASE"
} && {
echo "Create database"
$CMD mysql -e "CREATE DATABASE IF NOT EXISTS $DATABASE" 2> /dev/null
} && {
echo "Restore $DATABASE"
for localTxtFile in "${SRC}"/*.txt; do
localSqlFile=${localTxtFile/${SRC}/${SECURE_DIR::-1}}
table=$(basename "${localTxtFile::-4}")
rows=$(wc -l "${localTxtFile}" | awk '{ print $1 }')
sqlFile=${localSqlFile/.txt/.sql}
txtFile=${localTxtFile/${SRC}/${SECURE_DIR::-1}}

#echo "Create table: $table"
$CMD mysql "${DATABASE}" -e "SET FOREIGN_KEY_CHECKS=0;SET UNIQUE_CHECKS=0;source ${sqlFile};"

echo -n "Restore data: $table (${rows} rows)"
startedAt=$(date +%s.%3N)
$CMD mysql "${DATABASE}" --local-infile=1 -e "SET FOREIGN_KEY_CHECKS=0;SET UNIQUE_CHECKS=0;SET AUTOCOMMIT=0;ALTER TABLE ${table} DISABLE KEYS;LOAD DATA LOCAL INFILE '${txtFile}' INTO TABLE ${table} FIELDS TERMINATED BY 0x1e LINES TERMINATED BY '\n';COMMIT;ALTER TABLE ${table} ENABLE KEYS;"
endedAt=$(date +%s.%3N)
time1=$(echo "scale=3; ${endedAt} - ${startedAt}" | bc)
echo " $time1 sec"
done

$CMD mysql "${DATABASE}" -e "SET FOREIGN_KEY_CHECKS=1;SET UNIQUE_CHECKS=1;SET AUTOCOMMIT=1;"
} && {
echo "The job has been successfully done!"
}

5 changes: 0 additions & 5 deletions composer.json

This file was deleted.

Loading

0 comments on commit 2bc1313

Please sign in to comment.