Skip to content

Instantly share code, notes, and snippets.

@yohanesgultom
Last active July 12, 2024 08:48
Show Gist options
  • 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
# 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")
bak_file="backup.$now.sql"
bak_file_gz="$bak_file.gz"
echo "Creating backup of all MySQL databases.."
time mysqldump --all-databases --single-transaction > "$bak_file"
tar zcvf "$bak_file_gz" "$bak_file"
rm "$bak_file"
echo "Backup file created: $bak_file_gz"
#!/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 [email protected]
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
# 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
# This will list our file by modification time and delete all but the 3 most recent
purging=$(ls -dt somedir/* | tail -n +3);
if [ "$purging" != "" ]; then
echo Purging old file: $purging;
rm -rf $purging;
else
echo "No file found for purging at this time";
fi
# Copied from https://gist.github.com/dougalsutherland/266983/9c88f1ca1cf1420af03166dcfccb9cb10a21c110
#!/bin/bash
exts=".aux .lof .log .lot .fls .out .toc .dvi .bbl .bcf .blg -blx.aux -blx.bib -blx.bib .run.xml .fdb_latexmk .synctex.gz .syntex.gz(busy) .pdfsync .algorithms .alg .loa .thm .nav .snm .vrb .acn .acr .glg .glo .gls .brf .lol .idx .ilg .ind .ist .maf .mtc .mtc0 .pyg .nlo .tdo .xdy .keys"
for x in "${@:-.}"; do
arg=$(echo ${x:-.} | perl -pe 's/\.(tex|pdf)$//')
if [[ -d "$arg" ]]; then
for ext in $exts; do
rm -f "$arg"/*$ext
done
else
for ext in $exts; do
rm -f "$arg"$ext
done
fi
done
rm -rf $(biber --cache) # gross biber bug
#!/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
### 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="[email protected]"
dbname="sirsak"
dbuser="sirsak"
# main script
# create sudo user
adduser "$dbuser"
usermod -aG sudo "$dbuser"
# ssh-copy-id [email protected]
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment