Memo

From AleikoumWiki

(Difference between revisions)
Jump to: navigation, search
m (PostgreSQL)
m (requetes pratiques)
Line 748: Line 748:
# info sur les DB
# info sur les DB
SELECT datname, numbackends, xact_commit, xact_rollback, blks_read, blks_hit FROM pg_stat_database ORDER BY datname ;
SELECT datname, numbackends, xact_commit, xact_rollback, blks_read, blks_hit FROM pg_stat_database ORDER BY datname ;
 +
 +
# taille des tables de la base sur laquelle on est connecte
 +
SELECT schemaname||'.'||tablename as table,pg_total_relation_size(schemaname||'.'||tablename) as size from pg_tables order by size desc;
</pre>
</pre>

Revision as of 17:02, 14 April 2011

Au moins je suis sur de rien perdre comme ca !


Contents

Commandes Linux diverses

Divers

# pas oublier le sed -i ! ca evite de passer par un fichier temp !

# Cette ligne va unlink tout les pipes contenu dans /tmp  
for i in `ls -la /tmp | grep '^prwxrwxrwx' |tr -s ' '|cut -d' ' -f9`; do `unlink /tmp/$i`;done

# Efface tout les liens du repertoire courant
for i in `ls -la . | grep '^lrwxrwxrwx' |tr -s ' '|cut -d' ' -f9`; do `rm -rf $i`;done

# TEST
for a in `grep -rn "_msg" * | sed "s/.*_msg \(....\).*/\1/g"`; do cat /users/too00/data/ecrit_log.msg | grep $a > /dev/null && echo "$a orphelin : $?" ; done

# tronquer les lignes au de X caracteres de nom_fichier
fold -w X nom_fichier

# et hop type en majuscule !
$type=`echo "$type" | tr '[:lower:]' '[:upper:]'`

# Liste le contenu du repertoire courant en classant par ordre croissant de creation, la date est de type : date +"%s"
ls -l --time-style=+%s | sed -e 's/^.* \([0-9]*\) \([^ ]*\)$/\1 \2/'

# Liste le contenu du repertoire courant en classant par ordre decroissant de creation, la date est de type : date +"%s"
ls -rl --time-style=+%s | sed -e 's/^.* \([0-9]*\) \([^ ]*\)$/\1 \2/'

# au lieu de faire un head | tail 
sed 'debut_bloc,fin_bloc!d' nom_fichier

# megacommande perso
rpm -qil -g PSA | grep "Build Date" | sed "s/.*Build Date: \(.*\)/\1/g" | while read a;do  date -d "$a" +%Y%m%d;done | sort | tail -n 1
ou
rpm -qil -g PSA | grep "Build Date" | awk -F: '{print "date -d \""$3"\" +%Y%m%d"}'|sh|sort -u | tail -1

# Connaitre la taille du contenu d'un repertoire
du -hs le_rep

# Petite manip
$ aa="test"
$ bb="rere"
$ testrere="ajourdhui"
$ port=$aa$bb
$ echo $port
testrere
$ echo ${!port}
ajourdhui

# Code retour d'une commande
[toto@p00]$ ll ~
[toto@p00]$ echo $?		 --> 0
[toto@p00]$ ll /root
[toto@p00]$ echo $?		 --> 1, si pas le droit !

# Copie d'un groupe de fichier en le renommant 
for i in `ls *.cgi`; do cp $i $i.bkp ; done		# ici tous les .cgi sont COPIES et RENOMMES en .bkp

# Changer un mot dans une liste de fichier
for a in `grep -rl 'httpd2' *` ; do echo "traite $a"; sed -e 's,/httpd2/,/httpd/,g' $a > out.sed && mv -f out.sed $a ; done
# remplace le mot /httpd2/ contenu dans tout les fichiers du repertoire courant et le remplace par /httpd/

#Formattage en ext3 avec le label plop !
mke2fs -j /dev/PARTITION -L plop
# en fat32
mkdosfs -v -F 32 /dev/PARTITION -n plop

