Skip to content

Instantly share code, notes, and snippets.

@gishi-yama
Last active February 26, 2018 17:58
Show Gist options
  • Save gishi-yama/917157d8b1c61b6f0edbfbb014b98718 to your computer and use it in GitHub Desktop.
Save gishi-yama/917157d8b1c61b6f0edbfbb014b98718 to your computer and use it in GitHub Desktop.
オンプレ鯖のスクリプト
#!/bin/bash
# -------------
# How to use.
# ------------
# DB vaccum and dump.
# Execute the file as a root.
# If you vacuum and dump "foo" database, execute command :`daily-dump foo`
# ------------
if [ "$1" = "" ]
then
echo "Unknown dbname."
echo "If you vacuum and dump "foo" database, execute command :daily-dump foo"
exit 1
fi
DATE=`date +%Y%m%d_%H%M%S`;
BACKUP_TO="/data/daily-dumps";
PGHOME="/opt/postgresql";
WORK_DIR="/tmp";
DBNAME=$1;
cd $WORK_DIR
## vaccum
echo "Start daily-vacuum...";
su - postgres -c "nice -n 19 ${PGHOME}/bin/vacuumdb -z -d ${DBNAME}";
## dump
echo "Start daily-dump...";
for i in `seq 1 13`
do
current=$((14-$i))
prev=$(($current-1))
from="${BACKUP_TO}/${DBNAME}-dump.${prev}.tar.gz"
to="${BACKUP_TO}/${DBNAME}-dump.${current}.tar.gz"
echo $from
echo $to
if [ -e $from ]; then
su - postgres -c "nice -n 19 mv -f ${from} ${to}"
fi
done
su - postgres -c "nice -n 19 ${PGHOME}/bin/pg_dump -f ${DBNAME}_${DATE}.pgdb -U postgres -Fc -O -x ${DBNAME}";
su - postgres -c "nice -n 19 tar -cvzf ${BACKUP_TO}/${DBNAME}-dump.1.tar.gz ${DBNAME}_${DATE}.pgdb";
su - postgres -c "nice -n 19 rm -Rf ${DBNAME}_${DATE}.pgdb";
exit 0
#!/bin/sh
# SSHのアクセスを日本国内に限定
echo '' > /etc/hosts.allow
echo 'all: 127.0.0.1' >> /etc/hosts.allow
echo 'sshd: 210.128.52.4' >> /etc/hosts.allow
# see http://nami.jp/ipv4bycc/
wget -q http://nami.jp/ipv4bycc/mask.txt.gz
gunzip mask.txt.gz
sed -n 's/^JP\t/sshd: /p' mask.txt >> /etc/hosts.allow
rm -rf mask.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment