Skip to content

Instantly share code, notes, and snippets.

@eusonlito
eusonlito / updated-at.sql
Last active April 21, 2024 21:47
Use trigger to change `updated_at` on all PostgreSQL tables
#
# Delete previous function definition (if exists)
#
DROP FUNCTION IF EXISTS before_update_updated_at() CASCADE;
#
# Create function to update updated_at timestamp if changed values on update
#
CREATE OR REPLACE FUNCTION before_update_updated_at() RETURNS trigger AS
$BODY$
@eusonlito
eusonlito / oppo-coloros-bloatware-disable
Last active April 20, 2024 02:28
Disable and Enable Oppo ColorOS bloatware. AVOID TO UNINSTALL PACKAGES OR YOUR PHONE CAN BE BRICKED ON FUTURE UPDATES.
pm disable-user --user 0 com.caf.fmradio
pm disable-user --user 0 com.coloros.activation
pm disable-user --user 0 com.coloros.activation.overlay.common
pm disable-user --user 0 com.coloros.alarmclock
pm disable-user --user 0 com.coloros.appmanager
pm disable-user --user 0 com.coloros.assistantscreen
pm disable-user --user 0 com.coloros.athena
pm disable-user --user 0 com.coloros.avastofferwall
pm disable-user --user 0 com.coloros.backuprestore
pm disable-user --user 0 com.coloros.backuprestore.remoteservice
@eusonlito
eusonlito / oom_score_adj.sh
Created March 26, 2024 08:46
This script identifies the second most memory-intensive PHP process, resets the Out-Of-Memory (OOM) priority of the previously adjusted process if still running, sets the current process's OOM priority to be more likely killed, and logs its PID and timestamp.
#!/bin/bash
APP="php"
CURRENT_HIGHEST=$(ps -eo pid,comm --sort=-%mem | grep "$APP" | head -n 2 | tail -n 1)
CURRENT_HIGHEST_PID=$(echo $CURRENT_HIGHEST | awk '{print $1'})
LAST_PID_FILE="/tmp/oom-kill-previous"
if [ -f "$LAST_PID_FILE" ]; then
LAST_PID=$(cat $LAST_PID_FILE)
@eusonlito
eusonlito / README.md
Last active March 15, 2024 15:01
Laravel Auth and Session without database. Using a remote API as Auth and Data provider.

This is an example of a web that uses a remote API as a database wrapper.

The remote API is stateless and the web uses cookies to maintain session persistence.

The API authentication endpoint returns a TOKEN that allows the web to make each request to the API with the user authentication header.


Este es un ejemplo de web que usa una API remota como wrapper de base de datos.

#!/bin/bash
if [ "`whoami`" != 'root' ]; then
echo ""
echo "This script only can be executed by root"
echo ""
exit 1
fi
@eusonlito
eusonlito / postgresql-table-size-rows.sql
Last active February 5, 2024 12:26
List PostgreSQL database tables by size and rows
SELECT
"s"."relname" AS "table_name",
pg_total_relation_size("s"."schemaname" || '.' || "s"."relname") / 1024 / 1024 AS "total_size",
pg_relation_size("s"."schemaname" || '.' || "s"."relname") / 1024 / 1024 AS "table_size",
(pg_total_relation_size("s"."schemaname" || '.' || "s"."relname") - pg_relation_size("s"."schemaname" || '.' || "s"."relname")) / 1024 / 1024 AS "index_size",
"s"."n_live_tup" AS "table_rows"
FROM
"pg_stat_user_tables" "s"
JOIN
"pg_class" "c" ON "s"."relid" = "c"."oid"
@eusonlito
eusonlito / README.md
Last active January 1, 2024 11:36
Strong iptables and ipset protection

Protect your server with a strong iptables rules and ipset lists.

1. Install ipset to manage ipstables lists

apt install ipset

2. Install iptables-persistent to preserve iptables rules on reboot

@eusonlito
eusonlito / apps-load.sh
Created December 18, 2023 21:25
Show Linux OS Apps Load
ps -eo comm,rss | awk '{arr[$1]+=$2} END {for (i in arr) print arr[i], i}' | sort -nr | head
@eusonlito
eusonlito / generateUniqueSlug.php
Created November 30, 2023 12:34
Generate unique slugs
<?php
protected static function generateUniqueSlug(string $title): string
{
$slug = str_slug($title);
if (static::query()->where('slug', $slug)->doesntExist()) {
return $slug;
}
@eusonlito
eusonlito / ipset-find.sh
Created November 3, 2023 09:30
Find IP in ipset lists
ip="XXX.XXX.XXX.XXX"
for set in $(ipset list -n); do
ipset test $set $ip &> /dev/null && echo "IP $ip found in $set"
done