<commit_before>#!/usr/bin/env zsh

# PostgreSQL backup script, by <NAME> <<EMAIL> at <EMAIL>>
# WWW: http://github.com/Chrononaut/pgsqlbackup/tree/master

PATH="/bin:/usr/bin:/usr/local/bin:/opt/local/bin"

# Load config
if [[ ! -f "`dirname $0`/config" ]]; then
  echo "Copy config.sample to config and edit the settings, then run this script again."
  exit
fi
source "`dirname $0`/config"

# Cd's into a given dir - creates if it doesn't already exist.
# Throws an exception if the directory is illegal.
create_and_cd() {
  if [ ! -d $1 ]; then mkdir -p $1 || exception "Could not create directory: $1"; fi
  cd $1 || exception "Could not cd to directory: $1"
}

# Echoes out an exception message, triggers the exception hooks and
# then exits the script with an error code.
exception() {
  echo $1
  if [ $EXCEPTIONHOOKS ]; then
    for hook in $EXCEPTIONHOOKS; do eval $hook \"$1\"; done
  fi
  exit 1
}

# Builds the connection string
pg_args=""
if [ $PGUSER ]; then pg_args+="-U $PGUSER"; fi
if [ $PGHOST ]; then pg_args+="-h $PGHOST"; fi

# Tests the given connection information to the database:
# We check if the database list is null - if so we throw an exception.
DBS=`psql ${=pg_args} -l` || exception "Could not connect to PostgreSQL database!"

if (( ! ${#DATABASES} )); then
  DATABASES=`echo $DBS|grep "^ \w"|awk -F '|' '{printf("%s\n", $1)}'|sed 's/ //g'`

  if (( ${#EXCLUDE} )); then
    for ex in $EXCLUDE; do
      DATABASES=`echo $DATABASES | grep -v $ex`
    done
  fi
fi

DATABASES=(${=DATABASES}) # Make an array of the databaselist
if (( ! ${#DATABASES} )); then exception "No suitable databases listed!"; fi

create_and_cd $BASEDIR

if [[ -d $BACKUPDIR ]]; then rm -rf $BACKUPDIR; fi
create_and_cd $BACKUPDIR

pg_args+=" -F t"

for db in $DATABASES; do
  eval "pg_dump $pg_args $db > $db.tar"
  $COMPRESSOR $db.tar
done

cd ..

# Cleanup old dirs
while [ `ls|wc -l` -gt $KEEP ]; do rm -rf `ls|head -1`; done

if [ $POSTHOOKS ]; then
  for hook in $POSTHOOKS; do eval $hook; done
fi

echo "Backup completed successfully"<commit_msg>Bugfix - use the -tr option for ls to sort by date<commit_after>#!/usr/bin/env zsh

# PostgreSQL backup script, by <NAME> <<EMAIL>>
# WWW: http://github.com/Chrononaut/pgsqlbackup/tree/master

PATH="/bin:/usr/bin:/usr/local/bin:/opt/local/bin"

# Load config
if [[ ! -f "`dirname $0`/config" ]]; then
  echo "Copy config.sample to config and edit the settings, then run this script again."
  exit
fi
source "`dirname $0`/config"

# Cd's into a given dir - creates if it doesn't already exist.
# Throws an exception if the directory is illegal.
cd_or_create_and_cd() {
  if [ ! -d $1 ]; then mkdir -p $1 || exception "Could not create directory: $1"; fi
  cd $1 || exception "Could not cd to directory: $1"
}

# Echoes out an exception message, triggers the exception hooks and
# then exits the script with an error code.
exception() {
  echo $1
  if [ $EXCEPTIONHOOKS ]; then
    for hook in $EXCEPTIONHOOKS; do eval $hook \"$1\"; done
  fi
  exit 1
}

# Builds the connection string
pg_args=""
if [ $PGUSER ]; then pg_args+="-U $PGUSER"; fi
if [ $PGHOST ]; then pg_args+="-h $PGHOST"; fi

# Tests the given connection information to the database:
# We check if the database list is null - if so we throw an exception.
DBS=`psql ${=pg_args} -l` || exception "Could not connect to PostgreSQL database!"

if (( ! ${#DATABASES} )); then
  DATABASES=`echo $DBS|grep "^ \w"|awk -F '|' '{printf("%s\n", $1)}'|sed 's/ //g'`

  if (( ${#EXCLUDE} )); then
    for ex in $EXCLUDE; do
      DATABASES=`echo $DATABASES | grep -v $ex`
    done
  fi
fi

DATABASES=(${=DATABASES}) # Make an array of the databaselist
if (( ! ${#DATABASES} )); then exception "No suitable databases listed!"; fi

cd_or_create_and_cd $BASEDIR
if [[ -d $BACKUPDIR ]]; then rm -rf $BACKUPDIR; fi
cd_or_create_and_cd $BACKUPDIR

pg_args+=" -F t"

for db in $DATABASES; do
  eval "pg_dump $pg_args $db > $db.tar"
  $COMPRESSOR $db.tar
done

cd ..

# Cleanup old dirs
while [ `ls|wc -l` -gt $KEEP ]; do rm -rf `ls -tr|head -1`; done

if [ $POSTHOOKS ]; then
  for hook in $POSTHOOKS; do eval $hook; done
fi

echo "Backup completed successfully"
{'fraction_non_alphanumeric': 0.14724957963007446, 'fraction_numerical': 0.005765073264472736, 'mean_word_length': 3.6734006734006734, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 12, 'https://': 0, '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': '1249654', 'n_tokens_mistral': 1574, 'n_tokens_neox': 1523, 'n_words': 546}