Skip to content

Instantly share code, notes, and snippets.

@pujianto
pujianto / startup script on vultr: add 2GB of SWAP
Last active June 8, 2017 12:10
Simple startup script on instance creation on vultr.com, for this case only updating repo and adding swap.
#!/bin/bash
#add swap
SWAP_SIZE="2G"
SWAP_MOUNT_NAME='/swapfile'
SWAP_EXISTS=`swapon -s`
SWAP_ON_STARTUP="$SWAP_MOUNT_NAME swap swap sw 0 0"
if [[ -z $SWAP_EXISTS ]] && [[ ! -f $SWAP_MOUNT_NAME ]] ; then
fallocate -l $SWAP_SIZE ${SWAP_MOUNT_NAME}
@pujianto
pujianto / backup.sh
Last active May 20, 2020 02:37
Simple script to backup all mysql databases & store them on remote server (in silent mode)
#!/bin/sh
HOST="localhost"
USER="root"
PASSWORD="root"
DB="--all-databases"
DATE=`date +%Y-%m-%d.%H.%M.%S`
NAME="mysql.backup.${DB}.${DATE}.sql.gz"
COMMAND="mysqldump -h ${HOST} -u ${USER} -p$PASSWORD ${DB} | gzip -c > ${NAME}"
SCP_COMMAND="scp ${NAME} user@backup-server:/path/to/backup/folder/"
@pujianto
pujianto / mailgun.sh
Created February 7, 2017 20:53
Bash script to send an email through mailgun api. Example usage: sh mailgun.sh
#!/bin/bash'
GREEN="\e[92m"
RED="\e[91m"
NC="\033[0m"
printf "${GREEN}Mailgun send email CLI${NC}\n"
function required {
while [[ true ]]; do
local _name=$1
local _label=$2
local _multiline=$3
@pujianto
pujianto / tcp.js
Created April 20, 2019 07:56
Minimal TCP socket server on Nodejs
const net = require('net');
const server = net.createServer( socket => {
socket.on('data', (data) => {
let message = data.toString('utf-8').trim();
console.log('incoming data ', message);
if (message === 'exit') {
let connectionName = socket.address().port + socket.address().address;
console.log('closing connection to ', connectionName);
socket.write('exiting...');
socket.destroy();
@pujianto
pujianto / amqp.js
Created June 21, 2019 06:48
amqplib send multiple queues to RabbitMQ
const amqp = require('amqplib');
const connect = amqp.connect('amqp://localhost');
let queue1 = 'queue1', queue2 = 'queue2';
connect.then( conn => {
return conn.createChannel();
})
.then(ch => {
return Promise.all([ch.assertQueue(queue1), ch.assertQueue(queue2)]).then((ok1,ok2) => {
return Promise.resolve(ch);
@pujianto
pujianto / Search *.css and *.js and add brotli compression
Created September 25, 2019 05:23
Search all css & js files recursively and compress it with brotli. -Z mode = maximum compression
find . -type f -regextype posix-extended -regex '.*(css|js)$' -exec brotli -fZ {} \;
@pujianto
pujianto / resize_tmp.sh
Created October 14, 2019 05:08
useful when updating android studio in aur package.
mount -o remount,size=10G /tmp/
@pujianto
pujianto / wpapi_test.js
Created November 23, 2019 07:36
wpapi.js Get or Create Tag example
const api = require('wpapi');
const wp = new api({
endpoint: 'https://wordpress.domain/wp-json',
username: 'admin',
password: 'admin'
});
var tags = ['a','b','c'];
var getOrCreateTags = tags.map(tag => {
return wp.tags().slug(tag)
@pujianto
pujianto / offload_backup.sh
Last active May 20, 2020 02:36
Offload mysql databases & website backup files to S3 Server
#!/bin/sh
LOCAL_BACKUP_FOLDER="/home/user/backup"
S3_BACKUP_FOLDER="s3://bucket-name/backup_folder/"
WEBSITE_PATH="/var/www"
WEBSITE_BACKUP_NAME="mywebsites.tar.gz"
printf "\nCREATING BACKUP ARCHIVE FROM ${WEBSITE_PATH}\n\n"
tar -czf "${LOCAL_BACKUP_FOLDER}/${WEBSITE_BACKUP_NAME}" -C "${WEBSITE_PATH}" .
@pujianto
pujianto / .my.cnf
Created November 27, 2019 14:45
set default mysql client params, so you don't need to pass host,username params when using mysql client. Place it in /home/username/.my.cnf
[client]
user=root
password=root
host=localhost
port=3306