Alvaro Guimaraes

Thursday, January 8, 2009

PostgreSQL :: Script para backups automáticos

Segue script básico que eu uso para configurar backups periódicos no crontab.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .

# (c) Copyright Alvaro Guimaraes, 07/01/2009



#!/bin/bash


# bash em portugues para que o dia da semana esteja em portugues tambem
export LC_ALL='pt_BR'


if [ $UID -ne 0 ]; then
echo "Voce precisa ser root para executar esse script."
exit
fi

if [ -z $1 ] || [ `echo -e $1 | cut -d = -f 1` != "--path" ]; then
if [ ! -w $0 ]; then
echo "Este script precisa de permissao total."
exit
fi

echo -n "Nome do banco: "
if [ ! -z $1 ]; then
database=$1
echo $database
else
read database
fi

lines=(
"# Backup do banco de dados: $database"
"00 12 * * * root `pwd`/backups.sh --path=`pwd` --time=1 --database=$database > /dev/null"
"00 19 * * * root `pwd`/backups.sh --path=`pwd` --time=2 --database=$database > /dev/null"
"00 23 * * 1-5 root vacuumdb -U postgres -f -z $database"
"00 23 * * 6 root vacuumdb -Upostgres -a -f -z"
"00 07 * * 7 root reindexdb -Upostgres -a > /dev/null"
)

for counter in "${lines[@]}";
do
echo "$counter" >> /etc/crontab
done

echo "Feito."
else
pgDump=`whereis pg_dump | cut -d ' ' -f 2 `
tm=`echo $2 | cut -d = -f 2`
dtDay=`date | cut -c -3 | tr "[:upper:]" "[:lower:]"`
db=`echo $3 | cut -d = -f 2`
fName=`echo $1 | cut -d = -f 2`/$dtDay'_'$db$tm'.fc'
if [ -e $fName ]; then
echo "rm -f $fName"
fi
`$pgDump -U postgres -i -Fc -f $fName $db`

fi

Monday, January 5, 2009

Bash :: Teclas de atalho

Trabalhando com o histórico:
  • !! executa o último comando do history (pronuncia-se "bang bang")
  • !string executa o comando mais recente que comece com a string
  • !?string executa o comando mais recente que contenha a string
  • ^string1^string2 executa o último comando substituindo string1 por string2 na primeira ocorrência
Edição de comandos estilo Emacs:
  • Ctrl-L equivalente ao clear
  • Ctrl-K apaga tudo do cursor ao final da linha
  • Ctrl-Y cola o que foi apagado com Ctrl+K
  • Alt-D apaga do cursor até o fim da palavra
  • Ctrl-R busca palavra recursivamente
  • Ctrl-S busca palavra adiante