#!/bin/bash

### <==========================================================================================> ###
### backup-ftp = Versão 2019 $ Qua Oct 28 13:08:14 BRST =============> ###

### <==========================================================================================> ###

Domain=`cat /root/newtonpmta-scripts/domain.info`
MonitoringEmail=`cat /root/newtonpmta-scripts/monitoringemail.info`
Data=`date`
DataPers=`date +%Y_%m_%d`
BackupFtpLog="/tmp/backup-ftp.log"
Arquivo=`ls /root/newtonpmta-scripts/backup-local | tail -1`
HostFtp=`cat /root/newtonpmta-scripts/hostftp.info`
UserFtp=`cat /root/newtonpmta-scripts/userftp.info`
UserPassFtp=`cat /root/newtonpmta-scripts/userpassftp.info`

echo "
### <=========================================================================================> ###
### ---> Backup domain $Domain in $Data!
### <=========================================================================================> ###
" > $BackupFtpLog 

cd /root/newtonpmta-scripts/backup-local/

echo " Performing backup ...
Always check in $HostFtp the space available and the consistency of backups. 
### <=========================================================================================> ###
" >> $BackupFtpLog 

/usr/bin/wput -B $Arquivo ftp://$UserFtp:$UserPassFtp@$HostFtp/backup-$Domain/ >> $BackupFtpLog

echo "
Backup the server. $Domain made in $DataPers! 
### <=========================================================================================> ###

###---> IMPORTANT INFORMATION: 
###---> the server. $Domain keeps the last 7 daily backups! 
###---> Target server does not erase backups for safety!
###---> Always check the space available and the consistency of backups.

### <=========================================================================================> ###
" >> $BackupFtpLog 

cat $BackupFtpLog >> /var/log/pmta/log
cat $BackupFtpLog >> /var/log/messages

Versao=`cut -d" " -f3 /etc/redhat-release | cut -d"." -f1`
	if [ $Versao = 6 ]
	then
		cat $BackupFtpLog | mail -r manutencao@server.$Domain -s "Backup the server. $Domain in $Data" $MonitoringEmail
	else
		cat $BackupFtpLog | mail -s "Backup the server. $Domain in $Data" $MonitoringEmail -- -f manutencao@server.$Domain
	fi
