Kevin Warrington

Loading clever subtitle...

Mounting an AFP Filesystem

Here’s a simple way to connect to an Apple Filing Protocol (AFP) based server and access shared files.

mkdir /Volumes/my-share
mount_afp "afp://192.168.0.1/folder/" /Volumes/my-share

You can access your new connection in either /Volumes or in left pane of the finder.

Forward vs Reverse Proxy Using Apache

A proxy server acts as an intermediary between a client and a destination server, primarily controlling access to resources and caching content.

To enable proxy functionality in Apache, use the following:

# /etc/apache2/httpd.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

Forward Proxy

A Forward Proxy is typically used to:

  • hide the identities of clients
  • provide internet access to clients blocked by firewall
  • cache content to reduce network traffic

A forward proxy accepts requests from internal clients and forwards them to an external resource.

To configure this in Apache, we can simply control who has access to external resources by specifying their IP address.

<VirtualHost *:80>
  # Enables Apache to acting as forwarding server
  ProxyRequests On

  # Adds Via header to requests
  ProxyVia On

  <Proxy *>
    Order deny,allow
    Deny from all

    # Only allows this client to proxy requests
    Allow from 192.168.0.11
  </Proxy>
</VirtualHost>

Reverse Proxy

A Reverse proxy is typically used to:

  • hide the identities of servers
  • act as load balancer
  • act as web accelerator
  • used to perform A/B testing

A reverse proxy accepts requests from external clients and usually forwards them to an internal resource.

To configure this in Apache, we use the ProxyPass and ProxyPassReverse directives.

<VirtualHost *:80>
  # Prevents Apache from acting as forwarding server
  ProxyRequests Off

  # http://example.com/assets/css to be internally converted into a proxy request to http://cdn.example.com/css
  ProxyPass /assets/ http://cdn.example.com/

  # if http://cdn.example.com/css has a redirect to http://cdn.example.com/v2/css
  # Apache will adjust the request to http://example.com/assets/v2/css before forwarding response
  ProxyPassReverse /assets/ http://cdn.example.com/
</VirtualHost>

Querying MySQL Configuration

MySQL system variables can be queried using the show variables syntax:

mysql -uUSERNAME -pPASSWORD -e 'SHOW VARIABLES LIKE "ft_min_word_len"'

It’s also useful to find out with my.cnf file MySQL will try to load on init:

mysql --help | grep cnf

Moving on the Command Line

Moving along the command line will be a breeze, if you know the following shortcuts.

ctrl+a - move to start of line
ctrl+e - move to end of line
alt+b - move backwards a word
alt+f - move forwards a word
alt+del - remove word
ctrl+k - remove to end of line

On Mac, make sure you setup the following config in Terminal > Preferences > Keyboard:

  • use option as meta key is checked
  • option cursor left is set to \033b
  • option cursor right is set to \033f

where \033 = esc

Working With Background Processes

For long running processes, rather than blocking your prompt, it’s often useful to push commands to the background and complete other tasks.

To run a command in the background.

1
<command> &

With nohup you can run a command which will continue after logout. It will ignore SIGHUP signals. nice sets a lower priority.

1
nohup nice <command> &

With screen you can run a command which can be resumed after logout. It creates a new window with multiple processes instead of multiple Unix login sessions, so it is resource efficient.

1
screen -A -m -d -S mysessionname ./myscript.sh &

List all background processes

jobs

To send currently running command to background, first stop the process, Ctrl-z.

bg

To bring a background process to the foreground

fg
fg %1

To destroy a background process

kill %1 
kill -9 %1  
kill <pid>

To receive an email notification when a background process finishes

<command> | tee command.log | mailx -s 'PROCESS COMPLETE' test@example.com &

To run a set of jobs when cpu levels permit, use batch. ctrl+d to end input.

user@example.com > ~/.forward
batch -m
command1
command2
command3

You can list batch jobs and kill using the id

at -l
at -r id

MySQL Import/Export Progress Bar

MySQL command line progress can be monitored using the terminal-based “Pipe Viewer” pv.

Import

pv /path/to/sqlfile.sql | mysql -uUSERNAME -pPASSWORD -D DATABASE_NAME

Export

We need to estimate the file size of our export to get an accurate reading. This can be done via the information schema:

SELECT
    Data_BB / POWER(1024,1) Data_KB,
    Data_BB / POWER(1024,2) Data_MB,
    Data_BB / POWER(1024,3) Data_GB
FROM (
    SELECT SUM(data_length) Data_BB
    FROM information_schema.tables
    WHERE table_schema IN ('DATABASE_NAME')
) A;

We then use the estimated size to track export progress:

mysqldump -uUSERNAME -pPASSWORD DATABASE_NAME | pv -s 9999M > DATABASE_NAME.sql 

Beer Break!