HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //opt/scripts/db-backup.sh
#!/bin/bash

# Define backup variables
DB_USER="db-backups"
DB_PASSWORD="!...@(~Aws/Backups/Rockzz~)@...!"
DB_HOST="localhost"
date_format=`date +%d_%B_%Y_at_%H_%M_%S -d '-5 hours'`
db_dir="/tmp/databases/$date_format"
sync_dir="/tmp/databases"
dest_backup_file="/tmp/databases-VariosDEV-$date_format.tgz"
log_file="/var/log/s3"
s3_bucket="s3://b001-si/db_backups/"
email_notification="helpdesk@aliansap.com.co"
email_copy_notification="andres.garcia@allup.com.co, francisco.restrepo@allup.com.co"

# Delete previous backups from machine
sudo rm -rf /tmp/databases*

# create log file
if [ ! -e $log_file ]; then
    touch $log_file
fi

# Create database backups dir
if [ ! -d $db_dir ]; then
    mkdir -p $db_dir
fi

# Get a list of all databases -- Except mysql and schema dbs

databases=`mysql -u $DB_USER -h $DB_HOST -p$DB_PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`

# Dump all databases

for db in $databases; do
    if [[ "$db" != "information_schema"  ]] && [[ "$db" != "performance_schema"  ]] && [[ "$db" != "mysql"  ]] && [[ "$db" != _*  ]] ; then
        echo "Dumping database: $db"
        mysqldump -u $DB_USER -h $DB_HOST -p$DB_PASSWORD --databases $db > $db_dir/$db-$date_format.sql
    fi
done


# Generate compressed file of backup dir

echo "Compressing databases before upload.."
tar -zcvf $dest_backup_file -C $db_dir . | tee -a $log_file

# Copy compressed file to s3

echo "Backing up databases to s3.."
aws s3 cp $dest_backup_file  ${s3_bucket} | tee -a $log_file


# Send email when successful - Optional
# Uncomment this section if you don't need email notification upon completion

echo "Backup successful"
rm -rf $db_dir
echo "" > /tmp/success
echo "DB backups to s3 successful" >> /tmp/success
echo "" >> /tmp/success
aws s3 ls ${s3_bucket} | tee -a  /tmp/success
# mail -s "Database backups to s3" -c $email_copy_notification  $email_notification < /tmp/success
# echo "Mail sended"
exit 0