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 7 x32
# some commands require user inputs (eg. password)
# 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"
su "$dbuser"
sudo apt-get update
# setup tomcat
sudo apt-get install tomcat7
sudo apt-get install openjdk-7-jdk
echo "export JAVA_HOME=/usr/lib/jvm/default-java" >> ~/.bashrc
# setup db
locale-gen
echo "export LC_ALL=\"en_US.UTF-8\"" >> ~/.bashrc
source ~/.bashrc
sudo apt-get install postgresql
sudo pg_createcluster 9.3 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
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
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" ~
# setup routing
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to 8080
# set timezone
sudo dpkg-reconfigure tzdata
# add autorestart job
crontab -l > editcron
echo "0 0 * * * /home/ubuntu/deploylatest.sh" >> editcron
crontab editcron
rm editcron
sudo service cron restart
#!/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/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
# 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=/opt/tomcat/apache-tomcat-7.0.76
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
# 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