Afin d'eviter de redemarrer le MySQL voici ce que l'on peut faire (apres avoir modifier les fichiers de configurations)
mysql> SET GLOBAL expire_logs_days=2; Query OK, 0 rows affected (0.02 sec) mysql> SHOW GLOBAL VARIABLES LIKE "%EXPIRE%"; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | expire_logs_days | 2 | +------------------+-------+ 1 row in set (0.00 sec)
mysql> PURGE BINARY LOGS BEFORE '2012-03-12 22:32'; Query OK, 0 rows affected (0.00 sec)
à completer
mysql> GRANT ALL PRIVILEGES ON table2toto.* TO 'toto'@'localhost' IDENTIFIED BY 'goodsecret';
FLUSH PRIVILEGES;
#!/bin/bash REP=/var/log/mysql-dump IPPROD=192.168.251.210/32 #### ATTENTION LES LOGROTATE PLANTERONT SI ON NE FAIT PAS CA LORS DE L'INSTALL # mkdir -p /var/log/mysql-dump # touch /var/log/mysql-dump/null.sql.gz # on considere que les info des cnx sont correctement renseignes ds ~/.my.cnf # On est bourrin : on fait un dump global puis des dump moins globaux # et en sus, on compress a la volée # On est faineant : on consigne la rotation de ces dump a logrorate # Et pour finir (ou plutot commencer) : on ne dump pas si on a l'ip de prod mysql ip addr ls | grep -q $IPPROD if [ $? == 0 ]; then echo Dump non fait : nous avons l IP de prod aka $IPPROD exit 2 fi echo Lancement du DUMP mkdir -p $REP echo Traitement global time mysqldump --master-data --all-databases | gzip -c -9 > $REP/_bigdump.sql.gz echo Traitement unitaire mysql -e "show databases\G" | grep "^Database: " | cut -f2 -d" " | while read db do echo === $db time mysqldump --master-data --databases $db | gzip -c -9 > $REP/$db.sql.gz done
/var/log/mysql-dump/*sql.gz { daily rotate 7 missingok create 640 mysql adm sharedscripts postrotate /opt/mysql/bkp-mysql-adlp endscript }
L'idee est de savoir si un Dump est un nouveau dump ou non…. les avantages:
Attention, ce code est a adapter au besoin, ici c'est un POC, codé une nuit d'Ô rage
mysql -e "show databases\G" | grep "^Database" | sed -s "s/^Database: //g" | while read db; do ./DoDump $db; done
#!/usr/bin/perl # License # GPLv2 (http://www.gnu.org/licenses/gpl-2.0.txt) # Authors # DELAPORTE Antoine # Versions # 1.0 adlp : poc, it's work just fine ;) use strict; use warnings; use Digest::MD5; my $db=$ARGV[0]; sub md5dump { my ($in,$out)=@_; my $md5 = Digest::MD5->new; my $eod = 0; my $ll = ""; while(<$in>) { if(/^-- Dump completed on /) { # -- Dump completed on 2013-10-02 23:02:52 chomp($ll=$_); $ll=~s/^-- Dump completed on //g; $ll=~tr/ :-/-/s; $eod=1; } elsif(!/^CHANGE MASTER TO /) { $md5->add($_); } if(defined($out)) { print $out $_;} } if($eod) { $eod=$md5->clone->hexdigest; } return(($eod,$ll)); } my ($omd,$date)=(0,undef); my ($omd,$date)=(0,undef); if(open(PD,"zcat $db.sql |")) { ($omd,$date)=md5dump(\*PD,undef); close(PD); } open(CD,"mysqldump --master-data --database $db |") or die "Impossible d'ouvrir la db $db"; open(ND,"| gzip -9 -c >$db.sql.new") or die "Impossible d'ouvirir le fichier de sortie"; my ($cmd,$null)=md5dump(\*CD,\*ND); close(CD); close(ND); # conserve bakup si dump courant pas null, et md5 different # on ne s'appuie pas sur la notion de l'evolution du binlog car entre 2 bkp cette db n'a pas changé if($cmd and $omd ne $cmd) { print "Dump necessaires et differents $db\n"; if($omd) { rename("$db.sql.gz","$db.sql.gz-$date"); } rename("$db.sql.new","$db.sql.gz"); } else { unlink("$db.sql.new"); }
Cf https://dev.mysql.com/doc/refman/5.0/en/purge-binary-logs.html
PURGE BINARY LOGS TO 'mysql-bin.010'; PURGE BINARY LOGS BEFORE '2008-04-02 22:46:26';
mysql> show global variables like 'expire_logs_days'; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | expire_logs_days | 10 | +------------------+-------+ 1 row in set (0.00 sec)
mysql> set global expire_logs_days=2; Query OK, 0 rows affected (0.03 sec)
Attention, dangereux
mysql> set GLOBAL read_only=true; mysql> exit; wurzel:~> [...] wurzel:~> mysql mysql> set GLOBAL read_only=false; [...] mysql> FLUSH TABLES WITH READ LOCK; mysql> [...] mysql> UNLOCK TABLES