Este é um pequeno script que escrevi para fazer backup de minhas configurações de apache, arquivos da Web & db para o Amazon S3. O único propósito do servidor é um servidor web onde cada desenvolvedor usa seu próprio homedir. Portanto, há um arquivo em '/ home' com uma lista de usuários que precisam de backup.
#!/bin/bash
# 2012 Bart De Vos
# 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 2 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, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# location of this script
DIRECTORY=$(cd 'dirname $0' && pwd)
INSTALLER_FILENAME='basename $0'
# error handling
function error_exit
{
echo "Error on line ${1:-'Aborting script.'}" 1>&2
exit 1
}
function backup
{
# exit on errors
set -e
# getting hostname
HOSTNAME_FQDN='hostname --fqdn' || error_exit "${LINENO}: Error determening hostname"
echo "Determening timestamp"
timestampbackup='/bin/date +%Y%m%d%H' || error_exit "${LINENO}: Error determening timestamp"
echo "Create backupdir"
/bin/mkdir /backup/${timestampbackup} || error_exit "${LINENO}: Error creating backupdir"
echo "Dump and save MYSQL for all dbs"
/usr/bin/mysqldump --all-databases --opt -c -e -Q -uScriptRunner -pMySuperSecretPasswordThaIAlmostForgotToRemoveFromTheScript | /bin/gzip > /backup/${timestampbackup}/sqlbackup.sql.gz || error_exit "${LINENO}: Error backing up mysql"
echo "Itterate users in /home/.backup"
for username in 'awk -F: '{print $1}' /home/.backup'
do
echo "Backing up user: $username"
/bin/tar -czpf /backup/${timestampbackup}/${username}.tar.gz /home/${username} || error_exit "${LINENO}: Error backing up $username"
done
echo "Get configs"
/bin/tar -czpf /backup/${timestampbackup}/httpdconfigs.tar.gz /etc/httpd/conf.d/ || error_exit "${LINENO}: Error backing up httpd conf.d"
echo "Copy to S3 bucket"
/usr/bin/s3cmd --config=/backup/.s3cfg put /backup/ s3://${HOSTNAME_FQDN} --recursive || error_exit "${LINENO}: Error with transfer to S3"
echo "Remove backupdir"
/bin/rm /backup/${timestampbackup} -rf || error_exit "${LINENO}: Error deleting backupdir"
echo "All done!"
}
backup
Antes de poder usá-lo, você precisa instalar o s3cmd e criar um arquivo .config
com suas credenciais de segurança do S3. (Vai criar isso após o primeiro uso).
Não se esqueça de verificar / alterar os caminhos, porque eles podem diferir entre as versões / distros.