Skip to content

Instantly share code, notes, and snippets.

@yohanesgultom
Last active May 15, 2021 04:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save yohanesgultom/fbacf601ce49bd38363b to your computer and use it in GitHub Desktop.
Save yohanesgultom/fbacf601ce49bd38363b to your computer and use it in GitHub Desktop.
Random shell scripts
Random shell scripts
# Author: acdcjunior http://stackoverflow.com/users/1850609/acdcjunior
# Source: http://stackoverflow.com/questions/16006943/add-an-answer-to-a-command-in-shell-script
# !/usr/bin/expect -f
# Expect script to supply root/admin password for remote ssh server
# and execute command.
# This script needs three argument to(s) connect to remote server:
# password = Password of remote UNIX server, for root user.
# ipaddr = IP Addreess of remote UNIX server, no hostname
# scriptname = Path to remote script which will execute on remote server
# For example:
# ./sshlogin.exp password 192.168.1.11 who
# ------------------------------------------------------------------------
# Copyright (c) 2004 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# ----------------------------------------------------------------------
# set Variables
set password [lrange $argv 0 0]
set ipaddr [lrange $argv 1 1]
set scriptname [lrange $argv 2 2]
set arg1 [lrange $argv 3 3]
set timeout -1
# now connect to remote UNIX box (ipaddr) with given script to execute
spawn ssh root@$ipaddr $scriptname $arg1
match_max 100000
# Look for passwod prompt
expect "*?assword:*"
# Send password aka $password
send -- "$password\r"
# send blank line (\r) to make sure we get back to gui
send -- "\r"
expect eof
#!/bin/bash
# set PUBLIC_IP env variable with current instance IP to be used in docker-compose configuration
date &&
export PUBLIC_IP=$(/usr/bin/curl http://checkip.amazonaws.com) &&
cd /home/ubuntu/condor_cloud &&
/usr/bin/docker-compose down -v &&
/usr/bin/docker-compose up -d
#!/bin/bash
# Dump mysql database(s) and keep the limit of backup files by deleting older ones
# to prevent password prompt by mysqldump create .my.cnf file in $HOME directory containing:
# [mysqldump]
# user=root
# password=rootpassword
now=$(date +"%Y%m%d%H%M%S")
dirname="backup"
maxfile=3
echo "Creating backup of all MySQL databases.."
mkdir -p "$dirname"
# single database
dbname="mgps"
bak_file="$dbname.$now.sql.gz"
mysqldump "$dbname" | gzip -9 > "$dirname/$bak_file"
# all databases
# bak_file="all-databases.$now.sql.gz"
# mysqldump --all-databases --single-transaction | gzip -9 > "$dirname/$bak_file"
echo "Backup file created: $bak_file"
# This will list our file by modification time and delete all but the 3 most recent
$((maxfile++))
purging=$(ls -dt "$dirname"/* | tail -n +"$maxfile");
if [ "$purging" != "" ]; then
echo Purging old file: $purging;
rm -rf $purging;
else
echo "No file found for purging at this time";
fi
#!/bin/bash
# Tested on Ubuntu 20 LTS
DATE=$(date +'%Y%m%d%H%M')
DB_NAME=pmjkt
APP_DIR=$HOME/pmjkt
BACKUP_DIR=$HOME/backup
BACKUP_FINAL_PATH="$HOME/$DB_NAME-$DATE.tar.gz"
echo "deleting old backup dir.."
rm -Rf $BACKUP_DIR
mkdir -p $BACKUP_DIR/nginx
mkdir -p $BACKUP_DIR/mysql
mkdir -p $BACKUP_DIR/app
echo "backing up nginx.."
cp -R /etc/nginx/sites-available $BACKUP_DIR/nginx/
cp /etc/nginx/nginx.conf $BACKUP_DIR/nginx/
echo "backing up mysql.."
sudo mysqldump $DB_NAME | gzip -9 > $BACKUP_DIR/mysql/"$DB_NAME.sql.gz"
echo "backing up app.."
cp -r $APP_DIR $BACKUP_DIR/app
find $BACKUP_DIR/app -name ".git" -type d -print0|xargs -0 rm -r --
echo "compressing to $BACKUP_FINAL_PATH.."
cd $BACKUP_DIR
tar -zcf $BACKUP_FINAL_PATH *
echo "completed"
#!/bin/sh
# Find file with $findpattern recursively in $findpath
# Execute $cmd $file where for all files found
#
# Usage example:
# ./batch_process.sh "/var/www/yohanes/wp-content/uploads" "*.png" "optipng"
# ./batch_process.sh "/var/www/yohanes/wp-content/uploads" "*.jpg" "jpegoptim"
#
# @Author: yohanes.gultom@gmail.com
findpath=$1
findpattern=$2
cmd=$3
: ${findpath:="."}
find "$findpath" -name "$findpattern" | while read f ; do
eval "$cmd $f"
done
#!/bin/sh
# tested on debian 9 x64
# some commands require user inputs (eg. password)
# configuration
# repo="https://bitbucket.org/yohanesgultom/ucds2"
# install basic tools
apt-get update
apt install sudo locales-all zip curl git -y
# create sudo user
adduser dev
usermod -aG sudo dev
# ssh-copy-id user@hostname.example.com
su dev
sudo dpkg-reconfigure tzdata
locale-gen
# setup tomcat
curl -s "https://get.sdkman.io" | bash
source ~/.sdkman/bin/sdkman-init.sh
sdk install java 7.0.181-zulu
echo "export JAVA_HOME=/home/dev/.sdkman/candidates/java/7.0.181-zulu" >> ~/.bashrc
source ~/.bashrc
wget http://www-us.apache.org/dist/tomcat/tomcat-7/v7.0.90/bin/apache-tomcat-7.0.90.tar.gz
tar -zxvf apache-tomcat-7.0.90.tar.gz
rm -Rf apache-tomcat-7.0.90/webapps/*
# setup db
echo "export LC_ALL=\"en_US.UTF-8\"" >> ~/.bashrc
source ~/.bashrc
sudo apt-get install postgresql -y
sudo pg_createcluster 9.3 main --start # if autostart is not working
sudo -i -u postgres createdb sirsak
sudo -i -u postgres createuser --no-superuser --pwprompt sirsak
sudo -i -u postgres psql -c "grant all privileges on database sirsak to sirsak;"
# setup grails
sdk install grails 2.2.1
# setup app
git clone https://bitbucket.org/yohanesgultom/ucds2 sirsak
cp ucds2/.sirsak.groovy .
cd sirsak
grails dev war dist/sirsak.war
cp dist/sirsak.war /home/dev/apache-tomcat-7.0.90/webapps/ROOT.war
# start/stop tomcat
/home/dev/apache-tomcat-7.0.90/bin/startup.sh
# /home/dev/apache-tomcat-7.0.90/bin/shutdown.sh
# setup routing
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to 8080
# sudo nano /etc/rc.local
# add autorestart job
crontab -l > editcron
echo "0 0 * * * /home/ubuntu/deploylatest.sh" >> editcron
crontab editcron
rm editcron
sudo service cron restart
# OPTIONAL (just in case) install missing plugins
cd ~/.sdkman/candidates/grails/2.2.1/plugins/
wget -O resources-1.2.14.zip https://grails.org/plugins/grails-resources/tags/RELEASE_1.2.14/grails-resources-1.2.14.zip
wget -O resources-1.2.14.pom https://grails.org/plugins/grails-resources/tags/RELEASE_1.2.14/grails-resources-1.2.14.pom
wget -O excel-export-0.2.1.zip https://grails.org/plugins/grails-excel-export/tags/RELEASE_0.2.1/grails-excel-export-0.2.1.zip
wget -O excel-export-0.2.1.pom https://grails.org/plugins/grails-excel-export/tags/RELEASE_0.2.1/grails-excel-export-0.2.1.pom
wget -O shiro-1.1.4.zip https://grails.org/plugins/grails-shiro/tags/RELEASE_1.1.4/grails-shiro-1.1.4.zip
wget -O shiro-1.1.4.pom https://grails.org/plugins/grails-shiro/tags/RELEASE_1.1.4/grails-shiro-1.1.4.pom
#!/bin/bash
# Simple script to switch between git global config & credential store
# Assuming you are storing your config in "$HOME/.gitconfig-{profile_name}"
# and credential in "$HOME/.git-credentials-{profile_name}", switch to it by calling
# git-switch {profile_name}
if [ "$#" -ne 1 ]
then
echo "usage: git-switch {profile}"
else
profile=$1
config_file="$HOME/.gitconfig-$profile"
cred_file="$HOME/.git-credentials-$profile"
if [[ -f "$config_file" && -f "$cred_file" ]]
then
rm $HOME/.gitconfig && cp $config_file $HOME/.gitconfig
rm $HOME/.git-credentials && cp $cred_file $HOME/.git-credentials
else
[ ! -f "$config_file" ] && echo "$config_file does not exist"
[ ! -f "$cred_file" ] && echo "$cred_file does not exist"
fi
fi
#!/bin/bash
# script configuration
src_path="/path/to/project/source"
build_path="$src_path/dist"
deploy_path="/var/lib/tomcat7/webapps/"
war_build_name="app"
war_deploy_name="ROOT"
service_name="tomcat7"
genv="dev" # grails default build env
branch="master" # git remote branch
# set env if given in arg $1
if [ $# -gt 0 ]; then
genv="$1"
fi
if [ $# -gt 1 ]; then
branch="$2"
fi
# set grails build env if given in arg $1
if [ $# -gt 0 ]; then
genv="$1"
fi
# pull latest change
cd $src_path
git pull origin master
# build
echo "grails $genv war $build_path/$war_build_name.war"
grails $genv war "$build_path/$war_build_name.war"
# backup
echo "creating backup.."
now=$(date +"%Y%m%d%H%M%S")
sudo cp "$deploy_path/$war_deploy_name.war" "$pwd/$war_build_name.war.$now"
# deploy
echo "deploying.."
sudo service $service_name stop
sudo rm -R "$deploy_path/$war_deploy_name"
sudo rm "$deploy_path/$war_deploy_name.war"
sudo cp "$build_path/$war_build_name.war" "$deploy_path/$war_deploy_name.war"
sudo service $service_name start
# return to original path
cd "$pwd"
#!/bin/bash
# Delete uploads leaving the $maxfile most recent files
dirname="static/uploads"
maxfile=$1
printf "maxfile=$maxfile\n"
maxfile=$((maxfile+1))
purging=$(ls -dt "$dirname"/* | tail -n +"$maxfile");
if [ "$purging" != "" ]; then
printf "Purging old directories:\n$purging\n";
rm -rf $purging;
else
printf "No directory found for purging at this time\n";
fi
#!/bin/sh
# configuration
PROJECT_NAME="plasadata-api"
PROJECT_DIR="/root/plasadata-api"
# keep original path
pwd=$(pwd)
# switch to project dir
cd "$PROJECT_DIR"
# stop loopback
slc ctl stop "$PROJECT_NAME"
echo "Get latest changes.."
# pull from git
git pull origin master
# install new dependencies if any
npm install
# start loopback again
slc ctl start "$PROJECT_NAME"
# give some time
sleep 5
# check status
slc ctl status "$PROJECT_NAME"
# return to original path
cd "${pwd}"
echo "DONE"
#!/bin/bash
now=$(date +"%Y%m%d%H%M%S")
db=mydb
username=secret
password=secret
# backup to temp dir
mongodump -u $username -p $password --db $db --excludeCollection sessions --excludeCollection jobs --out "$HOME/backups/$now" &&
# compress
tar -zcvf "$HOME/backups/mongo-$now.tar.gz" "$HOME/backups/$now" &&
# delete tmp dir
rm -Rf "$HOME/backups/$now"
# !/bin/bash
# Postgresql db backup script
dbname="dbname"
now="$(date +'%Y%m%d%H%M')"
target="/path/to/backup/$dbname.$now.dump"
echo "dumping $dbname to $target.."
pg_dump -U postgres "$dbname" > "$target"
# restore dump
# psql dbname < yourbackup.sql
# username will be prompted here
# hence manual intervention is still required
# for full automation, consider integrate this
# with autofill_password.sh
echo "DONE"
#!/bin/bash
LOCALDB=kasihkerja_wp
REMOTEDB=wordpress
REMOTESERVER=kasihkerja
DBFILE=${LOCALDB}_$(date +'%Y%m%d%H%M')
mysqldump -u root -p $LOCALDB | gzip -9 > $DBFILE.sql.gz
scp $DBFILE.sql.gz $REMOTESERVER:~
ssh $REMOTESERVER << EOF
gunzip $DBFILE.sql.gz
sudo mysql -e "DROP DATABASE IF EXISTS $REMOTEDB"
sudo mysql -e "CREATE DATABASE $REMOTEDB"
sudo mysql $REMOTEDB < $DBFILE.sql
EOF
#!/bin/bash
### BEGIN INIT INFO
# Provides: tomcat7
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/Stop Tomcat server
### END INIT INFO
PATH=/sbin:/bin:/usr/sbin:/usr/bin
CATALINA_HOME=/var/lib/tomcat7
start() {
sudo $CATALINA_HOME/bin/startup.sh
}
stop() {
sudo $CATALINA_HOME/bin/shutdown.sh
}
case $1 in
start) start;;
stop) stop;;
restart) stop; start;;
*) echo "Run as $0 "; exit 1;;
esac
# Tested on Ubuntu 16 x64
# configuration
dir="ucds2"
repo="https://bitbucket.org/yohanesgultom/ucds2"
gitusername="Yohanes Gultom"
gitemail="yohanes.gultom@gmail.com"
dbname="sirsak"
dbuser="sirsak"
# main script
# create sudo user
adduser "$dbuser"
usermod -aG sudo "$dbuser"
# ssh-copy-id user@hostname.example.com
su "$dbuser"
sudo dpkg-reconfigure tzdata
sudo apt-get update
# setup tomcat
wget http://www-eu.apache.org/dist/tomcat/tomcat-7/v7.0.85/bin/apache-tomcat-7.0.85.tar.gz
sudo tar xzvf apache-tomcat-7.0.85.tar.gz -C /var/lib
sudo mv /var/lib/apache-tomcat-7.0.85 /var/lib/tomcat7
sudo nano /etc/init.d/tomcat7 # copy paste content of `tomcat7.init.d`
sudo chmod 755 /etc/init.d/tomcat7
sudo update-rc.d tomcat7 defaults
# install java
sudo add-apt-repository ppa:openjdk-r/ppa
sudo apt-get update
sudo apt-get install openjdk-7-jdk
echo "export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64" >> ~/.bashrc
source ~/.bashrc
# setup db
sudo locale-gen
echo "export LC_ALL=\"en_US.UTF-8\"" >> ~/.bashrc
source ~/.bashrc
sudo apt-get install postgresql -y
#sudo pg_createcluster 9.5 main --start # if autostart is not working
sudo -i -u postgres createdb "$dbname"
sudo -i -u postgres createuser --no-superuser --pwprompt "$dbuser"
sudo -i -u postgres psql -c "grant all privileges on database $dbname to $dbuser;"
# setup grails
sudo apt-get install zip -y
curl -s "https://get.sdkman.io" | bash
source ~/.sdkman/bin/sdkman-init.sh
sdk install grails 2.2.1
# setup app
sudo apt-get install git -y
git config --global user.email $gitemail
git config --global user.name $gitusername
git config --global credential.helper store
git clone "$repo" "$dir"
cp "$dir/deploylatest.sh" ~
./deploylatest.sh
# forward port 80 to 8080
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to 8080
# append on /etc/rc.local to be run automatically on startup
sudo service tomcat7 start
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to 8080
# (optional) firewall setting (fail2ban is already installed by default)
sudo ufw allow ssh
sudo ufw allow www
sudo ufw allow 8080
sudi ufw enable
# Installing NVIDIA OpenCL
# tested with Ubuntu 15.10 (Willy Werewolf)
# install cuda
# http://www.r-tutor.com/gpu-computing/cuda-installation/cuda7.5-ubuntu
# install nvidia opencl libs
sudo apt-get nvidia-opencl-dev
# compile
# hello.c https://www.fixstars.com/en/opencl/book/OpenCLProgrammingBook/first-opencl-program/
gcc hello.c -l OpenCL -o hello.o
# run
./hello.o
# Taken from https://gist.github.com/kradalby/3252b8bacca6622bf864/#file-uwsgi
# and change the nginx user to www-data
#!/usr/bin/env bash
### BEGIN INIT INFO
# Provides: emperor
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the uwsgi emperor app server
# Description: starts uwsgi emperor app server using start-stop-daemon
### END INIT INFO
set -e
PATH=/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/bin/uwsgi
RUN=/var/run/uwsgi
ENABLED_CONFIGS_DIR=/etc/uwsgi/apps-enabled
AVAILABLE_CONFIGS_DIR=/etc/uwsgi/apps-available
NAME=uwsgi
DESC=emperor
OWNER=www-data
GROUP=www-data
OP=$1
[[ -x $DAEMON ]] || exit 0
[[ -d $RUN ]] || mkdir $RUN && chown $OWNER.$GROUP $RUN
DAEMON_OPTS=""
# Include uwsgi defaults if available
if [[ -f /etc/default/uwsgi ]]; then
. /etc/default/uwsgi
fi
do_pid_check()
{
local PIDFILE=$1
[[ -f $PIDFILE ]] || return 0
local PID=$(cat $PIDFILE)
for p in $(pgrep $NAME); do
[[ $p == $PID ]] && return 1
done
return 0
}
do_start()
{
local PIDFILE=$RUN/$NAME.pid
local START_OPTS=" \
--emperor $ENABLED_CONFIGS_DIR \
--pidfile $PIDFILE \
--daemonize /var/log/$NAME/uwsgi-emperor.log \
--uid $OWNER \
--gid $GROUP"
if do_pid_check $PIDFILE; then
$NAME $DAEMON_OPTS $START_OPTS
else
echo "Already running!"
fi
}
send_sig()
{
local PIDFILE=$RUN/$NAME.pid
set +e
[[ -f $PIDFILE ]] && kill $1 $(cat $PIDFILE) > /dev/null 2>&1
set -e
}
wait_and_clean_pidfile()
{
local PIDFILE=$RUN/uwsgi.pid
until do_pid_check $PIDFILE; do
echo -n "";
done
rm -f $PIDFILE
}
do_stop()
{
send_sig -3
wait_and_clean_pidfile
}
do_reload()
{
send_sig -1
}
do_force_reload()
{
send_sig -15
}
get_status()
{
send_sig -10
}
enable_configs()
{
local configs
if [[ $# -eq 0 || ${1,,} = 'all' ]]; then
configs=$(diff $AVAILABLE_CONFIGS_DIR $ENABLED_CONFIGS_DIR \
| grep $AVAILABLE_CONFIGS_DIR \
| sed -re 's#.+: (.+)$#\1#')
else
configs=$@
fi
for c in $configs; do
echo -n "Enabling $c..."
[[ -f $ENABLED_CONFIGS_DIR/$c ]] && echo "Skipped" && continue
[[ -f $AVAILABLE_CONFIGS_DIR/$c ]] && \
ln -s $AVAILABLE_CONFIGS_DIR/$c $ENABLED_CONFIGS_DIR && \
echo "Done" && \
continue
echo "Error"
done
}
disable_configs()
{
local configs
if [[ $# -eq 0 || ${1,,} = 'all' ]]; then
configs=$(find $ENABLED_CONFIGS_DIR -type l -exec basename {} \;)
else
configs=$@
fi
for c in $configs; do
local config_path="$ENABLED_CONFIGS_DIR/$c"
echo -n "Disabling $c..."
[[ ! -L $config_path ]] && echo "Skipped" && continue
[[ -f $config_path ]] && rm $config_path && echo "Done" && continue
echo "Error"
done
}
case "$OP" in
start)
echo "Starting $DESC: "
do_start
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
do_stop
echo "$NAME."
;;
reload)
echo -n "Reloading $DESC: "
do_reload
echo "$NAME."
;;
force-reload)
echo -n "Force-reloading $DESC: "
do_force_reload
echo "$NAME."
;;
restart)
echo "Restarting $DESC: "
do_stop
sleep 1
do_start
echo "$NAME."
;;
status)
get_status
;;
enable)
shift
enable_configs $@
;;
disable)
shift
disable_configs $@
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|reload|force-reload|status|enable|disable}">&2
exit 1
;;
esac
exit 0
#!/bin/bash
# Wordpress backup script
# Author: yohanes.gultom@gmail.com, 2019
# Change variables below according to your setup
NOW=$(date +"%Y%m%d%H%M%S")
FILE="yohanes.gultom.me.$NOW.tar"
BACKUP_DIR="/home/yohanesgultom/backups/yohanes"
WP_HOME="/var/www/yohanes"
DB_NAME="wordpress"
DB_FILE="yohanes.gultom.me.$NOW.sql"
MAXFILE=3
# Do not edit codes below unless you know what you are doing
WP_PLUGINS="$WP_HOME/wp-content/plugins"
WP_THEMES="$WP_HOME/wp-content/themes"
WP_UPLOADS="$WP_HOME/wp-content/uploads"
WP_CONFIG="$WP_HOME/wp-config.php"
# Create WP backup with relative path
tar --transform="s,^${WP_HOME:1}/,," --show-transformed -cvf $BACKUP_DIR/$FILE $WP_PLUGINS $WP_THEMES $WP_UPLOADS $WP_CONFIG &&
# Create WP database backup using credentials from .my.cnf and add it to archive
mysqldump $DB_NAME > $BACKUP_DIR/$DB_FILE &&
# Append the dump to the archive, remove the dump and gzip the whole archive
tar --transform="s,^${BACKUP_DIR:1},database," --show-transformed --append --file=$BACKUP_DIR/$FILE $BACKUP_DIR/$DB_FILE &&
gzip -9 $BACKUP_DIR/$FILE &&
# Delete DB file
rm $BACKUP_DIR/$DB_FILE
# This will list our file by modification time and delete all but the MAXFILE most recent
MAXFILE=$((MAXFILE+1))
purging=$(ls -dt "$BACKUP_DIR"/* | tail -n +"$MAXFILE");
if [ "$purging" != "" ]; then
echo Purging old file: $purging;
rm -rf $purging;
else
echo "No file found for purging at this time";
fi
#!/bin/bash
# UPDATE: 2019-03-06 Turns out single zip for each language is available at http://wpaorg.wordproject.com/bibles/app/audio/{LANG_ID}.zip :p
# Politely (sequentially) downloading bible audio from WordProject website
# Usage: ./wordproject_audio_bible_download.sh [START] [END] [DELAY]
# START: first bible book code eg. Genesis -> 1, Exodus -> 2, Matthew -> 40, Revelation -> 66
# END: last bible book code eg. Genesis -> 1, Exodus -> 2, Matthew -> 40, Revelation -> 66
# DELAY: delay time in second between download
# @Author yohanes.gultom@gmail.com
START=$1
END=$2
DELAY=$3
BASE_URL="http://wpaorg.wordproject.com/bibles/app/audio"
BASE_DIR="$HOME/Audio/ALKITAB_TB_WORDPROJECT_ORG"
LANG_ID="14"
for (( i=$START; i<=$END; i++ )) do
n=$(printf %03d $i)
wget -c -O "${BASE_DIR}/${LANG_ID}_${n}.zip" "${BASE_URL}/${LANG_ID}_${i}.zip"
sleep $DELAY
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment