917 |
franck |
1 |
#!/bin/bash
|
958 |
franck |
2 |
# $Id: alcasar-archive.sh 1358 2014-05-23 12:26:25Z richard $
|
917 |
franck |
3 |
|
|
|
4 |
# alcasar-archive.sh
|
|
|
5 |
# by Franck BOUIJOUX and REXY
|
|
|
6 |
# This script is distributed under the Gnu General Public License (GPL)
|
|
|
7 |
|
|
|
8 |
# Script permettant
|
1157 |
stephane |
9 |
# - d'exporter dans un seul fichier les logs de traçabilités et la base des usagers (à des fins d'archivages).
|
917 |
franck |
10 |
# - Une fonction de chiffrement des logs a été implémentée dans ce script. Lisez la documentation d'exploitation pour l'activer.
|
|
|
11 |
# - nettoyage des archives supérieures à 1 an (365 jours)
|
|
|
12 |
|
|
|
13 |
# This script allows
|
1157 |
stephane |
14 |
# - export in one file the log files and user's base (in order to archive them).
|
1300 |
richard |
15 |
# - a cypher fonction allows to protect these files. Read the exploitation documentation to enable it.
|
917 |
franck |
16 |
# - delete backup files older than one year (365 days)
|
|
|
17 |
|
|
|
18 |
DIR_SAVE="/var/Save" # répertoire accessible par webs
|
|
|
19 |
DIR_LOG="/var/log" # répertoire local des log
|
|
|
20 |
|
|
|
21 |
#DIR_SERVICE="squid httpd firewall" # répertoires contenant des logs utiles à exporter
|
1300 |
richard |
22 |
DIR_BASE="$DIR_SAVE/base" # répertoire de sauvegarde de la base de données usagers
|
1308 |
richard |
23 |
DIR_ARCHIVE="$DIR_SAVE/archive" # répertoire de sauvegarde des archives de log
|
917 |
franck |
24 |
NOW="$(date +%G%m%d-%Hh%M)" # date et heure du moment
|
|
|
25 |
DIR_TMP="/tmp/archive-$NOW" # Répertoire temporaire d'export
|
|
|
26 |
FILE="archive-$NOW.tar.gz" # Nom du fichier de l'archive
|
|
|
27 |
EXPIRE_DAY=365 # Nbre de jour avant suppression des fichiers journaux
|
1263 |
richard |
28 |
CRYPT="0" # chiffrement des logs ( 0=non / 1=oui) --> Si oui alors la signature est automatiquement activée
|
|
|
29 |
# log files encryption ( 0=no / 1=yes) --> if yes, the signature is automaticly enabled
|
917 |
franck |
30 |
SIGN="0" # Signature/empreinte des logs ( 0=non / 1=oui ) ATTENTION : nécessite la clé privée !!!
|
1263 |
richard |
31 |
# Signature of log files ( 0=no / 1=yes ) ATTENTION : need the private key !!!
|
1157 |
stephane |
32 |
GPG_USER="" # utilisateur autorisé à déchiffrer les logs. Sa clé publique doit être connu dans le portefeuille gnupg de root (/root/.gnupg)
|
1263 |
richard |
33 |
# user allowed to decrypt the log files. Its public key must be known in the root keyring (/root/.gnupg)
|
917 |
franck |
34 |
|
1308 |
richard |
35 |
usage="Usage: alcasar-archive.sh {--live or -l} | {--now or -n} | {--clean or -c}"
|
917 |
franck |
36 |
|
|
|
37 |
nb_args=$#
|
|
|
38 |
args=$1
|
|
|
39 |
if [ $nb_args -eq 0 ]
|
|
|
40 |
then
|
|
|
41 |
nb_args=1
|
|
|
42 |
args="-h"
|
|
|
43 |
fi
|
|
|
44 |
|
|
|
45 |
|
|
|
46 |
function cleanup() {
|
|
|
47 |
# Nettoyage des fichiers archives
|
|
|
48 |
cd $DIR_SAVE
|
|
|
49 |
find . \( -mtime +$EXPIRE_DAY \) -a \( -name '*.gz' -o -name '*.sql' -o -name '' -o -name 'gpg' \) -exec rm -f {} \;
|
|
|
50 |
} # end function cleanup
|
|
|
51 |
|
|
|
52 |
|
|
|
53 |
function crypt() {
|
|
|
54 |
# Chiffrement des logs dans /var/Save/
|
|
|
55 |
find . \( -mtime -7 -o -ctime 0 \) -a \( -name '*log-*.gz' \) -exec gpg --output $DIR_ARCHIVE/$file/{}.gpg --encrypt --recipient $GPG_USER {} \;
|
|
|
56 |
} # end function crypt
|
|
|
57 |
|
|
|
58 |
function archive() {
|
|
|
59 |
mkdir -p $DIR_ARCHIVE
|
1247 |
crox53 |
60 |
mkdir -p $DIR_TMP
|
1358 |
richard |
61 |
nb_files=`ls $DIR_LOG/firewall/traceability.log*.gz 2>/dev/null | wc -w`
|
1266 |
richard |
62 |
if [ $nb_files -ne 0 ]; then
|
1358 |
richard |
63 |
mv $(echo $(ls -rt $DIR_LOG/firewall/traceability.log*.gz | tail -n 1 -)) $DIR_TMP/traceability-HTTP-$NOW.gz
|
1266 |
richard |
64 |
fi
|
|
|
65 |
nb_files=`ls $DIR_BASE/radius-*.sql 2>/dev/null | wc -w`
|
|
|
66 |
if [ $nb_files -ne 0 ]; then
|
|
|
67 |
mv $(echo $(ls -rt $DIR_BASE/radius-*.sql | tail -n 1 -)) $DIR_TMP/
|
|
|
68 |
fi
|
1247 |
crox53 |
69 |
cd /var/log/nfsen/profiles-data/live/ipt_netflow
|
1266 |
richard |
70 |
nb_files=`find . -mtime -7 -name 'nfcapd.[0-9]*' | wc -l`
|
|
|
71 |
if [ $nb_files -ne 0 ]; then
|
1358 |
richard |
72 |
find . -mtime -7 -name 'nfcapd.[0-9]*' | xargs tar -cf $DIR_TMP/traceability-ALL-$NOW.tar;
|
1266 |
richard |
73 |
fi
|
1247 |
crox53 |
74 |
cd /tmp/
|
1266 |
richard |
75 |
nb_files=`ls archive-$NOW/* 2>/dev/null | wc -w`
|
|
|
76 |
if [ $nb_files -ne 0 ]; then
|
|
|
77 |
tar cvzf /tmp/$FILE archive-$NOW/*
|
|
|
78 |
else echo "no file to archive"
|
|
|
79 |
fi
|
917 |
franck |
80 |
} # end archive
|
|
|
81 |
|
|
|
82 |
# Core script
|
|
|
83 |
case $args in
|
|
|
84 |
-\? | -h* | --h*)
|
|
|
85 |
echo "$usage"
|
|
|
86 |
exit 0
|
|
|
87 |
;;
|
|
|
88 |
--clean | -c)
|
|
|
89 |
cleanup
|
|
|
90 |
;;
|
|
|
91 |
--now | -n)
|
|
|
92 |
cleanup
|
|
|
93 |
archive
|
1266 |
richard |
94 |
if [ -e /tmp/$FILE ]; then
|
|
|
95 |
if [ $CRYPT -eq "1" ]; then
|
|
|
96 |
{
|
|
|
97 |
# 1 ) chiffrement/signature =1 ==> gpg --encrypt avec test de la clé présente
|
|
|
98 |
gpg --output $DIR_ARCHIVE/$FILE-crypt.gpg --armor --encrypt --recipient $GPG_USER /tmp/$FILE
|
|
|
99 |
}
|
|
|
100 |
elif [ $SIGN -eq "1" ]; then
|
|
|
101 |
{
|
|
|
102 |
# 2) signature = 1 Chiffrement = 0 --> gpg --encrypt idem test de la clé présente
|
|
|
103 |
gpg --output $DIR_ARCHIVE/$FILE-sign.gpg --sign --recipient $GPG_USER /tmp/$FILE
|
|
|
104 |
gpg --output $DIR_ARCHIVE/$FILE-sign.gpg --sign --recipient $GPG_USER --detach-sign /tmp/$FILE
|
|
|
105 |
}
|
|
|
106 |
else
|
|
|
107 |
{
|
|
|
108 |
# 3) chiffrement/signature = 0 --> cp simple avec suppression des droits d'écriture
|
|
|
109 |
cp /tmp/$FILE $DIR_ARCHIVE/.
|
|
|
110 |
}
|
|
|
111 |
fi
|
917 |
franck |
112 |
fi
|
|
|
113 |
rm -rf /tmp/archive-*
|
1157 |
stephane |
114 |
chown root:apache $DIR_ARCHIVE/*
|
917 |
franck |
115 |
;;
|
1247 |
crox53 |
116 |
--live | -l)
|
1308 |
richard |
117 |
mkdir -p $DIR_ARCHIVE
|
1247 |
crox53 |
118 |
mkdir -p /tmp/live
|
|
|
119 |
gap=$(($(date +%d)-1))
|
|
|
120 |
cd /var/log/nfsen/profiles-data/live/ipt_netflow
|
1358 |
richard |
121 |
find . -mtime -$gap -name 'nfcapd.[0-9]*' | xargs tar -cf /tmp/live/traceability-ALL-$NOW.tar;
|
1303 |
richard |
122 |
/usr/local/sbin/alcasar-mysql.sh --dump
|
|
|
123 |
mv $(echo $(ls -rt $DIR_BASE/radius-*.sql | tail -n 1 -)) /tmp/live/
|
1358 |
richard |
124 |
cp /var/log/firewall/traceability.log /tmp/live/traceability-HTTP-$NOW.log
|
|
|
125 |
tar -czf $DIR_ARCHIVE/traceability-$NOW.tar.gz /tmp/live/*
|
1247 |
crox53 |
126 |
rm -rf /tmp/live
|
|
|
127 |
;;
|
917 |
franck |
128 |
*)
|
|
|
129 |
echo "Unknown argument :$1";
|
|
|
130 |
echo "$usage"
|
|
|
131 |
exit 1
|
|
|
132 |
;;
|
|
|
133 |
esac
|
|
|
134 |
exit 0
|