Created
February 24, 2019 21:54
-
-
Save thblckjkr/c2ee7d7d2299b21c4c5746eed5df140b to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
# You need to execute this bash file on tmux, to have an easy way to see the output | |
# then, just execute the script and have a daily report of the status | |
# | |
# Default ouput on $path/log.md can be readed by markdown and by a human | |
# | |
# Maybe you'll need to do some modifications | |
# Autor: Teo Gonzalez Calzada @thblckjkr | |
# Global variables | |
path="/home/$USER/backup" # path destination of restores | |
dbAuthFile="/srv/keys/database.ini" # ini file with credentials to open databases | |
# Get database ini passwords and user | |
username=$(awk -F "=" '/username/ {print $2}' $dbAuthFile | tr -d ' ') | |
password=$(awk -F "=" '/password/ {print $2}' $dbAuthFile | tr -d ' ') | |
# Get key to crypt | |
key=$(</srv/keys/decrypt_key) | |
# Define more things | |
enctype="-aes-256-cbc" # encryption type for files | |
mysqldatabases="test test2" # Databases to create a restore on mysql | |
codepath="ABSOLUTE_PATH" # Path of code to restore | |
# Checks if the file exists, if not create a log | |
if [ ! -e "$path/log.md" ] ; then | |
printf "#Backup restore point#\n" > $path/log.md | |
printf "Document log sucessfully started, system running" | |
fi | |
while : | |
do | |
printf "###Starting restore###\n\n" >> $path/log.md | |
printf "Starting restore point at `date`\n" | |
# Variables declaration | |
fDate=`date +%Y-%m-%d_%H-%M` # Date formatted for today | |
mysqldumpname="mysql_r$fDate.sql.gz" # Final name of the mysql dump | |
codedumpname="files_r$fDate.tar.gz" # Final name of the code files restoration | |
mongodumpname="mongo_r$fDate.tgz" # Final name of the MongoDB dump | |
## MySQL ## | |
#Create databases restore point | |
a=`mysqldump -u $username -p$password --extended-insert --databases $mysqldatabases | gzip > $path/$mysqldumpname` | |
b=`openssl enc $enctype -in $path/$mysqldumpname -out $path/$mysqldumpname.enc -k $key` | |
#Add info to the log | |
printf "GunZiped and encripted SQL Database with name **$mysqldumpname** at *`date`*\n\n" >> $path/log.md | |
printf "> dump: $a \n" >> $path/log.md | |
printf "> crypt: $b \n" >> $path/log.md | |
rm -rf $path/$mysqldumpname #remove unencrypted | |
## MySQL ## | |
## MongoDB ## | |
#Create a MongoDB restoration | |
g=`mongodump --out $path/mongo` | |
h=`sudo tar -zcf $path/$mongodumpname $path/mongo` | |
i=`openssl enc $enctype -in $path/$mongodumpname -out $path/$mongodumpname.enc -k $key` | |
#Add the info to the log | |
printf "GunZiped and encripted Mongo database with name **$codedumpname** at *`date`*\n\n" >> $path/log.md | |
printf "> mongo: $g \n" >> $path/log.md | |
printf "> tar: $h \n" >> $path/log.md | |
printf "> crypt: $h \n" >> $path/log.md | |
rm -rf $path/mongo | |
rm -rf $path/$mongodumpname | |
## MongoDB ## | |
## CODE ## | |
#Create a code restoration | |
d=`sudo tar -zcf $path/$codedumpname $codepath --exclude=''` | |
e=`openssl enc $enctype -in $path/$codedumpname -out $path/$codedumpname.enc -k $key` | |
#Add the info to the log | |
printf "GunZiped and encripted Code files with name **$codedumpname** at *`date`*\n\n" >> $path/log.md | |
printf "> tar: $d \n" >> $path/log.md | |
printf "> crypt: $e \n" >> $path/log.md | |
rm -rf $path/$codedumpname | |
## CODE ## | |
## CLEAN ## | |
# Delete code restorations older than 15 days | |
find $path -name "*.tar.gz.enc" -mtime +15 -exec rm -rf {} \; | |
# Delete mysql from 30 days of more | |
find $path -name "*.sql.gz.enc" -mtime +30 -exec rm -rf {} \; | |
# Delete mongodb of 30 days or older | |
find $path -name "*.tgz.enc" -mtime +30 -exec rm -rf {} \; | |
printf "> Removed unencripted files \n\n" >> $path/log.md | |
## CLEAN ## | |
## RE-RUN ## | |
# Wait's 1/2 day to run again | |
printf " *Restoration point end at `date`*\n\n --- \n\n" >> $path/log.md | |
printf "Restoration process ended, sleeping for one day\n" | |
sleep 12h | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment