917 |
franck |
1 |
#!/bin/bash
|
958 |
franck |
2 |
# $Id: alcasar-archive.sh 2488 2018-02-25 14:53:54Z lucas.echard $
|
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 |
|
2454 |
tom.houday |
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 |
|
2454 |
tom.houday |
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 |
|
2488 |
lucas.echa |
21 |
#DIR_SERVICE="squid lighttpd 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
|
2454 |
tom.houday |
24 |
NOW="$(date +%G%m%d-%Hh%M)" # date et heure du moment
|
1596 |
richard |
25 |
DIR_TMP="/tmp/traceability-$NOW" # Répertoire temporaire d'export
|
|
|
26 |
FILE="traceability-$NOW.tar.gz" # Nom du fichier de l'archive
|
917 |
franck |
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
|
2454 |
tom.houday |
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 !!!
|
2454 |
tom.houday |
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() {
|
2454 |
tom.houday |
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 {} \;
|
917 |
franck |
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() {
|
2454 |
tom.houday |
59 |
mkdir -p $DIR_ARCHIVE
|
|
|
60 |
mkdir -p $DIR_TMP
|
|
|
61 |
nb_files=`ls $DIR_LOG/firewall/traceability.log*.gz 2>/dev/null | wc -w`
|
|
|
62 |
if [ $nb_files -ne 0 ]; then
|
|
|
63 |
mv $(echo $(ls -rt $DIR_LOG/firewall/traceability.log*.gz | tail -n 1 -)) $DIR_TMP/traceability-HTTP-$NOW.gz
|
|
|
64 |
fi
|
|
|
65 |
nb_files=`ls $DIR_BASE/alcasar-users-database-*.sql.gz 2>/dev/null | wc -w`
|
|
|
66 |
if [ $nb_files -ne 0 ]; then
|
|
|
67 |
mv $(echo $(ls -rt $DIR_BASE/alcasar-users-database-*.sql.gz | tail -n 1 -)) $DIR_TMP/
|
|
|
68 |
fi
|
|
|
69 |
cd /var/log/nfsen/profiles-data/live/alcasar_netflow
|
|
|
70 |
nb_files=`find . -mtime -7 -name 'nfcapd.[0-9]*' | wc -l`
|
|
|
71 |
if [ $nb_files -ne 0 ]; then
|
|
|
72 |
find . -mtime -7 -name 'nfcapd.[0-9]*' | xargs tar -cf $DIR_TMP/traceability-ALL-$NOW.tar;
|
|
|
73 |
fi
|
|
|
74 |
cd /tmp/
|
|
|
75 |
nb_files=`ls traceability-$NOW/* 2>/dev/null | wc -w`
|
|
|
76 |
if [ $nb_files -ne 0 ]; then
|
|
|
77 |
tar cvzf /tmp/$FILE traceability-$NOW/*
|
|
|
78 |
else echo "no file to archive"
|
|
|
79 |
fi
|
917 |
franck |
80 |
} # end archive
|
|
|
81 |
|
2454 |
tom.houday |
82 |
# Core script
|
917 |
franck |
83 |
case $args in
|
|
|
84 |
-\? | -h* | --h*)
|
|
|
85 |
echo "$usage"
|
|
|
86 |
exit 0
|
|
|
87 |
;;
|
|
|
88 |
--clean | -c)
|
1695 |
franck |
89 |
# Cleanup of files older than 365 days
|
917 |
franck |
90 |
cleanup
|
|
|
91 |
;;
|
|
|
92 |
--now | -n)
|
1695 |
franck |
93 |
|
|
|
94 |
# Cleanup of files older than 365 days
|
917 |
franck |
95 |
cleanup
|
1695 |
franck |
96 |
# make an archive
|
917 |
franck |
97 |
archive
|
1695 |
franck |
98 |
# Saving of the database
|
1827 |
raphael.pi |
99 |
/usr/local/bin/alcasar-mysql.sh --dump
|
1695 |
franck |
100 |
# Encryption of the archive
|
2454 |
tom.houday |
101 |
if [ -e /tmp/$FILE ]; then
|
1266 |
richard |
102 |
if [ $CRYPT -eq "1" ]; then
|
|
|
103 |
{
|
|
|
104 |
# 1 ) chiffrement/signature =1 ==> gpg --encrypt avec test de la clé présente
|
2454 |
tom.houday |
105 |
gpg --output $DIR_ARCHIVE/$FILE-crypt.gpg --armor --encrypt --recipient $GPG_USER /tmp/$FILE
|
1266 |
richard |
106 |
}
|
|
|
107 |
elif [ $SIGN -eq "1" ]; then
|
|
|
108 |
{
|
2454 |
tom.houday |
109 |
# 2) signature = 1 Chiffrement = 0 --> gpg --encrypt idem test de la clé présente
|
|
|
110 |
gpg --output $DIR_ARCHIVE/$FILE-sign.gpg --sign --recipient $GPG_USER /tmp/$FILE
|
|
|
111 |
gpg --output $DIR_ARCHIVE/$FILE-sign.gpg --sign --recipient $GPG_USER --detach-sign /tmp/$FILE
|
1266 |
richard |
112 |
}
|
|
|
113 |
else
|
|
|
114 |
{
|
|
|
115 |
# 3) chiffrement/signature = 0 --> cp simple avec suppression des droits d'écriture
|
|
|
116 |
cp /tmp/$FILE $DIR_ARCHIVE/.
|
|
|
117 |
}
|
|
|
118 |
fi
|
917 |
franck |
119 |
fi
|
1596 |
richard |
120 |
rm -rf /tmp/traceability-*
|
1157 |
stephane |
121 |
chown root:apache $DIR_ARCHIVE/*
|
917 |
franck |
122 |
;;
|
1247 |
crox53 |
123 |
--live | -l)
|
1308 |
richard |
124 |
mkdir -p $DIR_ARCHIVE
|
2454 |
tom.houday |
125 |
mkdir -p /tmp/live
|
1909 |
raphael.pi |
126 |
gap=7
|
1400 |
richard |
127 |
cd /var/log/nfsen/profiles-data/live/alcasar_netflow
|
2454 |
tom.houday |
128 |
find . -mtime -$gap -name 'nfcapd.[0-9]*' | xargs tar -cf /tmp/live/traceability-ALL-$NOW.tar;
|
1695 |
franck |
129 |
# Saving of the database
|
1827 |
raphael.pi |
130 |
/usr/local/bin/alcasar-mysql.sh --dump
|
1583 |
richard |
131 |
mv $(echo $(ls -rt $DIR_BASE/alcasar-users-database-*.sql.gz | tail -n 1 -)) /tmp/live/
|
1358 |
richard |
132 |
cp /var/log/firewall/traceability.log /tmp/live/traceability-HTTP-$NOW.log
|
|
|
133 |
tar -czf $DIR_ARCHIVE/traceability-$NOW.tar.gz /tmp/live/*
|
1247 |
crox53 |
134 |
rm -rf /tmp/live
|
|
|
135 |
;;
|
917 |
franck |
136 |
*)
|
|
|
137 |
echo "Unknown argument :$1";
|
|
|
138 |
echo "$usage"
|
|
|
139 |
exit 1
|
|
|
140 |
;;
|
|
|
141 |
esac
|
|
|
142 |
exit 0
|