Skip to content

Instantly share code, notes, and snippets.

@dhaupin
Last active November 20, 2017 15:05
Show Gist options
  • Save dhaupin/08bd631c7433976fefaabe795a35f4d8 to your computer and use it in GitHub Desktop.
Save dhaupin/08bd631c7433976fefaabe795a35f4d8 to your computer and use it in GitHub Desktop.
Random Bash Aliases & Functions
backup-oc() {
accnt=account
prefix=prefix
path=subfolder (no pre or trailing slash)
if [ ! -d "/backup/_SYNC/${accnt}" ]; then
mkdir -p /backup/_SYNC/${accnt}
fi
if [ ! -d "/home/${accnt}/_SYNC" ]; then
mkdir /home/${accnt}/_SYNC
fi
if [ ! -z ${path} ]; then
path=${path}/
fi
tar -pczvf /backup/_SYNC/${accnt}/${prefix}-$(date +%Y%m%d)-excludes.tar.gz /home/${accnt}/public_html/${path} --exclude=/home/${accnt}/public_html/${path}system/cache/* --exclude=/home/${accnt}/public_html/${path}image/cache/* --exclude=/home/${accnt}/public_html/${path}image/data/* --exclude=/home/${accnt}/public_html/${path}system/logs/* --exclude=/home/${accnt}/public_html/${path}download/* --exclude=/home/${accnt}/public_html/${path}vqmod/logs/* --exclude=/home/${accnt}/public_html/${path}vqmod/vqcache/*
cp /backup/_SYNC/${accnt}/${prefix}-$(date +%Y%m%d)-excludes.tar.gz /home/${accnt}/_SYNC/${prefix}-$(date +%Y%m%d)-excludes.tar.gz
chown ${accnt}:${accnt} /home/${accnt}/_SYNC/${prefix}-$(date +%Y%m%d)-excludes.tar.gz
echo -e "${cyan}${bold}Backup Complete - /home/${accnt}/_SYNC/${prefix}-$(date +%Y%m%d)-excludes.tar.gz${reset}"
}
backup-oc-full() {
accnt=account
prefix=prefix
path=subfolder (no pre or trailing slash)
if [ ! -d "/backup/_SYNC/${accnt}" ]; then
mkdir -p /backup/_SYNC/${accnt}
fi
if [ ! -d "/home/${accnt}/_SYNC" ]; then
mkdir /home/${accnt}/_SYNC
fi
if [ ! -z ${path} ]; then
path=${path}/
fi
tar -pczvf /backup/_SYNC/${accnt}/${prefix}-$(date +%Y%m%d)-full.tar.gz /home/${accnt}/public_html/${path} --exclude=/home/${accnt}/public_html/${path}system/cache/* --exclude=/home/${accnt}/public_html/${path}image/cache/* --exclude=/home/${accnt}/public_html/${path}system/logs/* --exclude=/home/${accnt}/public_html/${path}vqmod/logs/* --exclude=/home/${accnt}/public_html/${path}vqmod/vqcache/*
cp /backup/_SYNC/${accnt}/${prefix}-$(date +%Y%m%d)-full.tar.gz /home/${accnt}/_SYNC/${prefix}-$(date +%Y%m%d)-full.tar.gz
chown ${accnt}:${accnt} /home/${accnt}/_SYNC/${prefix}-$(date +%Y%m%d)-full.tar.gz
echo -e "${cyan}${bold}Backup Complete - /home/${accnt}/_SYNC/${prefix}-$(date +%Y%m%d)-full.tar.gz${reset}"
}
backup-vt() {
accnt=account
prefix=prefix
path=subfolder (no pre or trailing slash)
if [ ! -d "/backup/_SYNC/${accnt}" ]; then
mkdir -p /backup/_SYNC/${accnt}
fi
if [ ! -d "/home/${accnt}/_SYNC" ]; then
mkdir /home/${accnt}/_SYNC
fi
if [ ! -z ${path} ]; then
path=${path}/
fi
tar -pczvf /backup/_SYNC/${accnt}/${prefix}-$(date +%Y%m%d)-excludes.tar.gz /home/${accnt}/public_html/${path} --exclude=/home/${accnt}/public_html/${path}cache/* --exclude=/home/${accnt}/public_html/${path}test/templates_c/* --exclude=/home/${accnt}/public_html/${path}test/vtlib/*
cp /backup/_SYNC/${accnt}/${prefix}-$(date +%Y%m%d)-excludes.tar.gz /home/${accnt}/_SYNC/${prefix}-$(date +%Y%m%d)-excludes.tar.gz
chown ${accnt}:${accnt} /home/${accnt}/_SYNC/${prefix}-$(date +%Y%m%d)-excludes.tar.gz
echo -e "${cyan}${bold}Backup Complete - /home/${accnt}/_SYNC/${prefix}-$(date +%Y%m%d)-excludes.tar.gz${reset}"
}
backup-cs() {
accnt=account
prefix=prefix
path=subfolder (no pre or trailing slash)
if [ ! -d "/backup/_SYNC/${accnt}" ]; then
mkdir -p /backup/_SYNC/${accnt}
fi
if [ ! -d "/home/${accnt}/_SYNC" ]; then
mkdir /home/${accnt}/_SYNC
fi
if [ ! -z ${path} ]; then
path=${path}/
fi
tar -pczvf /backup/_SYNC/${accnt}/${prefix}-$(date +%Y%m%d)-excludes.tar.gz /home/${accnt}/public_html/${path} --exclude=/home/${accnt}/public_html/${path}images/* --exclude=/home/${accnt}/public_html/${path}var/attachments/* --exclude=/home/${accnt}/public_html/${path}var/cache/* --exclude=/home/${accnt}/public_html/${path}var/backups/* --exclude=/home/${accnt}/public_html/${path}var/files/*/exim/* --exclude=/home/${accnt}/public_html/${path}var/files/*/exim.tar.gz
cp /backup/_SYNC/${accnt}/${prefix}-$(date +%Y%m%d)-excludes.tar.gz /home/${accnt}/_SYNC/${prefix}-$(date +%Y%m%d)-excludes.tar.gz
chown ${accnt}:${accnt} /home/${accnt}/_SYNC/${prefix}-$(date +%Y%m%d)-excludes.tar.gz
echo -e "${cyan}${bold}Backup Complete - /home/${accnt}/_SYNC/${prefix}-$(date +%Y%m%d)-excludes.tar.gz${reset}"
}
backup-cs-full() {
accnt=account
prefix=prefix
path=subfolder (no pre or trailing slash)
if [ ! -d "/backup/_SYNC/${accnt}" ]; then
mkdir -p /backup/_SYNC/${accnt}
fi
if [ ! -d "/home/${accnt}/_SYNC" ]; then
mkdir /home/${accnt}/_SYNC
fi
if [ ! -z ${path} ]; then
path=${path}/
fi
tar -pczvf /backup/_SYNC/${accnt}/${prefix}-$(date +%Y%m%d)-full.tar.gz /home/${accnt}/public_html/${path} --exclude=/home/${accnt}/public_html/${path}var/cache/* --exclude=/home/${accnt}/public_html/${path}var/backups/* --exclude=/home/${accnt}/public_html/${path}var/files/*/exim/* --exclude=/home/${accnt}/public_html/${path}var/files/*/exim.tar.gz
cp /backup/_SYNC/${accnt}/${prefix}-$(date +%Y%m%d)-full.tar.gz /home/${accnt}/_SYNC/${prefix}-$(date +%Y%m%d)-full.tar.gz
chown ${accnt}:${accnt} /home/${accnt}/_SYNC/${prefix}-$(date +%Y%m%d)-full.tar.gz
echo -e "${cyan}${bold}Backup Complete - /home/${accnt}/_SYNC/${prefix}-$(date +%Y%m%d)-full.tar.gz${reset}"
}
# Exports
export EDITOR=vim
# Custom Colors
black=$(tput setaf 0)
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
magenta=$(tput setaf 5)
cyan=$(tput setaf 6)
white=$(tput setaf 7)
bgblack=$(tput setab 0)
bgred=$(tput setab 1)
bggreen=$(tput setab 2)
bgyellow=$(tput setab 3)
bgblue=$(tput setab 4)
bgmagenta=$(tput setab 5)
bgcyan=$(tput setab 6)
bgwhite=$(tput setab 7)
bold=$(tput bold)
reset=$(tput sgr0)
# Custom Aliases
alias clr="clear && printf '\e[3J'"
alias grepr="grep -RiI --color=always"
alias lsa="ls -lah"
alias csfr='csf -ra'
alias apfr='apf -r'
alias mkdir='mkdir -pv'
alias home='cd ~/ && ls -lAh'
alias apt-update='apt-get update && apt-get upgrade'
# Custom Functions
suser() {
su - -s /bin/bash $1 && cd $HOME
}
bashr() {
echo -e "${cyan}${bold}Reloading $HOME/.bashrc${reset}"
. $HOME/.bashrc
echo -e "${cyan}${bold}Reloading /etc/bash.bashrc${reset}"
. /etc/bash.bashrc
}
wwwr() {
if nginx -t 2>/dev/null; then
echo -e "\n${cyan}${bold}Restarting Nginx:${reset}"
service nginx restart
service nginx status
echo -e "\n${cyan}${bold}Restarting PHP-FPM:${reset}"
service php*-fpm restart
service php*-fpm status
fi
}
uwsgir() {
if nginx -t 2>/dev/null; then
echo -e "\n${cyan}${bold}Restarting Nginx:${reset}"
service nginx restart
service nginx status
echo -e "\n${cyan}${bold}Restarting UWSGI:${reset}"
service uwsgi restart
service uwsgi status
fi
}
odoor() {
echo -e "\n${cyan}${bold}Restarting Odoo Server:${reset}"
service odoo-server restart
service odoo-server status
echo -e "\n${cyan}${bold}Restarting Nginx:${reset}"
service nginx restart
service nginx status
}
mailr() {
echo -e "\n${cyan}${bold}Restarting Amavis-New:${reset}"
service amavis restart
service amavis status
echo -e "\n${cyan}${bold}Restarting Postfix:${reset}"
service postfix restart
service postfix status
echo -e "\n${cyan}${bold}Restarting Dovecot:${reset}"
service dovecot restart
service dovecot status
}
maillog() {
if [ -z "$1" ]; then
less +F -I /var/log/mail{.,}log
else
less +F -Ip $1 /var/log/mail{.,}log
fi
}
chkmem() {
# Shows memory use for a process
# chkmem [process] [count] [delay]
if [ -z "$1" ]; then
printf "${cyan}${bold}A process/service name is required.${reset}\n"
return
else
process=$1
fi
if [ -z "$2" ]; then
repeat=1
else
repeat=$2
fi
if [ -z "$3" ]; then
delay=0.5
else
delay=$3
fi
for ((n=0; n < ${repeat}; n++)); do
ps -ylC ${process} --sort:rss | awk -v process=${process} '!/RSS/ {pool += $8; per += 1} END {print "Process "process" using: "int(pool/1024)"M ("per" running, ~"int(pool/((per-1)*1024))"M each)"}'
sleep ${delay}
done
}
chksize() {
du -sh $1
}
chkports() {
netstat -tulpn | less
}
chkchange() {
if [ -z "$2" ]; then
countd="cat"
echo -e "${cyan}All modified files:${reset}"
else
countd="head -n $2"
echo -e "${cyan}Last $2 modified files:${reset}"
fi
find $1 -type f -printf '%TY-%Tm-%Td %TT %p\n' | sort -r | ${countd}
}
chkos() {
if gcc --version | grep -qi "ubuntu"; then
echo "${cyan}${bold}ubuntu${reset}"
elif gcc --version | grep -qi "red hat"; then
echo "${cyan}${bold}red hat${reset}"
else
echo "${cyan}${bold}other${reset}"
fi
}
chkboot() {
if [ -f /var/run/reboot-required ]; then
echo "${cyan}${bold}Reboot Required${reset}"
else
echo "${cyan}${bold}No Reboot Required${reset}"
fi
}
chkip() {
ip=`dig TXT +short o-o.myaddr.l.google.com @ns1.google.com | sed 's/"//g'`
host=`host ${ip}`
div="\u27A4"
echo -e "${cyan}${bold}${ip} ${div} ${host}${reset}"
}
chkdeb() {
echo -e "${cyan}${bold}Latest Installed Packages:${reset}"
zgrep -sh . /var/log/dpkg.log{.[0-9]{.gz,},} | awk '$3~/^install$/ {print $1" "$2" "$4;}' | tail $1
}
flush-redis() {
echo -e "${cyan}${bold}Clearing Redis Cache${reset}"
redis-cli flushall
}
flush-pagespeed() {
if [ -z "$1" ]; then
psdir="/var/mod_pagespeed/cache/"
else
psdir=$1
fi
read -r -p "Delete ${psdir}? [y/N]" response
response=${response,,} # tolower
if [[ $response =~ ^(yes|y) ]]; then
printf "${cyan}${bold}Deleting ${psdir} ${reset}\n"
rm -rf ${psdir}
elif [[ $response =~ ^(no|n) ]]; then
printf "${cyan}${bold}Creating Flush File ${psdir}cache.flush ${reset}\n"
touch ${psdir}cache.flush
elif [ -z $response ]; then
printf "${cyan}${bold}Invalid Option, Aborting ${reset}\n"
fi
}
cp-from() {
# Copies things based on a file containing newline seperated paths
set -e
if [ -z "$1" ] || [ -z "$2" ]; then
printf "${cyan}${bold}Useage: cp-from file.txt destination_directory${reset}\n"
if [ -z "$1" ]; then
printf "${cyan}${bold}Error: missing input file argument.${reset}\n"
fi
if [ ! -f $1 ]; then
printf "${cyan}${bold}Error: file does not exist.${reset}\n"
fi
if [ -z "$2" ]; then
printf "${cyan}${bold}Error: missing output directory argument.${reset}\n"
fi
return
fi
cp --parents -av $(<$1) $2
printf "${cyan}${bold}Copied files from $1 into $2.${reset}\n"
}
# Specific CentOS on cPanel aliases and functions
alias less-sys="grep -Ev --color=always '(127.0.0.1|10.30.9.*|209.203.197.*|socket\ failed)' /var/log/messages /var/log/secure | less +F -R"
alias less-dom="grep -Evsh --color=always '(LiquidWeb|nagios|209.203.197.*|Google|bing|Baidu|Netcraft|majestic12|207.198.99*)' /usr/local/apache/domlogs/{50.28.34.226,206.196.110.58} | less +F -R"
alias less-access="grep -Ev --color=always '(127.0.0.1|whm-server-status|internal\ dummy|majestic12|x16)' /usr/local/apache/logs/access_log | less +F -R"
alias less-error="grep -Ev --color=always '(2015]|127.0.0.1|209.203.197.*|207.198.99.*|invalid\ method|majestic12|501.shtml|400.shtml|apple-touch|apple-app-site|mod_fcgid|mpm_event)' /usr/local/apache/logs/error_log | less +F -R"
chkboot() {
LAST_KERNEL=$(rpm -q --last kernel | perl -pe 's/^kernel-(\S+).*/$1/' | head -1)
CURRENT_KERNEL=$(uname -r)
test $LAST_KERNEL = $CURRENT_KERNEL || echo REBOOT
}
chkrpm() {
echo -e "${cyan}${bold}Latest Installed Packages:${reset}"
rpm -qa --queryformat '%{installtime} (%{installtime:date}) %{name}\n' | sort -n | tail -$1
}
wwwr() {
echo -e "\n${cyan}${bold}Restarting Apache:${reset}"
service httpd restart
service httpd status
echo -e "\n${cyan}${bold}Restarting PHP-FPM:${reset}"
/scripts/restartsrv_cpanel_php_fpm
}
fpmr() {
/scripts/restartsrv_apache_php_fpm
printf "${cyan}${bold}Restarted Apache public PHP-FPM service.${reset}\n"
}
whm-fpmr() {
/scripts/restartsrv_cpanel_php_fpm
printf "${cyan}${bold}Restarted cPanel Internal PHP-FPM service.${reset}\n"
}
cpscrub() {
path=/usr/local/cpanel/cgi-sys/
for file in ${path}Count.cgi ${path}countedit.cgi ${path}entropybanner.cgi ${path}entropybanner.cgi-wrap ${path}entropysearch.cgi ${path}formmail.cgi ${path}FormMail.cgi ${path}FormMail-clone.cgi ${path}formmail.pl ${path}FormMail.pl ${path}guestbook.cgi ${path}randhtml.cgi ${path}randhtml.cgi-wrap
do
if [ -f ${file} ]; then
chmod 000 ${file}
echo "${cyan}${bold}Chmod ${file} with permission 000${reset}"
fi
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment