#!/bin/bash

echo -e "\n************************************  `date`  ************************************\n"

echo "==BEGIN " `date`

pixMysqlDumpPath="/home/pix/dumpmysql"
pixDateDay=`date +%A`
pixMysqlDumpExt="/mnt/pwstor01/dumpmysql"

cd /var/www/pix/current

#tools/file_processor/bin/file_processor stop
#tools/media_processor/bin/media_processor stop

echo "--DEBUT mysqldump comptes utilisateurs " `date`
mysqldump -peisha5eo pix_production authorizations search_provider_group_names search_provider_groups settings title_provider_group_names title_provider_groups titles users > $pixMysqlDumpPath/$pixDateDay.backup_users.sql
echo "--FIN mysqldump comptes utilisateurs " `date`
echo
echo "--DEBUT mysqldump database pix_production " `date`
mysqldump --single-transaction -peisha5eo pix_production > $pixMysqlDumpPath/$pixDateDay.pix_production.sql
echo "--FIN mysqldump database pix_production " `date`
echo

echo "--DEBUT Provider.delete_toomany_images " `date`
script/rails runner -e production 'Provider.delete_toomany_images'
echo "--FIN Provider.delete_toomany_images " `date`
echo

case $1 in
#1
image_core )

echo "--DEBUT index image_core " `date`
/usr/bin/indexer --config /var/www/pix/current/config/production.sphinx.conf --rotate image_core
echo "--FIN index image_core " `date`
;;

##2
all_core )

echo "--DEBUT index image_core " `date`
/usr/bin/indexer --config /var/www/pix/current/config/production.sphinx.conf --rotate image_core
echo "--FIN index image_core " `date`

echo
echo "--DEBUT index statistic_core " `date`
/usr/bin/indexer --config /var/www/pix/current/config/production.sphinx.conf --rotate statistic_core
echo "--FIN index statistic_core " `date`
;;

###*
*)    echo Option incorrecte
esac

echo "--DEBUT Sessions table purge " `date`
mysql -e 'delete from sessions where updated_at < DATE_SUB(NOW(), INTERVAL 1 WEEK);'
echo "--FIN Sessions table purge " `date`

#tools/media_processor/bin/media_processor start
#tools/file_processor/bin/file_processor start

mv -vf $pixMysqlDumpPath/* $pixMysqlDumpExt/

echo "==DONE " `date`
