Skip to content

Instantly share code, notes, and snippets.

@atao
Created April 10, 2018 10:37
Show Gist options
  • Save atao/19e00010b921a624c5ccccf9dafc1ef3 to your computer and use it in GitHub Desktop.
Save atao/19e00010b921a624c5ccccf9dafc1ef3 to your computer and use it in GitHub Desktop.
Backup Ansible
#!/bin/bash
###########################################
# Backup Ansible
###########################################
# auteur : atao
VERSION="2018.04.10"
# Source : https://github.com/blogmotion/bm-RaspberryPi/blob/master/Backup%20scripts/backupToNas.sh
# Source : https://tecadmin.net/mysql-database-backup-to-ftp-server-shell-script/
# licence type : Creative Commons Attribution-NoDerivatives 4.0 (International)
# licence info : http://creativecommons.org/licenses/by-nd/4.0/
###########################################
### VARIABLES ###
LOCAL_BACKUP_DIR="~"
FTP_SERVER="FTP_SERVER"
FTP_USERNAME="USER_LOGIN"
FTP_PASSWORD="USER_PASSWORD"
FTP_UPLOAD_DIR="/"
### FIN DES VARIABLES ###
DATE=$(date +%Y-%m-%d_%Hh%M)
TAR=$(hostname -s)"-${DATE}.tar.gz"
ipserver=`ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/'`
locpath=`dirname "${BASH_SOURCE[0]}"` # chemin vers le script
scriptpath=`basename $0` # script.sh
bold=$(tput bold)
underline=$(tput sgr 0 1)
reset=$(tput sgr0)
rouge=$(tput setaf 1)
vert=$(tput setaf 2)
jaune=$(tput setaf 3)
bleu=$(tput setaf 4)
violet=$(tput setaf 5)
cyan=$(tput setaf 6)
gris=$(tput setaf 7)
# Fonctions
shw_norm () { echo -en "${bold}$(tput setaf 9)${@}${reset}"; }
shw_info () { echo -en "${bold}${cyan}${@}${reset}"; }
shw_OK () { echo -en "${bold}${vert}OK!${@}${reset}"; }
shw_warn () { echo -en "${bold}${violet}${@}${reset}"; }
shw_err () { echo -en "${bold}${rouge}${@}${reset}"; }
gris () { echo -en "${bold}${gris}${@}${reset}"; }
header () { echo -e "${bold}${jaune}$*${reset}"; }
headerU () { echo -e "${underline}${bold}${jaune}$*${reset}"; }
# debut du script
clear && echo -e "\n\n"
header "****************************************************************************"
header "*** ansible backup by @atao - Creative Commons BY-ND 4.0 (v${VERSION}) ***"
headerU "****************************************************************************"
shw_info "\n\n=== Demarrage sauvegarde d'Ansible le $(date +'%d/%m/%Y a %Hh%M'):\n\n"
# Vérification execution en tant que 'root'
shw_norm "\t::: execution en tant que root... "
if [[ $EUID -ne 0 ]]; then
sudo "$0" "$@" || (shw_err "Ce script doit être executé avec les droits 'root'. Arrêt du script.\n" ; exit 1)
else
shw_OK
fi
# creation archive
shw_norm "\n\t::: creation de l'archive ${TAR}... "
# Sauvegarde listing cron
for user in $(cut -f1 -d: /etc/passwd); do echo -e "\n\n==> $user:" ; crontab -u $user -l ; done > /tmp/crontab 2>&1
# une ligne par fichier ou dossier à sauvegarder, sans oublier les exclusions
tar zcf "$TAR" \
/tmp/crontab \
/etc/ansible \
--exclude "/home/debian/" \
> /dev/null 2>&1
tarsize=$(du -sh "$TAR")
if [[ $? -ne 0 ]]; then
shw_err "\n\t ERREUR: impossible de creer l'archive $TAR \n"
exit 1
fi
shw_OK
gris "\n\n\t=> Taille du backup: $tarsize\n\n"
############### UPLOAD to FTP Server ################
shw_norm "\n\t::: Upload on ${FTP_SERVER}...\n"
ftp -n $FTP_SERVER <<- EndFTP
user "$FTP_USERNAME" "$FTP_PASSWORD"
binary
cd $FTP_UPLOAD_DIR
lcd $LOCAL_BACKUP_DIR
put "$TAR"
bye
EndFTP
if [[ $? -ne 0 ]]; then
shw_err "\n\t ERREUR: Upload error on $FTP_SERVER"
exit 1
fi
shw_norm "\n\t::: Upload on ${FTP_SERVER}... "
shw_OK
shw_info "\n\n=== Fin du Backup le $(date +'%d/%m/%Y a %Hh%M')"
shw_info "\n=== ce script tourne depuis ${ipserver}:${locpath}/${scriptpath}\n\n"
# Nettoyage des fichiers temporaires
rm -f /tmp/crontab
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment