Skip to content

Instantly share code, notes, and snippets.

@nathansgreen
nathansgreen / example.sql
Last active January 24, 2024 23:06
Postgres Sequence Bulk Increment
-- I needed a way to safely pull a bunch of sequence values to my client in order to bulk
-- insert many thousands of rows. I'm doing this because I don't want to use 128-bit keys.
-- I found this:
-- https://www.depesz.com/2008/03/20/getting-multiple-values-from-sequences/
select pg_advisory_lock(123);
alter sequence seq increment by 1000;
select nextval('seq');
alter sequence seq increment by 1;
select pg_advisory_unlock(123);
@nathansgreen
nathansgreen / new-mac-iinit.sh
Last active February 26, 2024 15:02
Initial setup for a new MacOS install
#!/usr/bin/env sh
# stuff to do when setting up a fresh install of MacOS
set -o errexit -o nounset -o noclobber
[ -e /etc/pam.d/sudo_local ] || \
echo 'auth sufficient pam_tid.so' \
| sudo tee /etc/pam.d/sudo_local
softwareupdate --install-rosetta --agree-to-license
@nathansgreen
nathansgreen / docker-compose.yml
Created December 4, 2023 17:39
UniFi Network Application with FerretDB
version: "3.8"
services:
unifi-controller:
image: lscr.io/linuxserver/unifi-network-application:7.5.187
restart: unless-stopped
depends_on:
- ferretdb
environment:
PUID: 1000
PGID: 1000
@nathansgreen
nathansgreen / last_insert_test.sql
Last active August 4, 2023 04:42
MySQL LAST_INSERT_ID used with On Duplicate Key Update
/*
* Quick demonstration of `id = LAST_INSERT_ID(id)` being the key to returning
* the existing id when doing `ON DUPLICATE KEY UPDATE`. The unfortunate side
* effect of this approach is that the sequence number for `id` increments on
* every update, even though the value for the updated row does not change. On
* update-heavy systems with 32-bit id`s, the sequence could be exhausted in a
* fairly short amount of time.
*
* Just switch to MariaDB and use `RETURNING id` instead. PostgreSQL got this
* keyword in 2006. Oracle was doing this in procedural code no later than 1997.
@nathansgreen
nathansgreen / icloud-evict.sh
Last active May 25, 2023 17:28
Fix macOS not enough free space to update
#!/bin/sh
# Manually free up local copies of files backed up to iCloud.
path=${1:-*}
pushd "~/Library/Mobile Documents/com~apple~CloudDocs" \
&& brctl evict $path
@nathansgreen
nathansgreen / config
Created February 13, 2023 20:21
.ssh/config
IdentitiesOnly=yes
ConnectTimeout=10
ConnectionAttempts=1
Host *
UseKeychain yes
GSSAPIAuthentication no
Host 10.245.*
User ec2-user
IdentityFile ~/.ssh/aws.pem
@nathansgreen
nathansgreen / gs-resample.sh
Created December 22, 2022 16:17 — forked from lkraider/gs-resample.sh
Ghostscript PDF quality downsample
#!/bin/sh
# It seems it's very hard to set resample output quality with Ghostscript.
# So instead rely on `prepress` preset parameter to select a good /QFactor
# and override the options we don't want from there.
gs \
-o resampled.pdf \
-sDEVICE=pdfwrite \
-dPDFSETTINGS=/prepress \
@nathansgreen
nathansgreen / AccessLogValve.java
Last active July 23, 2021 23:05
Send Embedded Tomcat Access Logs to Application Logger
@lombok.extern.log4j.Log4j2
public class AccessLogValve extends
org.apache.catalina.valves.AbstractAccessLogValve {
@Override
protected void log(java.io.CharArrayWriter message) {
log.info(message.toString());
}
}
@nathansgreen
nathansgreen / renew.udhcp.sh
Created May 19, 2020 21:48
Renew DHCP lease on Alpine Linux (using udhcp as the client)
#!/usr/bin/env sh
pkill -USR1 udhcpc
@nathansgreen
nathansgreen / README.md
Last active May 19, 2020 21:23
USG Disable DNS over HTTPS on local LAN with dnsmasq

I wanted a way to disable DNS over HTTPS so I can control DNS activity on my local network. I also block unauthorized outbound DNS requests with firewall rules.

The biggest limitation of this approach is the need to put all forwarding options in the config. Leaving out options, especially the 'server=' can break DNS on your whole network, so be careful. SSH into your USG and run mca-ctrl -t dump-cfg to find your current options and use them to replace the values in the file here.

I recommend running cat /etc/dnsmasq.conf before and after applying this config to see if you are missing things you need.