Created
February 9, 2017 23:17
-
-
Save brentrjones/fac9f8c2c36ae7bea11ad3ffd5b5a60f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Aims to be a rather generic script to perform backups on various things | |
# Local edits can just add functions, and declare the usage in exec_begin (or omit functions) | |
# Flag to use GPG | |
GPG="True" | |
GPG_KEYFILE=/root/gpg.key | |
if [ $GPG == "True" ]; then | |
if [ -f $GPG_KEYFILE ]; then | |
source $GPG_KEYFILE | |
else | |
echo "Missing GPG keyfile" | |
exit 1 | |
fi | |
fi | |
NOW=$(date +"%Y-%m-%d.%T") | |
HOSTNAME=`hostname -f` | |
BACKUP_ROOT=/exports/backups01 | |
BACKUP=$BACKUP_ROOT/$HOSTNAME/backup.$NOW | |
BACKUP_ROOTHOST=$BACKUP_ROOT/$HOSTNAME/ | |
SCP_DST="" | |
DIRS="/etc" | |
TAR_FILE="fs-full-$NOW.tar.gz" | |
exec_begin() | |
{ | |
prep | |
# Put the functions here you want to backup | |
#exec_pgsql_backups | |
#exec_scp_backups | |
exec_tar_backups | |
} | |
prep() | |
{ | |
# Make $now backup folder | |
[ ! -d $BACKUP ] && mkdir -p $BACKUP || : | |
} | |
exec_tar_backups() | |
{ | |
tar=`tar -zcvf $BACKUP/$TAR_FILE $DIRS` | |
if [ $? -eq 1 ]; then | |
echo "tar Failed" | |
exit 1 | |
else | |
if [ $GPG == "True" ]; then | |
gpg --yes --batch --passphrase=$KEY -c $BACKUP/$TAR_FILE | |
if [ $? -eq 0 ]; then | |
TARSTATUS=0 | |
rm -f $BACKUP/$TAR_FILE | |
report | |
else | |
echo "GPG failure" | |
exit 1 | |
fi | |
fi | |
fi | |
} | |
exec_pgsql_backups() | |
{ | |
# PGSQL Backups | |
PGSQL="True" | |
databases=`psql -U postgres -l -t | cut -d'|' -f1 | sed -e 's/ //g' -e '/^$/d'` | |
echo -e "Databases:\n$databases" | |
for i in $databases; do | |
if [ "$i" != "template0" ] && [ "$i" != "template1" ]; then | |
echo Dumping $i to $BACKUP/$i-$NOW | |
pg_dump -U postgres -Fc $i > "$BACKUP/$i-$NOW.sql" | |
if [ $? -eq 1 ]; then | |
MESSAGE="PgSQL backup failed on: $i, at $NOW" | |
SQLSTATUS=1 | |
else | |
gpg --yes --batch --passphrase=$KEY -c $BACKUP/$i-$NOW.sql | |
if [ $? -eq 1 ]; then | |
echo "pSQL GPG Failed" | |
exit 1 | |
else | |
rm -f $BACKUP/$i-$NOW.sql | |
fi | |
fi | |
fi | |
done | |
report | |
} | |
report() | |
{ | |
content=`ls $BACKUP` | |
current_size=`du -hs $BACKUP` | |
total_size=`du -hs $BACKUP_ROOTHOST` | |
message="Backup time: $NOW | |
Backed up Directories: | |
$DIRS | |
Backed up Databases: | |
$databases | |
Backup Encrypted=$GPG | |
Backup content: $content | |
Current Backup size: $current_size | |
Total Backup size: $total_size | |
" | |
/usr/local/bin/slack_say "$message" "#sandbox" "HEALTHY" | |
} | |
exec_scp_backups() | |
{ | |
scp -r $BACKUP $SCP_DST | |
if [ $? -eq 0 ]; then | |
echo "Backup SCP to $SCP_DST complete" | |
else | |
echo "Backup SCP failed" | |
exit 1 | |
fi | |
} | |
exec_begin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment