Last active
August 29, 2015 14:06
-
-
Save boTux/eec18df9cdcb55eab862 to your computer and use it in GitHub Desktop.
Wordpress bash multi-backup script - MySQL + Data + GIT over SSH
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 | |
# --------------------------------------------------------------------------- | |
# multi-backup-wordpress.sh - Mysql/Data/Git Backup Wordpress. | |
# Copyright 2014, Romain Fluttaz, boTux.fr, <[email protected]> | |
# 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 3 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 at <http://www.gnu.org/licenses/> for | |
# more details. | |
# Usage: multi-backup-wordpress.sh [-h|--help] [-a|--auto] [-s|--sql sql_file] [-d|--data data_file] [-g|--git] [-i|--info] | |
# Revision history: | |
# 2014-09-22 Creation, Mise en place structure. | |
# --------------------------------------------------------------------------- | |
# Structure requise : | |
# WebHost | |
# ├── www/ | |
# │ wordpress directory | |
# ├── backups/ | |
# │ ├── dump | |
# │ ├── data | |
# │ ├── sql | |
# │ └── tmp | |
# │ backups directory | |
# └ admin.sh | |
# Do not store admin.sh in an apache accessible directory or limit access right with .htaccess. | |
# --------------------------------------------------------------------------- | |
# ~~[Configuration]~~ | |
## Repertoires de travail : | |
BASE="/home/user/directory/blog" | |
WWWROOT="www" | |
## Suffix des fichiers dump : | |
SQLFILE="blog.backup" # => blog.backup-[sql_file].sql.gz | |
ARCHIVE="blog.backup" # => blog.backup-[data_file].tar.gz | |
## MySQL Connection : | |
SQL_SERVER="" | |
SQL_USER="" | |
SQL_DBNAME="" | |
SQL_PASSWORD="" | |
# ~~[/Configuration]~~ | |
PROGNAME=${0##*/} | |
VERSION="0.2" | |
clean_up() { # Perform pre-exit housekeeping | |
return | |
} | |
error_exit() { | |
echo -e "${PROGNAME}: ${1:-"Unknown Error"}" >&2 | |
clean_up | |
clean_exit 1 | |
} | |
clean_exit() { | |
clean_up | |
exit | |
} | |
signal_exit() { # Handle trapped signals | |
case $1 in | |
INT) | |
error_exit "Program interrupted by user" ;; | |
TERM) | |
echo -e "\n$PROGNAME: Program terminated" >&2 | |
clean_exit ;; | |
*) | |
error_exit "$PROGNAME: Terminating on unknown signal" ;; | |
esac | |
} | |
usage() { | |
echo -e "Usage: $PROGNAME [-h|--help] [-a|--auto] [-s|--sql sql_file] [-d|--data data_file] [-g|--git] [-i|--info]" | |
} | |
help_message() { | |
cat <<- _EOF_ | |
$PROGNAME ver. $VERSION | |
Administration blog.boTux.fr | |
$(usage) | |
Options: | |
-h, --help Display this help message and clean_exit. | |
-a, --auto Sauvegarde Automatique Mysql et Data Dumps | |
-s, --sql sql_file Sauvegarde MySQL | |
Where 'sql_file' is the Mysql Dump. | |
-d, --data data_file Sauvegarde Data | |
Where 'data_file' is the Data Dump. | |
-g, --git Git Actions | |
-i, --info Informations | |
_EOF_ | |
return | |
} | |
# Trap signals | |
trap "signal_exit TERM" TERM HUP | |
trap "signal_exit INT" INT | |
# Declaration des fonctions | |
# | |
AUTO() { # -a | Sauvegarde Automatique Mysql et Data Dumps | |
echo -e "------------------>" | |
echo -e ${Green}"Initialisation..." | |
# Init DateStamp + Directory check. | |
today=$(date '+%Y.%m.%d-%H.%M') | |
cd $BASE/backups/tmp/ | |
echo -e ${Cyan}" - Date : $today" | |
echo " - Dossier : $BASE/backups/dump" | |
# SQL DUMP | |
echo -e ${Green}"Sauvegarde de la base de donnée..." | |
echo -e ${Cyan}"=> Sortie :"${White} | |
mysqldump -v --host=$SQL_SERVER --user=$SQL_USER --password=$SQL_PASSWORD $SQL_DBNAME > $SQLFILE-dump-$today.sql | |
echo -e ${Green}"Compression..." | |
echo -e ${Cyan}"=> Sortie :"${White} | |
gzip $SQLFILE-dump-$today.sql -v | |
echo -e ${Green}"Verification..."$NC | |
ls -la $BASE/backups/tmp | grep $SQLFILE-dump-$today | |
echo -e ${Blue}"---------------------- [${Green}Base de donnée Sauvegardé${Blue}]$NC" | |
# Data DUMP | |
echo -e ${Green}"Sauvegarde des données..." | |
cd $BASE | |
echo -e ${Cyan}"=> Sortie :"${White} | |
tar -czf $BASE/backups/tmp/"$ARCHIVE"-"$today".tar.gz $WWWROOT --exclude="$WWWROOT/.git" -C $BASE/ | |
echo -e ${Green}"Verification..."$NC | |
ls -la $BASE/backups/tmp/ | grep "$ARCHIVE"-"$today".tar.gz | |
echo -e ${Blue}"---------------------- [${Green}Données Sauvegardé${Blue}]$NC" | |
# Archive Finale | |
echo -e ${Green}"Création de l'archive..." | |
cd $BASE/backups/tmp/ | |
echo -e ${Cyan}"=> Sortie :"${White} | |
tar -cvf $BASE/backups/dump/"$ARCHIVE"-"$today".tar $SQLFILE-dump-$today.sql.gz $ARCHIVE-$today.tar.gz | |
rm $SQLFILE-dump-$today.sql.gz $ARCHIVE-$today.tar.gz | |
echo -e ${Green}"Verification..."$NC | |
ls -la $BASE/backups/dump/ | grep "$ARCHIVE"-"$today".tar | |
echo -e ${Blue}"---------------------- [${Green}Sauvegarde Automatique Accomplie !${Blue}]$NC" | |
clean_exit | |
} | |
SQL() { # -s | Sauvegarde MySQL | |
echo -e "------------------>" | |
echo -e ${Green}"Initialisation..." | |
# Init DateStamp + Directory check. | |
today=$(date '+%Y.%m.%d-%H.%M') | |
cd $BASE/backups/sql/ | |
echo -e ${Cyan}" - Date : $today" | |
echo " - Dossier : $BASE/backups/sql" | |
# SQL DUMP | |
echo -e ${Green}"Sauvegarde de la base de donnée..." | |
echo -e ${Cyan}"=> Sortie :"${White} | |
mysqldump -v --host=$SQL_SERVER --user=$SQL_USER --password=$SQL_PASSWORD $SQL_DBNAME > $SQLFILE-$sql_file-$today.sql | |
echo -e ${Green}"Compression..." | |
echo -e ${Cyan}"=> Sortie :"${White} | |
gzip $SQLFILE-$sql_file-$today.sql -v | |
echo -e ${Green}"Verification..."$NC | |
ls -la $BASE/backups/sql | grep $SQLFILE-$sql_file-$today | |
echo -e ${Blue}"---------------------- [${Green}Base de donnée Sauvegardé !${Blue}]$NC" | |
clean_exit | |
} | |
DATA() { # -d | Sauvegarde Data | |
echo -e "------------------>" | |
echo -e ${Green}"Initialisation..." | |
# Init DateStamp + Directory check. | |
today=$(date '+%Y.%m.%d-%H.%M') | |
cd $BASE | |
echo -e ${Cyan}" - Date : $today" | |
echo " - Dossier : $BASE/backups/data" | |
echo -e ${Green}"Sauvegarde des données..." | |
echo -e ${Cyan}"=> Sortie :"${White} | |
tar -czf $BASE/backups/data/"$ARCHIVE"-"$today"-"$data_file".tar.gz $WWWROOT --exclude="$WWWROOT/.git" -C $BASE/ | |
echo -e ${Green}"Verification..."$NC | |
ls -la $BASE/backups/data/ | grep "$ARCHIVE"-"$today"-"$data_file".tar.gz | |
echo -e ${Blue}"---------------------- [${Green}Données Sauvegardé !${Blue}]$NC" | |
clean_exit | |
} | |
GIT() { # -g | Git Actions | |
echo -e "------------------>" | |
cd $BASE/$WWWROOT | |
echo -e $NC | |
PS3=" - Selectionner l'action git a effectuer : " | |
select ACTION in "Status" "Ajouter tout" "Commit" "Tag" "Push" "Ajout-Commit-Push" exit | |
do | |
case $ACTION in | |
"Status") | |
echo -e ${Green} | |
echo "Status du depot GIT :" | |
echo -e ${NC} | |
git status | |
echo -e ${Blue} | |
echo "...................... [Ok]" | |
clean_exit | |
;; | |
"Ajouter tout") | |
echo -e ${Green} | |
echo "Ajout des fichiers et dossiers..." | |
git add -A | |
echo -e ${Blue} | |
echo "...................... [Ok]" | |
clean_exit | |
;; | |
"Commit") | |
echo -e ${Green} | |
echo "[5] Message de commit :" | |
echo " - detailler les modifications aporter au site." | |
echo "" | |
echo " - Ouverture de vim pour le message du commit..." | |
echo -e ${Cyan} | |
git commit -n | |
echo -e ${Green} | |
echo "Mise a jour du CHANGELOG..." | |
git log > CHANGELOG | |
echo -e ${Blue} | |
echo "...................... [Ok]" | |
clean_exit | |
;; | |
"Tag") | |
echo -e ${Green} | |
echo "Tags de master : " | |
echo "----------------------------------------" | |
git tag | |
echo "----------------------------------------" | |
echo "" | |
echo -e ${White}"Saisir le tag : " ${BWhite} | |
read TAGS | |
echo "Tagging..." | |
echo -e ${Cyan} | |
git tag "$TAGS" | |
echo -e ${Green} | |
echo "Pushing tags vers origin..." | |
echo -e ${Cyan} | |
git push origin master --tags | |
echo -e ${Green} | |
echo "Ajout du tag dans la variable gsv_version..." | |
echo -e ${Cyan} | |
drush vset --exact gsv_version "$TAGS" | |
git tag > TAGS | |
echo -e ${Blue} | |
echo "...................... [Ok]" | |
clean_exit | |
;; | |
"Push") | |
echo -e ${Green} | |
echo "Pushing master sur origin..." | |
echo -e ${Cyan} | |
git push origin master | |
echo -e ${Blue} | |
echo "...................... [Ok]" | |
clean_exit | |
;; | |
"Ajout-Commit-Push") | |
echo -e ${Green} | |
echo "[1] Ajout des fichiers et dossiers..." | |
git add -A | |
echo -e ${Green} | |
echo "[2] Message de commit :" | |
echo " - detailler les modifications aporter au site." | |
echo "" | |
echo " - Ouverture de vim pour le message du commit..." | |
echo -e ${Cyan} | |
git commit -n | |
echo -e ${Green} | |
echo "[3] Mise a jour du CHANGELOG..." | |
git log > CHANGELOG | |
echo "[4] Pushing master sur origin..." | |
echo -e ${Cyan} | |
git push origin master | |
echo -e ${Blue} | |
echo "...................... [Ok]" | |
clean_exit | |
;; | |
"exit") | |
clean_exit | |
;; | |
esac | |
done | |
} | |
INFO() { # -i | Informations | |
echo -e "Cf ./README.md" | |
} | |
# Init | |
## Color | |
Red='\e[0;31m' # Red | |
Green='\e[0;32m' # Green | |
Blue='\e[0;34m' # Blue | |
Cyan='\e[0;36m' # Cyan | |
White='\e[0;37m' # White | |
BPurple='\e[1;35m' # Purple | |
BWhite='\e[1;37m' # White | |
NC="\e[m" # Color Reset | |
## Directory | |
#cd $BASE/$WWWROOT | |
PWD=$(pwd) | |
# boTux Motd | |
MOTD() { | |
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" | |
echo -e "[${BPurple}Wordpress$NC] ## Administration" | |
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" | |
echo -e "Dossier : ${Green}$PWD$NC" | |
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" | |
echo -e $NC | |
} | |
MOTD | |
# Parse command-line | |
while [[ -n $1 ]]; do | |
case $1 in | |
-h | --help) | |
help_message; clean_exit ;; | |
-a | --auto) | |
MOTD | |
echo -e ${Red}"Sauvegarde Automatique Mysql et Data Dumps"$NC | |
AUTO ;; | |
-s | --sql) | |
echo -e ${Red}"Sauvegarde MySQL"$NC; shift; sql_file="$1" | |
SQL ;; | |
-d | --data) | |
echo -e ${Red}"Sauvegarde Data"$NC; shift; data_file="$1" | |
DATA ;; | |
-g | --git) | |
echo -e ${Red}"Git Actions"$NC | |
GIT ;; | |
-i | --info) | |
echo -e ${Red}"Informations"$NC | |
INFO ;; | |
-* | --*)- | |
usage | |
error_exit "Unknown option $1" ;; | |
*) | |
echo "Argument $1 to process..." ;; | |
esac | |
shift | |
done | |
# Main logic | |
clean_exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment