Archives par mot-clé : Backup

Script de backup

#!/bin/sh
## Autor : Infragate
## PS: color only for bash & Modify ssh/rsync cmd if u plan to use it with rsa-keys
## Binary needed : bc / mysqldump / tar / [cifs-utils] | [sshpass] | [rsync]
## apt-get install bc postfix mutt bc pigz
## apt-get install rsync sshpass cifs-utils

###########################
#### SCRIPT VARIABLES #####
###########################

### Runtime computing ###
 basetime=$(date +%s%N)

### Colors for local logfile ###
 red='\033[0;31m'
 RED='\033[1;31m'
 green='\033[0;32m'
 GREEN='\033[1;32m'
 blue='\033[0;34m'
 BLUE='\033[1;34m'
 purple='\033[0;35m'
 PURPLE='\033[1;35m'
 cyan='\033[1;36m'
 def='\033[0m'
###

### Timestamps ###
 date=`date +%Y%m%d`
 day=`date +%u`
 jour=`date +%A`
 week=`date +%Y%U`
###

###########################
##### USER VARIABLES ######
###########################

### Backup parameters ####
 folders="/etc/nginx /etc/php-fpm /home/www/sample1"
 exclude_files_pattern='*.mp3'
 bases="mysql cattle wp_client1"
 dest=/backup/`hostname`
 daily_backup=$dest/backup_$date'_'$jour
 retention_days=7
 retention_weeks_full=4
###

### OPTS ###
 activate_bckp_db=1
 activate_bckp_files=1
 activate_mail_alert=1
 activate_cifs_backup=0
 activate_sshrsync_backup=0
 activate_usb_backup=0
###

### Database credentials ###
 user=root
 pass="insert pass here"
 host=localhost
###

### CIFS Backup method parameters###
 user_share=username
 pass_share="pass_here"
 share_server=192.168.168.168
 share_folder=nom_du_partage
 cifs_mount_point=/backup
###

### SSH / Rsync Backup Method parameters###
 user_ssh=username
 pass_ssh='pass ssh here'
 remote_host=10.10.10.10
 remote_target=/home/$user_ssh/backup
###

### USB HD Backup Method parameters###
 usb_device=/dev/sdx
 usb_mount_point=/backup
###

### MAIL ###
recipient="yourmail@domain.com"
attachement=~/mail.txt
###

### LOG ###
 logfile=~/log_backup.txt
 temp=~/temp.txt
###

###########################
######## FUNCTIONS ########
###########################

log_date () {
 echo $(date +%d/%m/%Y-%H:%M:%S)
}

#################

clean_attachement () {
 sed -r "s/\x1B\[([0-9];)?([0-9]{1,2}(;[0-9]{1,2})?)?[m]//g" $temp > $attachement
}

##################

blank_log () {
 echo " "  >> $temp ;
}

##################

backup_type () {
 if [ $day -eq 7 ] || [ ! -d $dest/snap_files ]
    then
       backup_type='_FULL';
       bkp_kind="Full";
     else
       backup_type='_INC_part_'$day;
       bkp_kind="Incremental";
 fi
}

##################

backup_file () {
 echo "${GREEN}$(log_date) $bkp_kind Folders Backup : STARTED${def}"  >> $temp ;

 for folder in $folders
    do
        if [ ! -d $folder ]
            then
                echo "$(log_date) Folder ${blue}${folder} ${def}:${RED} Don't exist${def}" >> $temp;
            else
                bckp_list=$dest/snap_files/'files_'$(basename $folder).snap
                if [ $day -eq 7 ] ; then rm -rf $bckp_list ; fi
                tar -I pigz -cf $daily_backup/'files_'$date'_'$(basename $folder)$backup_type.tgz  -g $bckp_list $folder --exclude=$exclude_files_pattern
                [ "$?" = 0 ] &&  echo "$(log_date) Folder${blue} ${folder} ${def}Backup : ${green}DONE${def}" >> $temp ;
               [ "$?" != 0 ] &&  echo "$(log_date) Folder${blue} ${folder} ${def}Backup : ${RED}FAILED${def}" >> $temp ;
        fi
    done

 echo "${GREEN}$(log_date) $bkp_kind Folders Backup : ENDED ${def}"  >> $temp ;
 blank_log
}

##################

backup_db () {
 echo "${GREEN}$(log_date) Databases Backup : STARTED ${def}" >> $temp ;
 for base in $bases
    do
      mysqldump -l --user $user --password=$pass  --host $host --events --opt --databases $base  > /tmp/$base.sql
      [ "$?" = 0 ] &&  echo "$(log_date) Database ${purple}${base} ${def}Backup : ${green}DONE${def}" >> $temp ;
      [ "$?" != 0 ] &&  echo "$(log_date) Database ${purple}${base} ${def}Backup : ${RED}FAILED${def}" >> $temp ;
      tar -I pigz -cf $daily_backup/'database_'$date'_'$base.tgz /tmp/$base.sql
      rm -rf /tmp/$base.sql
    done
 mysqladmin -p$pass flush-tables

 echo "${GREEN}$(log_date) Databases Backup : ENDED ${def}" >> $temp ;
 blank_log
}

#################

retention () {
 if [ $day -eq 7 ]
    then
        weekly=$dest/arch_$week
        if [ ! -d $weekly ] ; then mkdir -p $weekly; fi
        cp -r -p $dest/backup_$date'_'$jour/*.tgz $weekly
 fi

 date=`date -d "$1 days ago" +%Y%m%d`
 jour=`date +%A`

 if [ -d $dest/backup_$date'_'$jour ]
        then
            blank_log
            echo "$(log_date) Folder${blue} $dest/backup_$date"_"$jour ${def} : ${green}Deleted : Obsolete${def} => Daily Retention policy" >> $temp ;
            rm -rf $dest/backup_$date'_'$jour
 fi

 date=`date -d "$2 week ago" +%Y%U`
 
 if [ -d $dest/arch_$date ]
              then
              echo "$(log_date) Folder${blue} $dest/arch_$date  ${def} : ${green}Deleted : Obsolete${def} => Full Archive Retention policy" >> $temp ;
              blank_log
              rm -rf $dest/arch_$date
 fi
}

#################

logging_trace () {
 touch $logfile
 size_log=`stat -c %s $logfile`
 size_max=1048576
 if [ $size_log -gt $size_max ]
   then
      tar czf $logfile_$date.tgz $logfile
      rm -rf  $logfile
 fi
 touch $logfile
}

##############################
######## SCRIPT START ########
##############################

### Root user testing ###

idnbr=`id -u`
if [ $idnbr != 0 ]; then echo "Must be r00t Assh0le!" >> $logfile && exit ; fi

### INIT LOG ###

logging_trace

### HEADER ###

blank_log
echo "\t\t\t ${cyan} Server : `hostname` ${def}"  >> $temp ;
echo "${BLUE}===================================================================${def}" >> $temp ;
echo "${BLUE}$(log_date) Backup Script Launched ${def}" >> $temp ;
echo "${BLUE}===================================================================${def}" >> $temp ;

#### CIFS MOUNT ######

if [ $activate_cifs_backup = 1 ]
    then
        echo "${BLUE}CIFS BACKUP Method Selected, Umounting after backup" >> $temp ;
        if [ ! -d $cifs_mount_point ]; then mkdir -p $cifs_mount_point ; fi
        mount -t cifs \\\\$share_server\\$share_folder $cifs_mount_point  -o username=$user_share,password=$pass_share
        [ "$?" != 0 ] &&  echo "${red}$(log_date)CIFS MOUNT : FAILED : BACKUP ABORTED${def} " >> $temp && exit ;
fi

##### USB MOUNT #####

if [ $activate_usb_backup = 1 ]
    then
        echo "${BLUE}USB BACKUP Method Selected, Umounting after backup" >> $temp ;
        if [ ! -d $usb_mount_point ]; then  mkdir -p $usb_mount_point; fi
        mount $usb_device $usb_mount_point
        [ "$?" != 0 ] &&  echo "${red}$(log_date)USB MOUNT : FAILED : BACKUP ABORTED${def} " >> $temp && exit ;
fi

###### RSYNC LOG #####

if [ $activate_sshrsync_backup = 1 ]; then echo "${BLUE}SSH/RSYNC BACKUP Method Selected, Local files will be keept after backup" >> $temp ; fi
blank_log

#### FOLDERS CREATION ####

if [ ! -d $daily_backup ] ; then mkdir -p $daily_backup; fi
backup_type
if [ ! -d $dest/snap_files ]; then mkdir -p $dest/snap_files ; fi

##
###### BACKUP FOLDERS ######
##

if [ $activate_bckp_files = 1 ]
    then
        backup_file
    else
        echo  "${RED}$(log_date) Files backup : DISABLED ${def}" >> $temp ;
fi

##
###### BACKUP DATABASES ######
##

if [ $activate_bckp_db = 1 ]
    then
       backup_db
    else
       echo "${RED}$(log_date) Database backup : DISABLED${def}" >> $temp ;
fi

##
###### RETENTION POLICY ######
##

retention $retention_days $retention_weeks_full

#######################
#####Backup Type#######
#######################

#### RSYNC w SSH ####

if [ $activate_sshrsync_backup = 1 ]
    then
        echo "$(log_date)${green} SSH/RSYNC Copy : STARTED${def}" >> $temp ;
        blank_log
        /usr/bin/rsync -avz --rsh="sshpass -p $pass_ssh  ssh -oUserKnownHostsFile=/dev/null  -oStrictHostKeyChecking=no" $dest/* $user_ssh@$remote_host:$remote_target 1>> $temp ;
        [ "$?" = 0 ] &&  echo "$(log_date)${green} SSH/RSYNC Copy : DONE${def}" >> $temp ;
        [ "$?" != 0 ] &&  echo "$(log_date)${red} SSH/RSYNC Copy : FAILED${def}" >> $temp ;
        blank_log
fi

#### UMOUNT CIFS ####

if [ $activate_cifs_backup = 1 ]
    then
        umount $cifs_mount_point
        [ "$?" = 0 ] &&  echo "$(log_date)${green} CIFS Share sucessfully unmounted${def}" >> $temp ;
        [ "$?" != 0 ] &&  echo "$(log_date)${red} CIFS Share unmounting : FAILED${def}" >> $temp ;
fi

#### UMOUNT USB ####

if [ $activate_usb_backup = 1 ]
    then
        umount $usb_mount_point
        [ "$?" = 0 ] &&  echo "$(log_date)${green} USB HD sucessfully unmounted${def}" >> $temp ;
        [ "$?" != 0 ] &&  echo "$(log_date)${red} USB HD  unmounting : FAILED${def}" >> $temp ;
fi

###### FOOTER #######

echo "${BLUE}===================================================================${def}" >> $temp ;
echo "${BLUE}$(log_date) Backup Script Ended  !${def}" >> $temp ;
echo "${cyan} \t \t \t runtime: $(echo "scale=3;($(date +%s%N) - ${basetime})/(1*10^09)" | bc) seconds"${def} >> $temp ;
echo "${BLUE}===================================================================${def}" >> $temp ;
blank_log

#### BACKUP CURRENT LOG ####

cat $temp >> $logfile

#### MAILING ####

if [ $activate_mail_alert = 1 ]
 then
    echo "set record=" "" > ~/.muttrc
    clean_attachement
    cat $attachement | mutt -s " $bkp_kind Backup Report for `hostname` $(log_date)" $recipient
    [ "$?" != 0 ] && exit ;
    rm -rf $attachement ;
fi

rm -rf $temp ;