Mise à jour automatique des scripts : Différence entre versions
De wikiGite
Ligne 2 : | Ligne 2 : | ||
#!/bin/bash | #!/bin/bash | ||
# FSo 2010-03 | # FSo 2010-03 | ||
− | # V1. | + | # V1.1 |
− | # Update system scripts | + | # Update system scripts |
SCRIPTS_DIR=/opt/systools | SCRIPTS_DIR=/opt/systools | ||
UPDATE_SERVER=http://www.systea.net/public/MaJ | UPDATE_SERVER=http://www.systea.net/public/MaJ | ||
CHANGED=0 | CHANGED=0 | ||
+ | REPORT="" | ||
REPORT_EMAIL=fsoyer@systea.net | REPORT_EMAIL=fsoyer@systea.net | ||
Ligne 15 : | Ligne 16 : | ||
# Get files | # Get files | ||
cd $SCRIPTS_DIR | cd $SCRIPTS_DIR | ||
+ | # script itself : if updated, it overwrite itself and exits : | ||
+ | # don't know what it could do after being overwrited, so tomorrow is another day | ||
+ | SCR=maj_scripts.sh | ||
+ | wget -q $UPDATE_SERVER/$SCR.maj > /dev/null 2>&1 | ||
+ | # If exists and download is ok | ||
+ | if [ $? -eq 0 ] | ||
+ | then | ||
+ | cmp $SCR.maj $SCR > /dev/null 2>&1 | ||
+ | # If downloaded file differs from local file, install it | ||
+ | if [ $? -gt 0 ] | ||
+ | then | ||
+ | # If there is no backup copy of file | ||
+ | if [ ! -f $SCR.old ] | ||
+ | then | ||
+ | cp $SCR $SCR.old | ||
+ | fi | ||
+ | mv $SCR.maj $SCR | ||
+ | chmod 700 $SCR | ||
+ | logger -t update_scripts "$SCR has changed." | ||
+ | exit 0 | ||
+ | else | ||
+ | rm -f $SCR.maj* | ||
+ | fi | ||
+ | fi | ||
for SCR in $(ls *.sh) | for SCR in $(ls *.sh) | ||
do | do | ||
Ligne 33 : | Ligne 58 : | ||
chmod 700 $SCR | chmod 700 $SCR | ||
logger -t update_scripts "$SCR has changed." | logger -t update_scripts "$SCR has changed." | ||
+ | REPORT="$(echo -e "$SCR has changed.\n") $REPORT" | ||
CHANGED=1 | CHANGED=1 | ||
else | else | ||
Ligne 43 : | Ligne 69 : | ||
( | ( | ||
echo "Subject: [scripts_update] $(hostname) Daily run" | echo "Subject: [scripts_update] $(hostname) Daily run" | ||
− | echo | + | echo $REPORT |
) | /usr/sbin/sendmail $REPORT_EMAIL | ) | /usr/sbin/sendmail $REPORT_EMAIL | ||
fi | fi |
Version du 29 mars 2010 à 13:00
Dans /opt/systools, créer maj_scripts.sh
#!/bin/bash # FSo 2010-03 # V1.1 # Update system scripts SCRIPTS_DIR=/opt/systools UPDATE_SERVER=http://www.systea.net/public/MaJ CHANGED=0 REPORT="" REPORT_EMAIL=fsoyer@systea.net # Verify if standard directory exist if [ -d $SCRIPTS_DIR ] then # Get files cd $SCRIPTS_DIR # script itself : if updated, it overwrite itself and exits : # don't know what it could do after being overwrited, so tomorrow is another day SCR=maj_scripts.sh wget -q $UPDATE_SERVER/$SCR.maj > /dev/null 2>&1 # If exists and download is ok if [ $? -eq 0 ] then cmp $SCR.maj $SCR > /dev/null 2>&1 # If downloaded file differs from local file, install it if [ $? -gt 0 ] then # If there is no backup copy of file if [ ! -f $SCR.old ] then cp $SCR $SCR.old fi mv $SCR.maj $SCR chmod 700 $SCR logger -t update_scripts "$SCR has changed." exit 0 else rm -f $SCR.maj* fi fi for SCR in $(ls *.sh) do wget -q $UPDATE_SERVER/$SCR.maj > /dev/null 2>&1 # If exists and download is ok if [ $? -eq 0 ] then cmp $SCR.maj $SCR > /dev/null 2>&1 # If downloaded file differs from local file, install it if [ $? -gt 0 ] then # If there is no backup copy of file if [ ! -f $SCR.old ] then cp $SCR $SCR.old fi mv $SCR.maj $SCR chmod 700 $SCR logger -t update_scripts "$SCR has changed." REPORT="$(echo -e "$SCR has changed.\n") $REPORT" CHANGED=1 else rm -f $SCR.maj* fi fi done if [ $CHANGED -eq 1 ] then ( echo "Subject: [scripts_update] $(hostname) Daily run" echo $REPORT ) | /usr/sbin/sendmail $REPORT_EMAIL fi fi
Ne pas oublier
chmod 700 maj_scripts.sh
Puis créer juste les noms des scripts pour une première mise à jour. Ex :
touch ossec-local-rules.sh touch dumpmysql.sh touch check_services.sh
Et planifier tout de suite ces scripts pour ne pas oublier !
cd /etc/cron.hourly ln -s /opt/systools/check_services.sh cd /etc/cron.daily ln -s /opt/systools/ossec-local-rules.sh ln -s dumpmysql.sh
Planifier aussi maj_scripts par un lien dans /etc/cron.daily, En tête de liste pour qu'il se lance avant les autres scripts, et le lancer une première fois manuellement.
cd /etc/cron.daily ln -s /opt/systools/maj_scripts.sh 0maj_scripts.sh ./0maj_scripts.sh ls -l /opt/systools/