Olá a todos!!!
Mais uma vez eu trago um script desenvolvido por mim para fazer backup de arquivos de um servidor, compactando-os e salvando-os em um ponto de montagem (de preferência de rede), ele guarda os arquivos de backup por 1 mês e então vai removendo pelo mais antigo. Originalmente esse script possuía 6 linhas (versão 1.0), mas como sempre eu me empolgo e tento fazer o mais completo possível. Eis o script:
#!/bin/bash
##########################################################################
# Script de backup 2.1 (23/05/2013)
# Desenvolvido por Rodrigo Garcia em 19/10/2012
##########################################################################
PATH=/sbin:/usr/sbin:/bin:/usr/bin
##########################################################################
# Informe os arquivos a serem copiados no campo BACKUP="" separados por espaços
##########################################################################
BACKUP=""
##########################################################################
# Informe o ponto de montagem Windows a ser utilizado no backup, ex: "//192.168.0.254/compartilhamento"
##########################################################################
MOUNT=""
##########################################################################
# Informe o username e a senha nos campos abaixo
##########################################################################
USER=""
PASSWD=""
##########################################################################
FN_INS()
{
echo "Deseja realmente instalar o script de backup (s/n)?"
read RESPOSTA
case $RESPOSTA in
"s")
echo "Criando ponto de montagem de rede..."
echo "$MOUNT cifs username=$USER,password=$PASSWD,user,dir_mode=0777,file_mode=0777 0 0 # Backup" >> /etc/fstab
mkdir /mnt/bkp
echo "Montando diretório de backup..."
mount -a
echo "Script de backup instalado com sucesso!!!"
;;
"n")
exit
;;
*)
echo "Opção Inválida!!!"
exit
;;
esac
}
FN_UNS()
{
echo "Deseja realmente desinstalar o script de backup (s/n)?"
read RESPOSTA
case $RESPOSTA in
"s")
echo "Excluindo ponto de montagem de rede..."
sed -i '/# Backup/d' /etc/fstab
echo "Desmontando diretório de backup..."
umount /mnt/bkp
rm -rf /mnt/bkp
echo "Script de backup desinstalado com sucesso!!!"
;;
"n")
exit
;;
*)
echo "Opção Inválida!!!"
exit
;;
esac
}
FN_HLP()
{
echo "Script de Backup de Arquivos 2.1"
echo "Desenvolvido por Rodrigo Garcia"
echo "Opções:"
echo "install instala automaticamente o ponto de montagem"
echo "uninstall remove automaticamente o ponto de montagem"
echo "run executa a rotina de backup"
echo "help mostra essa mensagem"
}
FN_TAR()
{
echo "Compactando os arquivos $BACKUP..."
for BKP in $BACKUP
do
tar -Pczf $(hostname)-$(date +%Y-%m-%d).tar.gz $BACKUP
done
}
FN_CP()
{
test -d /mnt/bkp/$(hostname)
if [ $? == 1 ]
then
echo "Criando diretorio /mnt/bkp/$(hostname)..."
mkdir /mnt/bkp/$(hostname)
fi
echo "Copiando o backup para a pasta /mnt/bkp/$(hostname)..."
cp $(hostname)-$(date +%Y-%m-%d).tar.gz /mnt/bkp/$(hostname)
}
FN_RM()
{
echo "Excluindo arquivo local..."
rm -rf $(hostname)-$(date +%Y-%m-%d).tar.gz
echo "Excluindo backups antigos..."
find /mnt/bkp/$(hostname) -maxdepth 1 -ctime +1 -exec rm -Rf {} \;
}
case $1 in
"install")
FN_INS
exit
;;
"uninstall")
FN_UNS
exit
;;
"run")
FN_TAR
FN_CP
FN_RM
echo "Backup Executado Com Sucesso!!!"
exit
;;
"help")
FN_HLP
exit
;;
*)
echo "Use $0 {install|uninstall|run|help}"
exit
;;
esac
Espero que seja útil, e até a próxima!!!
Nenhum comentário:
Postar um comentário