917 |
franck |
1 |
#!/bin/bash
|
958 |
franck |
2 |
# $Id: alcasar-archive.sh 3230 2024-11-06 23:38:15Z rexy $
|
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 |
|
1300 |
richard |
21 |
DIR_BASE="$DIR_SAVE/base" # répertoire de sauvegarde de la base de données usagers
|
1308 |
richard |
22 |
DIR_ARCHIVE="$DIR_SAVE/archive" # répertoire de sauvegarde des archives de log
|
2454 |
tom.houday |
23 |
NOW="$(date +%G%m%d-%Hh%M)" # date et heure du moment
|
1596 |
richard |
24 |
DIR_TMP="/tmp/traceability-$NOW" # Répertoire temporaire d'export
|
|
|
25 |
FILE="traceability-$NOW.tar.gz" # Nom du fichier de l'archive
|
917 |
franck |
26 |
EXPIRE_DAY=365 # Nbre de jour avant suppression des fichiers journaux
|
1263 |
richard |
27 |
CRYPT="0" # chiffrement des logs ( 0=non / 1=oui) --> Si oui alors la signature est automatiquement activée
|
2454 |
tom.houday |
28 |
# log files encryption ( 0=no / 1=yes) --> if yes, the signature is automaticly enabled
|
917 |
franck |
29 |
SIGN="0" # Signature/empreinte des logs ( 0=non / 1=oui ) ATTENTION : nécessite la clé privée !!!
|
2454 |
tom.houday |
30 |
# Signature of log files ( 0=no / 1=yes ) ATTENTION : need the private key !!!
|
1157 |
stephane |
31 |
GPG_USER="" # utilisateur autorisé à déchiffrer les logs. Sa clé publique doit être connu dans le portefeuille gnupg de root (/root/.gnupg)
|
1263 |
richard |
32 |
# user allowed to decrypt the log files. Its public key must be known in the root keyring (/root/.gnupg)
|
917 |
franck |
33 |
|
1308 |
richard |
34 |
usage="Usage: alcasar-archive.sh {--live or -l} | {--now or -n} | {--clean or -c}"
|
917 |
franck |
35 |
|
|
|
36 |
nb_args=$#
|
|
|
37 |
args=$1
|
|
|
38 |
if [ $nb_args -eq 0 ]
|
|
|
39 |
then
|
|
|
40 |
nb_args=1
|
|
|
41 |
args="-h"
|
|
|
42 |
fi
|
|
|
43 |
|
|
|
44 |
|
|
|
45 |
function cleanup() {
|
2454 |
tom.houday |
46 |
# Nettoyage des fichiers archives
|
|
|
47 |
cd $DIR_SAVE
|
|
|
48 |
find . \( -mtime +$EXPIRE_DAY \) -a \( -name '*.gz' -o -name '*.sql' -o -name '' -o -name 'gpg' \) -exec rm -f {} \;
|
917 |
franck |
49 |
} # end function cleanup
|
|
|
50 |
|
|
|
51 |
|
|
|
52 |
function crypt() {
|
|
|
53 |
# Chiffrement des logs dans /var/Save/
|
|
|
54 |
find . \( -mtime -7 -o -ctime 0 \) -a \( -name '*log-*.gz' \) -exec gpg --output $DIR_ARCHIVE/$file/{}.gpg --encrypt --recipient $GPG_USER {} \;
|
|
|
55 |
} # end function crypt
|
|
|
56 |
|
|
|
57 |
function archive() {
|
2454 |
tom.houday |
58 |
mkdir -p $DIR_ARCHIVE
|
|
|
59 |
mkdir -p $DIR_TMP
|
|
|
60 |
nb_files=`ls $DIR_LOG/firewall/traceability.log*.gz 2>/dev/null | wc -w`
|
|
|
61 |
if [ $nb_files -ne 0 ]; then
|
|
|
62 |
mv $(echo $(ls -rt $DIR_LOG/firewall/traceability.log*.gz | tail -n 1 -)) $DIR_TMP/traceability-HTTP-$NOW.gz
|
|
|
63 |
fi
|
|
|
64 |
nb_files=`ls $DIR_BASE/alcasar-users-database-*.sql.gz 2>/dev/null | wc -w`
|
|
|
65 |
if [ $nb_files -ne 0 ]; then
|
|
|
66 |
mv $(echo $(ls -rt $DIR_BASE/alcasar-users-database-*.sql.gz | tail -n 1 -)) $DIR_TMP/
|
|
|
67 |
fi
|
|
|
68 |
cd /var/log/nfsen/profiles-data/live/alcasar_netflow
|
|
|
69 |
nb_files=`find . -mtime -7 -name 'nfcapd.[0-9]*' | wc -l`
|
|
|
70 |
if [ $nb_files -ne 0 ]; then
|
|
|
71 |
find . -mtime -7 -name 'nfcapd.[0-9]*' | xargs tar -cf $DIR_TMP/traceability-ALL-$NOW.tar;
|
|
|
72 |
fi
|
|
|
73 |
cd /tmp/
|
|
|
74 |
nb_files=`ls traceability-$NOW/* 2>/dev/null | wc -w`
|
|
|
75 |
if [ $nb_files -ne 0 ]; then
|
|
|
76 |
tar cvzf /tmp/$FILE traceability-$NOW/*
|
|
|
77 |
else echo "no file to archive"
|
|
|
78 |
fi
|
917 |
franck |
79 |
} # end archive
|
|
|
80 |
|
2454 |
tom.houday |
81 |
# Core script
|
917 |
franck |
82 |
case $args in
|
|
|
83 |
-\? | -h* | --h*)
|
|
|
84 |
echo "$usage"
|
|
|
85 |
exit 0
|
|
|
86 |
;;
|
|
|
87 |
--clean | -c)
|
1695 |
franck |
88 |
# Cleanup of files older than 365 days
|
917 |
franck |
89 |
cleanup
|
|
|
90 |
;;
|
|
|
91 |
--now | -n)
|
1695 |
franck |
92 |
|
|
|
93 |
# Cleanup of files older than 365 days
|
917 |
franck |
94 |
cleanup
|
1695 |
franck |
95 |
# make an archive
|
917 |
franck |
96 |
archive
|
1695 |
franck |
97 |
# Saving of the database
|
1827 |
raphael.pi |
98 |
/usr/local/bin/alcasar-mysql.sh --dump
|
1695 |
franck |
99 |
# Encryption of the archive
|
2454 |
tom.houday |
100 |
if [ -e /tmp/$FILE ]; then
|
1266 |
richard |
101 |
if [ $CRYPT -eq "1" ]; then
|
|
|
102 |
{
|
|
|
103 |
# 1 ) chiffrement/signature =1 ==> gpg --encrypt avec test de la clé présente
|
2454 |
tom.houday |
104 |
gpg --output $DIR_ARCHIVE/$FILE-crypt.gpg --armor --encrypt --recipient $GPG_USER /tmp/$FILE
|
1266 |
richard |
105 |
}
|
|
|
106 |
elif [ $SIGN -eq "1" ]; then
|
|
|
107 |
{
|
2454 |
tom.houday |
108 |
# 2) signature = 1 Chiffrement = 0 --> gpg --encrypt idem test de la clé présente
|
|
|
109 |
gpg --output $DIR_ARCHIVE/$FILE-sign.gpg --sign --recipient $GPG_USER /tmp/$FILE
|
|
|
110 |
gpg --output $DIR_ARCHIVE/$FILE-sign.gpg --sign --recipient $GPG_USER --detach-sign /tmp/$FILE
|
1266 |
richard |
111 |
}
|
|
|
112 |
else
|
|
|
113 |
{
|
|
|
114 |
# 3) chiffrement/signature = 0 --> cp simple avec suppression des droits d'écriture
|
|
|
115 |
cp /tmp/$FILE $DIR_ARCHIVE/.
|
|
|
116 |
}
|
|
|
117 |
fi
|
917 |
franck |
118 |
fi
|
1596 |
richard |
119 |
rm -rf /tmp/traceability-*
|
1157 |
stephane |
120 |
chown root:apache $DIR_ARCHIVE/*
|
917 |
franck |
121 |
;;
|
1247 |
crox53 |
122 |
--live | -l)
|
1308 |
richard |
123 |
mkdir -p $DIR_ARCHIVE
|
2454 |
tom.houday |
124 |
mkdir -p /tmp/live
|
1909 |
raphael.pi |
125 |
gap=7
|
1400 |
richard |
126 |
cd /var/log/nfsen/profiles-data/live/alcasar_netflow
|
2454 |
tom.houday |
127 |
find . -mtime -$gap -name 'nfcapd.[0-9]*' | xargs tar -cf /tmp/live/traceability-ALL-$NOW.tar;
|
1695 |
franck |
128 |
# Saving of the database
|
1827 |
raphael.pi |
129 |
/usr/local/bin/alcasar-mysql.sh --dump
|
1583 |
richard |
130 |
mv $(echo $(ls -rt $DIR_BASE/alcasar-users-database-*.sql.gz | tail -n 1 -)) /tmp/live/
|
1358 |
richard |
131 |
cp /var/log/firewall/traceability.log /tmp/live/traceability-HTTP-$NOW.log
|
|
|
132 |
tar -czf $DIR_ARCHIVE/traceability-$NOW.tar.gz /tmp/live/*
|
1247 |
crox53 |
133 |
rm -rf /tmp/live
|
|
|
134 |
;;
|
917 |
franck |
135 |
*)
|
|
|
136 |
echo "Unknown argument :$1";
|
|
|
137 |
echo "$usage"
|
|
|
138 |
exit 1
|
|
|
139 |
;;
|
|
|
140 |
esac
|
|
|
141 |
exit 0
|