<commit_before>#!/bin/bash
# MySQL backup script
# Version 0.1 - https://github.com/gregorystorme/autoxtrabackup
# Copyright (c) 2014 <NAME>


if [ -f /etc/default/autoxtrabackup ] ; then
        . /etc/default/autoxtrabackup
else

backupDir=/var/backups/mysql
hoursBeforeFull=12
mysqlUser=dbuser
mysqlPwd=password
compression=yes
keepDays=7
fi

#####
# No editing should be required below this line
#####

dateNow=`date +%Y-%m-%d_%H-%M-%S`
dateNowUnix=`date +%s`
backupLog=/tmp/backuplog
delDay=`date -d "-$keepDays days" +%Y-%m-%d`

if [ $(($keepDays * 24)) -le $hoursBeforeFull ]; then
        echo "ERROR: You have set hoursBeforeFull to $hoursBeforeFull and keepDays to $keepDays, this will delete all your backups... Change this"
        exit 1
else
        :
fi

if [[ $compression == yes ]]; then
 compress="--compress"
else
 compress=
fi

if [ -f "$backupDir"/latest_full ]; then
 echo "Latest full backup information found... continuing"
 lastFull=`cat "$backupDir"/latest_full`
else
 echo "Latest full backup information not found... taking a first full backup now"
 echo $dateNowUnix > "$backupDir"/latest_full
 /usr/bin/innobackupex --user=$mysqlUser --password=$mysqlPwd --no-timestamp $compress --rsync "$backupDir"/"$dateNow"_full > $backupLog 2>&1
  if grep -q "completed OK" $backupLog; then
   echo "Full backup completed OK!"
   exit 0
  else
   echo "Full backup FAILED"
   exit 1
  fi
fi

difference=$((($dateNowUnix - $lastFull) / 60 / 60))

if [ $difference -lt $hoursBeforeFull ]; then
 echo "It's been $difference hours since last full, doing an incremental backup"
 lastFullDir=`date -d@"$lastFull" '+%Y-%m-%d_%H-%M-%S'`
 /usr/bin/innobackupex --user=$mysqlUser --password=$<PASSWORD> --no-timestamp $compress --rsync --incremental --incremental-basedir="$backupDir"/"$lastFullDir"_full "$backupDir"/"$dateNow"_incr > $backupLog 2>&1
else
 echo "It's been $difference hours since last full backup, time for a new full backup"
 echo $dateNowUnix > "$backupDir"/latest_full
 /usr/bin/innobackupex --user=$mysqlUser --password=$mysqlPwd --no-timestamp $compress --rsync "$backupDir"/"$dateNow"_full > $backupLog 2>&1
fi

if grep -q "completed OK" $backupLog; then
 if grep -q "incremental" $backupLog; then
         echo "Incremental backup completed OK!"
 else
  echo "Full backup completed OK!"
 fi
else
        echo "Backup FAILED"
        exit 1
fi

# Delete backups older than retention date
rm -rf $backupDir/$delDay*

# Delete incremental backups with full backup base directory that was deleted
for i in `find "$backupDir"/*incr -type f -iname xtrabackup_info |  xargs grep $delDay | awk '{print $10}' | cut -d '=' -f2`; do rm -rf $i; done

exit 0
<commit_msg>Add optional e-mail notifications; cleanup code<commit_after>#!/bin/bash
# MySQL backup script
# https://github.com/gregorystorme/autoxtrabackup
# Copyright (c) 2014 <NAME>
# Version: 0.2

if [ -f /etc/default/autoxtrabackup ] ; then
        . /etc/default/autoxtrabackup
else

backupDir=/var/backups/mysql
hoursBeforeFull=48
mysqlUser=dbuser
mysqlPwd=password
compression=true
keepDays=7
sendEmail=never
emailAddress=
fi

#####
# No editing should be required below this line
#####

dateNow=`date +%Y-%m-%d_%H-%M-%S`
dateNowUnix=`date +%s`
backupLog=/tmp/backuplog
delDay=`date -d "-$keepDays days" +%Y-%m-%d`

# Check if you set a correct retention
if [ $(($keepDays * 24)) -le $hoursBeforeFull ]; then
        echo "ERROR: You have set hoursBeforeFull to $hoursBeforeFull and keepDays to $keepDays, this will delete all your backups... Change this"
        exit 1
fi

# If you enabled sendEmail, check if you also set a recipient
if [[ -z $emailAddress ]] && [[ $sendEmail == onerror ]]; then
 echo "Error, you have enabled sendEmail but you have not configured any recipient"
 exit 1
elif [[ -z $emailAddress ]] && [[ $sendEmail == always ]]; then
 echo "Error, you have enabled sendEmail but you have not configured any recipient"
 exit 1
fi

# If compression is enabled, pass it on to the backup command
if [[ $compression == true ]]; then
 compress="--compress"
else
 compress=
fi

# Check for an existing full backup
if [ -f "$backupDir"/latest_full ]; then
 #echo "Latest full backup information found... continuing"
 lastFull=`cat "$backupDir"/latest_full`
else
 #echo "Latest full backup information not found... taking a first full backup now"
 echo $dateNowUnix > "$backupDir"/latest_full
 /usr/bin/innobackupex --user=$mysqlUser --password=$mysql<PASSWORD> --no-timestamp $compress --rsync "$backupDir"/"$dateNow"_full > $backupLog 2>&1
fi

# Calculate the time since the last full backup
difference=$((($dateNowUnix - $lastFull) / 60 / 60))

# Check if we must take a full or incremental backup
if [ $difference -lt $hoursBeforeFull ]; then
 #echo "It's been $difference hours since last full, doing an incremental backup"
 lastFullDir=`date -d@"$lastFull" '+%Y-%m-%d_%H-%M-%S'`
 /usr/bin/innobackupex --user=$mysqlUser --password=$mysql<PASSWORD> --no-timestamp $compress --rsync --incremental --incremental-basedir="$backupDir"/"$lastFullDir"_full "$backupDir"/"$dateNow"_incr > $backupLog 2>&1
else
 #echo "It's been $difference hours since last full backup, time for a new full backup"
 echo $dateNowUnix > "$backupDir"/latest_full
 /usr/bin/innobackupex --user=$mysqlUser --password=$<PASSWORD> --no-timestamp $compress --rsync "$backupDir"/"$dateNow"_full > $backupLog 2>&1
fi

# Check if the backup succeeded or failed, and e-mail the logfile, if enabled
if grep -q "completed OK" $backupLog; then
 #echo "Backup completed OK"
 if [[ $sendEmail == always ]]; then
  cat $backupLog | mail -s "AutoXtraBackup log" $emailAddress
 fi
else
        #echo "Backup FAILED"
        if [[ $sendEmail == always ]] || [[ $sendEmail == onerror ]]; then
                cat $backupLog | mail -s "AutoXtraBackup log" $emailAddress
 fi
        exit 1
fi

# Delete backups older than retention date
rm -rf $backupDir/$delDay*

# Delete incremental backups with full backup base directory that was deleted
for i in `find "$backupDir"/*incr -type f -iname xtrabackup_info |  xargs grep $delDay | awk '{print $10}' | cut -d '=' -f2`; do rm -rf $i; done

exit 0

{'fraction_non_alphanumeric': 0.13452477660438666, 'fraction_numerical': 0.00942323314378554, 'mean_word_length': 4.083402146985962, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1255614', 'n_tokens_mistral': 2104, 'n_tokens_neox': 1953, 'n_words': 671}