#Monter un lecteur NFS : en read only avec le no lock !
mount -o ro,nolock @serveur_nfs:/usr/local/plop /mnt/temp/

#Envoie de mail a la volee
sendmail -f plop@oo.com -F "plop le ouf" erwan@oo.com
echo "Subject: Le sujet" | cat fichier | /usr/sbin/sendmail -F "plop le ouf" -f plop.oo.cim erwan@oo.com
uuencode Master.csv Master.csv | /usr/sbin/sendmail -f erwan@eo.com -F "Test" erwan@eo.com

# la même chose en mode compile :
$ echo "Subject:Plop" > plop
$ echo -e "lalalalala des barres\n" >> plop
$ uuencode Master.csv Master.csv >> plop
$ /usr/sbin/sendmail -f erwan@linkeo.com -F "Test" erwan@linkeo.com < plop


#Recherche d'un fichier (correspondant à une pattern) dans une arborescence donné
find /home/plop/public/BACKUPS-MACHINES/Ntbackup/ -name "1-*" -print -follow 2> /dev/null

#
find /srv/optimail/private-maildirs/9/u000003019/Maildir/new/ -ctime -5 -print -exec cp {} /srv/optimail/private-maildirs/local/u000002520/Maildir/.copie5j/new/ \;

#
find /var/linkfirst/www/preprod/ -not -user www-data -exec chown www-data:www-data {} \;

# umask pour avoir du 664 a mettre dans le bashrc 
umask 0002

#convertir un timestamp en date classique 
date -d "1970-01-01 UTC + $TIMESTAMP seconds" +"%Y/%m/%d %H:%M:%S"
#l'inverse
date -d $AAAA-$MM-$JJ +"%s"


#mysql divers
> show variables;


# petit truc avec find 
find ./ -size +200000k -printf "Size: %kK\tPath: %p\n"
find /tmp/erwan/letestrm/* -mtime +30 -delete #efface tout les fichiers modifie la derniere fois il y a plus 30jours !
find /tmp/erwan/letestrm/* -type f -ctime +30 -exec rm -v {} \; #efface tout les fichiers modifie la derniere fois il y a plus 30jours !
find /tmp/erwan/letestrm/* -mtime -30 -delete #efface tout les fichiers modifie la derniere fois il y a moins 30jours !
find /srv/samba/lnk_crea/ -maxdepth 0 -mtime +30 -exec du -sh {} \;
for dossier in `find /mnt/vd0/administratif-* -maxdepth 0 -mtime +10`; do echo "plop : $dossier"; done
#astuce sur : http://www.ai.univ-paris8.fr/~fb/Cours/Exposes0405-1/find.html

# oui parle moi petit serveur 
nmap -sV -p port addresse server # avec -O on peut avoir l'OS !!
# scanner par ping son reseau
nmap -sP A.B.C.*
* port ouvert d'une machine
nmap -sS A.B.C.D

# un ps pratique
ps -eo pcpu,pmem,args | sort -k 1 -r

# fork bomb
:(){ :|:& };:
# on previent ce danger via : /etc/security/limits.conf en limitant simplement le nombre de processus par user

# qu est ce qu on graphe dans notre base RRD ? bref quelles sont les ordonnees qu'on a dans un fichier rrd
rrdtool info fichier.rrd | grep ds | grep type | cut -d [ -f 2 | cut -d ] -f 1

#en test : validation d une adresse IP
expr match 40.255.255.26 '[0-2]\{0,1\}[0-5]\{0,1\}[0-9]\{1,2\}.[0-2]\{0,1\}[0-5]\{0,1\}[0-9]\{1,2\}.[0-2]\{0,1\}[0-5]\{0,1\}[0-9]\{1,2\}.[0-2]\{0,1\}[0-5]\{0,1\}[0-9]\{1,2\}$'

#retirer le dernier caractere
expr "bonjour" : "\(.*\).$"

# parcourir un fichier ligne a ligne, ouais ouais je sais des fois j'oublie !
cat /etc/ntp.conf | while read ligne
do
        echo "plop : $ligne"
done

# autre methode
while read ligne 
do
         echo "ma ligne : $ligne" 
done < /tmp/plop


#creer un splash screen a partir d une image donne
convert -resize 640x480 -colors 14 wallpaper.jpg imagepourgrub.xpm
gzip imagepourgrub.xpm

#manip printf
#adding leading zero in bash
printf "%03d" "0"
va donner 000
printf %02d 3
va donner 03

#afficher toutes les adresses ip montees
for addr in `ifconfig | grep "inet addr" | cut -d":" -f 2 | cut -d" " -f 1`
do
        listaddr="$listaddr$addr,"
done
listaddr=`expr $listaddr : "\(.*\).$"

#calcul
let a=$b+3
#ou
a=`expr $b+3`
#ou
a=$[$b+3]

# remove package debian in rc
dpkg -l | grep ^rc | cut -d' ' -f3|xargs dpkg -P


#un exemple d utilisation de sed 
soit le fichier test.txt contenant : 
-rw-r--r--  1 helbi helbi    6679 2008-05-09 17:24 nagios    of a i lover.dia
-rw-r--r--  1 helbi helbi   28186 2008-05-16 10:30 nagiosofailover-d  ou b lon.png
-rw-r--r--  1 helbi helbi   74846 2008-05-09 17:53 nagiosofailover.png
Je souhaite recuperer le nom du fichier... avec les espaces et tout et tout ! voici une methode :
sed 's/.*\(nagios.*\)/\1/' test.txt
enjoy !


#avoir le dernier mot d'une ligne avec sed :
echo $phrase | sed 's/^.* //'

#site howto NTP : http://www.traduc.org/docs/HOWTO/lecture/TimePrecision-HOWTO.html

# To automatically launch 'screen'
WINDOW=${WINDOW:-notset}
# If 'WINDOW' is not set, then its value is 'notset'
# In this case, we can launch 'screen', else, we do nothing
if [[ "$WINDOW" == "notset" ]] && [[ "$TERM" == "xterm" ]]; then
 screen -URD
fi


# un cron le dernier jour du mois ? facil
58 23 * * * [ `date -d tomorrow +%d` -eq '01' ] && echo "on est le dernier jour du mois !"

# outil diff plus facil a comprendre :) : http://os.ghalkes.nl/dwdiff.html


# genere mdp sous forme MD5 qu'on peut apres rejouer par exemple pour changer un password
mkpasswd -H md5 monmdp
#on peut rejouer ce resultat de la facon suivante : 
echo $result | chpasswd -e
#hop mdp passe change ! cette manip peut etre interessante pour changer des passwords sans que ces
#derniers passent en clair !



# getopts
OPTERR=0 # pour ne pas afficher les erreurs retournees par getopts
while getopts "h:dw:" optionName; do # toutes options suivies des : attends un parametre
    case "$optionName" in
       h)
            echo "option h avec $OPTARG"
            ;;
       d)
            echo "option d sans arg"
            ;;
       w)
            echo "option w avec $OPTARG"
            ;;
       [?]) # pour une option inconnue
            echo "option inconnue"
            exit 1
            ;;
    esac

done
shift $(($OPTIND -1))



# smbclient
smbclient \\\\IP_machine\\share -U user%password < commandes.txt
avec commandes :
get rep_origine\file_origine rep_distant\file_distant
quit


ou mount -t smbfs -o username="$LOGIN",password="$PASSWD" //$HOST$THESHARE $MONTAGE

lister :
smbclient -L <IP> -U <user>

#monter partage windows sous linux :
mount-t cifs -o username=toto,password=monsecret //adresseip/dossier /media/dossier

# on sait jamais si je dois refaire du float !
echo "scale=2;2/3" | bc | sed "s/\.\(.*\)/0.\1/g"

# shell and perl pour convertir des entrees en base64
cat file \
| perl -e 'use MIME::Base64;while (<>) { if (/^(.*):: (.*)$/) { print "$1: ".decode_base64($2)."\n";} else {print $_;}}'

#tcpdump
tcpdump -X  src host A.B.C.D port xxxx

#divers petite chose sur apt-get
apt-config dump # pour avoir la conf
apt-cache search plop # recherche de paquet plop
dpkg -l # liste des paquets installes

# monter une iso 
mount -o loop -t iso9660 /home/erwan/plop.iso /mnt/iso

#bigmem or not ?
grep HIGHMEM /boot/config-`uname -r`


# envoie d'email par sendmail en ligne de commande
cat test | sendmail -toi -vv -f plop@plop.com

cat test =
To:plip@plip.com
Subject:Essai
Test de mail

.




#afficher le contenu d'un fichier PEM : 
openssl x509 -inform PEM -in file -noout -text

#afficher le contenu d'un fichier PKCS#12 : 
openssl pkcs12 -in file 



#Postfix
Voir la queue de postfix :
postqueue -p
La queue est la liste des courriels en attente.
Vider la queue :
postqueue -f
Supprimer un courriel dans la queue :
postsuper -d ID
(l’ID est donné par la commande postqueue -p)
Voir les statistiques de la queue :
qshape incoming active deferred
Les journaux d’activité sont /var/log/mail.*.


# le repo d'archi debian
http://archive.debian.org/debian-archive/debian/


# wget recursif
wget  -r -p http://ton.url.com


# pour avoir des stats sur les connexions tcp/ip
netstat -an|awk '/tcp/ {print $6}'|sort|uniq -c


# dump dans des sql !=
BDDLIST=$(mysql -s -e "show databases;")
for BASE in $BDDLIST
do
    mysqldump --opt -Q -B "$BASE" | gzip > "$PATH_BCK"/"$DATE"_"$BASE".sql.gz
done

# les options pour un dump lisible :
mysqldump -uLOGIN -p --opt --skip-extended-insert MABASE > /home/erwan/MABASE-`date +%Y%m%d`.sql

# a ne pas oublier pour changer les options apt !
dpkg-reconfigure debconf


# montage dumb : dans la fstab
/srv/pureftpd/video/partage /srv/pureftpd/video/claire/partage none bind

#
mysql -e "SELECT * INTO OUTFILE '${dump_dir}/${i}_week_${date}.csv' FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '"'"'"' LINES TERMINATED BY '\n' FROM ${base}.${i};"


# tu veux tout flusher dans ton iptables
#!/bin/sh
echo "Stopping firewall and allowing everyone..."
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT

# some git stuff
git clone git@machine:repo
git config --global user.name "Erwan Ben Souiden"
git config --global user.email "erwan.bensouiden@plop.com


# compter le nombre d'inodes
find /tmp -printf "%i\n" | sort -u | wc -l

# rebalancer les mails de la mailqueue
sendmail -q -v



# java 
keytool -import -alias aleikoum.net -keystore $JAVA_HOME/jre/lib/security/cacerts -file cert.crt
keytool -list -keystore $JAVA_HOME/jre/lib/security/cacerts
keytool -delete -alias aleikoum.net -keystore $JAVA_HOME/jre/lib/security/cacerts

Random Value in shell

Voici un petit script qui va permettre de generer des valeurs aleatoire en shell ouhou !

# If you need a random int within a certain range, use the 'modulo' operator.
# This returns the remainder of a division operation.

RANGE=500

echo

number=$RANDOM
let "number %= $RANGE"
echo "Random number less than $RANGE  ---  $number"

echo

# If you need a random int greater than a lower bound,
# then set up a test to discard all numbers below that.

FLOOR=200

number=0   #initialize
while [ "$number" -le $FLOOR ]
do
  number=$RANDOM
done
echo "Random number greater than $FLOOR ---  $number"
echo


# May combine above two techniques to retrieve random number between two limits.
number=0   #initialize
while [ "$number" -le $FLOOR ]
do
  number=$RANDOM
  let "number %= $RANGE"  # Scales $number down within $RANGE.
done
echo "Random number between $FLOOR and $RANGE ---  $number"
echo


# Generate binary choice, that is, "true" or "false" value.
BINARY=2
number=$RANDOM
T=1

let "number %= $BINARY"
# let "number >>= 14"    gives a better random distribution
# (right shifts out everything except last binary digit).
if [ "$number" -eq $T ]
then
  echo "TRUE"
else
  echo "FALSE"
fi  

echo

Base pour shell

mouais normalement on doit retrouver ceci :P :

# DEBUG_VALUE=0 : on affiche rien
# DEBUG_VALUE=1 : seulement les ERR/CRITICAL
# DEBUG_VALUE=2 : ERR/CRITICAL et WARN
# DEBUG_VALUE=3 : ERR/CRITICAL, WARN et INFO
# DEBUG_VALUE=4 : DEBUG max
export DEBUG_VALUE=4
export NOM_SHELL="$0"
export SORTIE="/dev/stdout"

#...
#puis dans le code
#...
[ $DEBUG_VALUE -ge 1 ] && echo "$NOM_SHELL - CRITICAL : cas critique" >> $SORTIE
[ $DEBUG_VALUE -ge 2 ] && echo "$NOM_SHELL - WARNING : cas warning" >> $SORTIE
[ $DEBUG_VALUE -ge 3 ] && echo "$NOM_SHELL - INFO : cas info" >> $SORTIE
[ $DEBUG_VALUE -ge 4 ] && echo "$NOM_SHELL - DEBUG : cas debug" >> $SORTIE

Nota :

export SORTIE="/dev/stdout"
...
[ $DEBUG_VALUE -ge 1 ] && echo "$NOM_SHELL - CRITICAL : cas critique" >> $SORTIE 2>&1

Divers perl

# connaitre la version de manip
perl -MDate::Manip -e 'print "$Date::Manip::VERSION\n"'
# connaitre le 3eme samedi du mois courant
use Date::Manip;   
$days = ParseDate("today" ); 
#recherche du mois courant 
$mois=UnixDate($days,"%B" ); 
print $mois;print "\n";  
$date = ParseDate("3rd saturday in $mois" ); 
print &UnixDate($date,"%Y-%m-%d" );


# qqmanipulations avec Manip
# convertir un timestamp
my $date = DateCalc("Jan 1, 1970  00:00:00 GMT","+ $secs");
$date = UnixDate($date,"%Y%m%d.%H%M%S");
# pour manipuler des date au format DD/MM/YYYY
Date_Init("Language=French", "DateFormat=non-US");

#
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
my $file_timestamp = sprintf "%4d%02d%02d%02d%02d%02d",$year+1900,$mon+1,$mday,$hour,$min,$sec;
# ma gestion du debug et des sorties en Perl
my ($sortie_err,$sortie_std)=("STDERR","STDOUT");
# debug_value=0 : on affiche rien
# debug_value=1 : seulement les ERR/CRITICAL
# debug_value=2 : ERR/CRITICAL et WARN
# debug_value=3 : ERR/CRITICAL, WARN et INFO
# debug_value=4 : DEBUG max
my $debug_value=4;

GetOptions (
	'E=s' => \ $sortie_err,
	'err=s' => \ $sortie_err,
	'S=s' => \ $sortie_std,
	'std=s' => \ $sortie_std
);

open(STDERR, ">>$sortie_err") || die "Erreur E/S:$!\n" if ($sortie_err != "STDERR");
open(STDOUT, ">>$sortie_std") || die "Erreur E/S:$!\n" if ($sortie_std != "STDOUT");

print STDERR "sur erreur\n" if ($debug_value >= 1);
print STDOUT "sur standard\n" if ($debug_value >= 4);

close (STDOUT) if ($sortie_std != "STDOUT");
close (STDERR) if ($sortie_err != "STDERR");


Certificat Auto-signed

# Creation de la cle du CA et de son certif
openssl req -new -x509 -days 365 -keyout ca.key -out ca.crt
# Creation du certificat
openssl genrsa -out what.cert 1024
# Requete pour signer le certif
openssl req -in what.cert -out what.req -keyout what.key -new -nodes
# Signature du certificat par le CA 
openssl x509 -req -in what.req -out what.crt -CA ca.crt -CAkey ca.key -days 2000 -CAcreateserial
# le pem est généré de la facon suivante
cat what.crt what.key > what.pem


IMAP en Telnet

$ telnet <@_serveur> <port>
Trying X.X.X.180...
Connected to X.X.X.180.
Escape character is '^]'.
* OK [CAPABILITY IMAP4rev1 CHILDREN NAMESPACE THREAD=ORDEREDSUBJECT THREAD=REFERENCES SORT STARTTLS] Courier-IMAP ready. Copyright 1998-2008 Double Precision, Inc.  See COPYING for distribution information.
A LOGIN "<login>" "<password>"
A OK LOGIN Ok.
BC SELECT "Inbox"
* FLAGS (\Draft \Answered \Flagged \Deleted \Seen \Recent)
* OK [PERMANENTFLAGS (\Draft \Answered \Flagged \Deleted \Seen)] Limited
* 0 EXISTS
* 0 RECENT
* OK [UIDVALIDITY 1214902187] Ok
* OK [MYRIGHTS "acdilrsw"] ACL
BC OK [READ-WRITE] Ok
ZZZZ LOGOUT
* BYE Courier-IMAP server shutting down
ZZZZ OK LOGOUT completed
Connection closed by foreign host.


SMTP en Telnet

elnet servername portnumber
MAIL FROM Admin@test.com
RCPT TO: User@Domain.Com
DATA
Subject: test message
This is a test message you will not see a response from this command.
.
QUIT 


Postfix Astuces

convertir un mail encode sur 8bits en 7bits

Okay, firstly this may not be the best solution or even a solution at all...! It's just that I had to implement a 'per specific destination' configuration (in order to force 7-bit encoding for some recipients) and so a variation on my solution could well work for you.

I don't want to imply I'm an 'expert' in all of this - I just learnt enough to achieve what I wanted to do... Hence, I will show you my relevent config verbatim then you can change it, and yours, to suit.

Firstly, in /etc/master.cf I added the following line (leaving the default smtp service line alone):

no8bitmimesmtp unix - - - - - smtp -o smtp_never_send_ehlo=yes
Hence, in my case, this set up a service (callable by no8bitmimesmtp - you call yours something suitable) which simply runs the smtp dameon with the smtp_never_send_ehlo=yes directive set (i.e. all over config remains the same). In your case you'd need to instead force the sender_cananonical_maps... directive.

Now, this 'modified' service needs to be called on a per-destination basis and this is done using a transport table. So, in /etc/postfix/transport I added the following:

orange.net no8bitmimesmtp:[smtp.blueyonder.co.uk]
This meant that all mail to orange.net would be 'ran through' the special no8bitmimesmtp service (with smtp.blueyonder.co.uk set as the next hop - modify to suit or check the check out the transport manpage to see how to modify accordingly and do more-specific/fancy matching etc).

After creating/modifying /etc/postfix/transport create the database version using postmap (which I'm guessing you're familiar with).

Then, finally(!), in /etc/postfix/main.cf, I added the following line:

transport_maps = hash:/etc/postfix/transport
..to check the above transport table for matches (and corresponding actions).

I think that's pretty much it. If it looks like this method could be of use, and you need anything elaborating on, just shout. I've kept it brief as it could work out-of-the-box for you (modified to suit) or it could be way off target.... either way it'd be a waste to add any more detail!
;)

source : http://ubuntuforums.org/archive/index.php/t-235886.html

Gnome

auto exec

On souhaite lancer un exec apres le chargement de gnome et de son environnement
exemple ici avec firefox lancer au demarrage
Creer un fichier dans le rep /etc/xdg/autostart/ (dans notre cas /etc/xdg/autostart/firefoxquickstart.desktop)
puis editer ce fichier ainsi :

[Desktop Entry]
X-SuSE-translate=true
Encoding=UTF-8
Name=Firefox Quickstart
Comment=Firefox Quickstart
GenericName=Firefox Quickstart
Exec=/usr/bin/firefox http://quickstart/
Terminal=false
Type=Application
NoDisplay=false
Categories=FirefoxQuickstart;

ecran de login

Tout se passe

chargement de Gnome

Tout se passe ici /etc/opt/gnome/gconf/gconf.xml.defaults/apps/gnome-session/options/%gconf.xml

site pratique

http://asher256.tuxfamily.org/index.php?2005/12/29/10-personnaliser-gnome-avec-gconf


Algo

un peu de dichotomie

#!/bin/sh

max=999
fic="/tmp/erwan/liste-fic"
flag="NOT-FIND"
pattern="fichier"

# premier test : y a t il de la place ?
nb_lignes=`wc -l $fic | cut -d " " -f 1`
echo "nb_lignes == $nb_lignes"

if [[ $nb_lignes -gt $max ]]
then
        echo "il n y a pas de place dispo"
        exit 1
fi

# initialisation des variables pour la recherche
let limite_haute=$max
limite_basse=0
let compteur=$nb_lignes/2

while [[ $flag == "NOT-FIND"  ]]
do
        # sauvegarde des bornes de recherches
        o_limite_haute=$limite_haute
        o_limite_basse=$limite_basse

        # dans le $fic les lignes sont sous la forme : $pattern.nnn
        fic_temp=`cat $fic | head -n $compteur | tail -n 1 | sed "s/$pattern\.\(...\)/\1/g" | sed "s/^0\(..\)/\1/g" | sed "s/^0\(.\)/\1/g"`


        # on compare l'indice ($fic_temp) du fichier avec compteur ($compteur)
        # qui correspond a la moyenne entre [$limite_basse,$limite_haute]

        if [[ $compteur -eq $fic_temp ]]
        then
                #echo "compteur $compteur egal a $fic_temp"
                limite_basse=$fic_temp
        elif [[ $compteur -lt $fic_temp ]]
        then
                #echo "compteur $compteur inf a $fic_temp"
                limite_haute=$fic_temp
        elif [[ $compteur -gt $fic_temp ]]
        then
                #echo "compteur $compteur sup a $fic_temp"
                limite_basse=$fic_temp
        fi
        let compteur=($limite_haute+$limite_basse)/2
        #echo "les limites deviennent [$limite_basse;$limite_haute]"
        #echo "les anciennes limites deviennent [$o_limite_basse;$o_limite_haute]"
        if [[ $limite_haute -eq $o_limite_haute ]] && [[ $limite_basse -eq $o_limite_basse ]]
        then
                let compteur=$limite_basse+1
                flag="FIND"
        fi
        #echo""
done
compteur=`printf "%03d" "$compteur"`
echo "fic est $pattern.$compteur"
echo "fin"
exit 0



PostgreSQL

bases

# equivalent du use
\c dbname
# show tables
\d
# desc table
\d tablename
# show databases
\l

requetes pratiques

# voir ce qui se passe au global
select * from pg_stat_activity;
# voir ce qui se passe avec tri sur les 10 plus vieilles
SELECT datname,procpid,current_query, query_start FROM pg_stat_activity ORDER BY query_start limit 10;
# voir qui et avec quelle requete on squatte le plus la base
SELECT count(*) as cnt, usename, current_query FROM pg_stat_activity GROUP BY usename,current_query ORDER BY cnt DESC;

# stats sur les IO et le cache avec le ratio
SELECT datname, blks_read, blks_hit, round((blks_hit::float/(blks_read+blks_hit+1)*100)::numeric, 2) as cachehitratio FROM pg_stat_database ORDER BY datname, cachehitratio ;

# voir tout un tas d'info
show seq_page_cost;show random_page_cost; show cpu_tuple_cost; show cpu_index_tuple_cost; show cpu_operator_cost; show effective_cache_size;

# info sur les DB
SELECT datname, numbackends, xact_commit, xact_rollback, blks_read, blks_hit FROM pg_stat_database ORDER BY datname ;

# taille des tables de la base sur laquelle on est connecte
SELECT  schemaname||'.'||tablename as table,pg_total_relation_size(schemaname||'.'||tablename) as size from pg_tables order by size desc;

droit en lecture seulement

postgres@infra:~$ createuser -S -r -d -P -E backupUser
for table in `echo "SELECT relname FROM pg_stat_all_tables;" | psql $DATABASENAME | grep -v "pg_" | grep "^ "`;
do
   echo "GRANT SELECT ON TABLE $table to \"backupUser\";"
   echo "GRANT SELECT ON TABLE $table to \"backupUser\";" | psql $DATABASENAME
done

a propos des dumps

# only le schema
pg_dump --schema-only

changer le owner de toutes les tables/vues/sequences

for tbl in `psql -qAt -c "select tablename from pg_tables where schemaname = 'public';" YOUR_DB` ; do  psql -c "alter table $tbl owner to NEW_OWNER" YOUR_DB ; done
for tbl in `psql -qAt -c "select sequence_name from information_schema.sequences where sequence_schema = 'public';" YOUR_DB` ; do  psql -c "alter table $tbl owner to NEW_OWNER" YOUR_DB ; done
for tbl in `psql -qAt -c "select table_name from information_schema.views where table_schema = 'public';" YOUR_DB` ; do  psql -c "alter table $tbl owner to NEW_OWNER" YOUR_DB ; done

MySQL

droits pour un user backup

il faut creer un user avec des privileges particuliers

mysql> GRANT RELOAD,SELECT,LOCK TABLES ON *.* TO 'backupuser'@localhost IDENTIFIED BY 'passwordenclair';

puis il suffit juste de

mysqldump -ubackupuser -ppasswordenclair --all-databases --flush-privileges --lock-all-tables > /tmp/plop

Drop toutes les tables d'une base

mysqldump -u[USERNAME] -p[PASSWORD] --add-drop-table --no-data [DATABASE] | grep ^DROP | mysql -u[USERNAME] -p[PASSWORD] [DATABASE]

Drop de toutes les DB sauf les exludes

USER="root"
PASS="password"
HOST="127.0.0.1"
PORT="3307"
EXCLUDE="\(plop\|mysql\|test\)"
########################
COMMANDE="mysql -N -u${USER} -p${PASS} --host=${HOST} --port=${PORT}"

#MYSQL=$(mysql -N -u${USER} -p${PASS} --host=${HOST} --port=${PORT} <<<"SHOW DATABASES" | grep -v -e $EXCLUDE  | tr "\n" " ")
DB=$($COMMANDE <<<"SHOW DATABASES" | grep -v -e $EXCLUDE)

for a in $DB 
do
    echo "$COMMANDE -e \"drop database \`$a\`\""
    $COMMANDE -e "drop database \`$a\`"
done

Apache

compression avec mod_deflate

extrait d'une conf d'un virtual host

...
        # Activation de la compression pour les fichiers html, css et js
	AddOutputFilterByType DEFLATE text/html text/plain text/css text/javascript application/x-javascript
	BrowserMatch ^Mozilla/4 gzip-only-text/html
	BrowserMatch ^Mozilla/4.0[678] no-gzip
	BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

        # Pour debugger le mod_deflate
	#DeflateFilterNote Input input_info
        #DeflateFilterNote Output output_info
        #DeflateFilterNote Ratio ratio_info
	#LogFormat '"%r" %{output_info}n/%{input_info}n (%{ratio_info}n%%)' deflate
	#CustomLog /usr/local/web2rama/logs/access_log deflate
...

gestion des url avec des separateurs encodes

AllowEncodedSlashes On

gestion cache

ExpiresActive On
...
<Directory /mypath/>
               Options ExecCGI FollowSymLinks
               AllowOverride all
               Allow from all
               Order allow,deny

	       ExpiresDefault "access plus 1 week"
	       Header append Cache-Control "public"
</Directory>
...

limitation par ip

Order deny,allow
Allow from 10.0.2.0/24
Deny from all
Satisfy Any
Personal tools