Created
May 7, 2020 02:49
-
-
Save aschiavon91/19041d70f9928d9b7a6fb45d69e6d79b to your computer and use it in GitHub Desktop.
mysql + web path bkp script with asset encoded using gpg
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 | |
| readonly BKP_TIME_STAMP=$(date +"%Y-%m-%d") | |
| readonly BKP_PATH_NAME=bkp-$BKP_TIME_STAMP | |
| readonly RECIPIENT_MAIL="rm85297@fiap.com.br" | |
| function check_mysql_config_file() { | |
| if [[ ! -f ./.my.cnf ]] | |
| then | |
| echo "Arquivo configuração do mysql não existe" | |
| echo "crie um arquivo .my.cnf com as infos necessárias" | |
| exit 1 | |
| fi | |
| } | |
| function check_gpg_pass_file() { | |
| if [[ ! -f ./.gpg_pass ]] | |
| then | |
| echo "Arquivo de senha gpg não existe" | |
| echo "crie um arquivo .gpg_pass contendo a senha do gpg em base64" | |
| exit 1 | |
| fi | |
| } | |
| function backup_database() { | |
| readonly DB_BKP_FILE_NAME=feirinha_mysql_$BKP_TIME_STAMP.dump | |
| echo "[Working] Backuping Mysql Data" | |
| mysqldump --defaults-file=../.my.cnf feirinha > $DB_BKP_FILE_NAME | |
| bzip2 -zq $DB_BKP_FILE_NAME | |
| echo "[OK] Backuping Mysql Data" | |
| } | |
| function backup_wordpress() { | |
| readonly WP_BKP_FILE_NAME=feirinha_$BKP_TIME_STAMP.tar.bz2 | |
| echo "[Working] Backuping Wordpress Files" | |
| mkdir -p ./wordpress | |
| cp -r /var/www/html/* ./wordpress/ | |
| tar -cjf $WP_BKP_FILE_NAME ./wordpress/ | |
| echo "[OK] Backuping Wordpress Files" | |
| } | |
| function encode_zip_files() { | |
| readonly DECODED_GPG_PASS=$(cat ../.gpg_pass | base64 -d) | |
| echo "[working] Encoding file all zipfile" | |
| for zipfile in $(find ./* -name "*.bz2"); do | |
| echo "[working] Encoding file ${zipfile}" | |
| gpg --homedir /home/aschiavon/.gnupg \ | |
| --batch --yes \ | |
| -r $RECIPIENT_MAIL -o "$zipfile".enc \ | |
| --passphrase $DECODED_GPG_PASS \ | |
| --pinentry-mode loopback \ | |
| --sign --armor \ | |
| --encrypt "$zipfile" | |
| echo "[OK] Encoding file ${zipfile}" | |
| done | |
| echo "[OK] Encoding file all zipfile" | |
| } | |
| function extract_encripted_files() { | |
| for encfile in $(find ./$BKP_PATH_NAME/* -name "*.enc"); do | |
| echo "[working] Encoding file all zipfile" | |
| mv $encfile . | |
| echo "[OK] Encoding file all zipfile" | |
| done | |
| } | |
| function cleanup() { | |
| echo "[working] Cleaning things" | |
| rm -fr ./$BKP_PATH_NAME | |
| rm -fr *.bz2 | |
| echo "[OK] Cleaning things" | |
| } | |
| function backup() { | |
| cd /etc/backups | |
| check_mysql_config_file | |
| check_gpg_pass_file | |
| mkdir -p $BKP_PATH_NAME | |
| cd $BKP_PATH_NAME | |
| backup_database | |
| backup_wordpress | |
| encode_zip_files | |
| cd ../ | |
| extract_encripted_files | |
| cleanup | |
| } | |
| function restore() { | |
| echo "Decodando todos os arquivos .enc nesta pasta" | |
| for encoded_file in $(find ./* -name "*.enc"); do | |
| echo "[Decripting] ${encoded_file}" | |
| gpg -d $encoded_file > ${encoded_file//.enc/} | |
| echo "[Decripted] ${encoded_file}" | |
| done | |
| for bzip_file in $(find ./* -name "*.bz2"); do | |
| echo "[Decompressing] $bzip_file" | |
| bzip2 -d $bzip_file | |
| echo "[Decompressed] $bzip_file" | |
| done | |
| for tar_file in $(find ./* -name "*.tar"); do | |
| echo "[Unziping] $tar_file" | |
| tar -xf $tar_file | |
| echo "[Unziped] $tar_file" | |
| done | |
| } | |
| case $1 in | |
| backup | BACKUP ) backup ;; | |
| restore | RESTORE ) restore ;; | |
| *) echo "Opção inválida" ;; | |
| esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment