Friday, September 04, 2015

MySQL script: drop all tables in a db and import a .sql file - Justin Kelly

MySQL script: drop all tables in a db and import a .sql file - Justin Kelly



#!/bin/bash

MUSER="-- Insert Db username here --"
MPASS="-- Insert Db password here --"
MDB="-- Insert Db name here --"
FILE="/home/user/path/to/your/import_file.sql"
 
# Detect paths
MYSQL=$(which mysql)
AWK=$(which awk)
GREP=$(which grep)
 
TABLES=$($MYSQL -u $MUSER -p$MPASS $MDB -e 'show tables' | $AWK '{ print $1}' | $GREP -v 'Tables' )
 
for t in $TABLES
do
    echo "Deleting $t table from $MDB database..."
    $MYSQL -u $MUSER -p$MPASS $MDB -e "drop table $t"
done

cat $FILE | mysql -u $MUSER -p$MPASS $MDB
echo "Sql imported"





PS:

fixed version

which mysql may return ''

suggest drop database instead of delete all tables

Thursday, July 16, 2015

search - Finding all files containing a text string on Linux - Stack Overflow

search - Finding all files containing a text string on Linux - Stack Overflow



Do the following:
grep -rnw 'directory' -e "pattern"
-r or -R is recursive, -n is line number and -w stands match the whole word. -l (letter L) can be added to have just the file name.
Along with these, --exclude or --include parameter could be used for efficient searching. Something like below:
grep --include=\*.{c,h} -rnw 'directory' -e "pattern"
This will only search through the files which have .c or .h extensions. Similarly a sample use of --exclude:
grep --exclude=*.o -rnw 'directory' -e "pattern"
Above will exclude searching all the files ending with .o extension. Just like exclude file it's possible to exclude/include directories through --exclude-dir and --include-dir parameter, the following shows how to integrate --exclude-dir:
grep --exclude-dir={dir1,dir2,*.dst} -rnw 'directory' -e "pattern"
This works for me very well, to achieve almost the same purpose like yours.
For more options :
man grep

Thursday, July 09, 2015

Install Redis and set multiple Redis instances on Mac OS | Codexpedia

Install Redis and set multiple Redis instances on Mac OS | Codexpedia



Install Redis with brew, if you don’t have brew installed yet, google how to install brew on Mac.
1
brew install redis
To have redis started on login.
1
ln -sfv /usr/local/opt/redis/*.plist ~/Library/LaunchAgents
Start redis with launchctl.
1
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
Start redis with redis-server command.
1
redis-server /usr/local/etc/redis.conf
To enter redis interactive console.
1
redis-cli
To list all keys in redis.
1
keys *
To clear all redis keys.
1
flushall
To exit redis interactive console.
1
exit
To set a new instance of redis, all you need is a new redis config file, start redis with that config file and you will have a second redis instance up and running.
1. Copy the default redis config file that is used for the default redis instance.
1
cp /usr/local/etc/redis.conf /usr/local/etc/redis2.conf
2. Open the redis2.conf and change the configuration for deamonize, pidfile, port, unixsocket, logfile and dbfilename to the below
1
2
3
4
5
6
daemonize yes
pidfile /usr/local/var/run/redis-2.pid
port 6380
unixsocket /tmp/redis-2.sock
logfile "/usr/local/var/log/redis-2.log"
dbfilename dump-2.rdb
3. Create launch on login config file.
1
cp ~/Library/LaunchAgents/homebrew.mxcl.redis.plist ~/Library/LaunchAgents/homebrew.mxcl.redis2.plist
4. Open ~/Library/LaunchAgents/homebrew.mxcl.redis2.plist and change the configuration as need so it will look like the below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>KeepAlive</key>
    <dict>
      <key>SuccessfulExit</key>
      <false/>
    </dict>
    <key>Label</key>
    <string>homebrew.mxcl.redis2</string>
    <key>ProgramArguments</key>
    <array>
      <string>/usr/local/opt/redis/bin/redis-server</string>
      <string>/usr/local/etc/redis2.conf</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>WorkingDirectory</key>
    <string>/usr/local/var</string>
    <key>StandardErrorPath</key>
    <string>/usr/local/var/log/redis-2.log</string>
    <key>StandardOutPath</key>
    <string>/usr/local/var/log/redis-2.log</string>
  </dict>
</plist>
5. Load redis with the plist file
1
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.redis2.plist
6. Start redis with the config file.
1
redis-server /usr/local/etc/redis2.conf
7. To have redis launch on start
1
ln -sfv /usr/local/opt/redis/*.plist ~/Library/LaunchAgents
8. To enter the second redis instance interactive console.
1
redis-cli -p 6380