<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="fr">
	<id>https://wikiold.home.tartarefr.eu/index.php?action=history&amp;feed=atom&amp;title=DBMS%2FMySQL%2Fbackup_scripts</id>
	<title>DBMS/MySQL/backup scripts - Historique des versions</title>
	<link rel="self" type="application/atom+xml" href="https://wikiold.home.tartarefr.eu/index.php?action=history&amp;feed=atom&amp;title=DBMS%2FMySQL%2Fbackup_scripts"/>
	<link rel="alternate" type="text/html" href="https://wikiold.home.tartarefr.eu/index.php?title=DBMS/MySQL/backup_scripts&amp;action=history"/>
	<updated>2026-07-06T22:31:20Z</updated>
	<subtitle>Historique des versions pour cette page sur le wiki</subtitle>
	<generator>MediaWiki 1.43.8</generator>
	<entry>
		<id>https://wikiold.home.tartarefr.eu/index.php?title=DBMS/MySQL/backup_scripts&amp;diff=1867&amp;oldid=prev</id>
		<title>Didier : Page créée avec « == Scripts Utiles pour les backups MySQL ==  Voici quelques scripts utiles qui permettent de faciliter les sauvegardes et/ou restaurations dans MySQL.  === Extraction de t... »</title>
		<link rel="alternate" type="text/html" href="https://wikiold.home.tartarefr.eu/index.php?title=DBMS/MySQL/backup_scripts&amp;diff=1867&amp;oldid=prev"/>
		<updated>2013-07-12T09:48:40Z</updated>

		<summary type="html">&lt;p&gt;Page créée avec « == Scripts Utiles pour les backups MySQL ==  Voici quelques scripts utiles qui permettent de faciliter les sauvegardes et/ou restaurations dans MySQL.  === Extraction de t... »&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Nouvelle page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Scripts Utiles pour les backups MySQL ==&lt;br /&gt;
&lt;br /&gt;
Voici quelques scripts utiles qui permettent de faciliter les sauvegardes et/ou restaurations dans MySQL.&lt;br /&gt;
&lt;br /&gt;
=== Extraction de table depuis un dump texte SQL (extract.sh): ===&lt;br /&gt;
&lt;br /&gt;
Ce script permet d&amp;#039;extraire des bases et/ou des tables d&amp;#039;un fichier dump. Cela va permettre, par exemple, de ressortir le fichier sql d&amp;#039;une seule base lorsque l&amp;#039;on fait des backups de toutes les bases dans un seul fichier de dump avec une commande du type:&lt;br /&gt;
&lt;br /&gt;
 # mysql -uuser -ppassword --all-databases &amp;gt; backup_of_the_day.sql&lt;br /&gt;
&lt;br /&gt;
Voici le script:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Bash&amp;quot;&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
&lt;br /&gt;
# &lt;br /&gt;
# Extracts a schema or a table from a text dump generated by mysqldump or phpMyAdmin&lt;br /&gt;
# (c) 2012 Maciej Dobrzanski http://www.dbasquare.com/&lt;br /&gt;
# Released under GNU General Public License, version 2&lt;br /&gt;
# &lt;br /&gt;
&lt;br /&gt;
function help()&lt;br /&gt;
{&lt;br /&gt;
  cat &amp;lt;&amp;lt;EOF&lt;br /&gt;
  Usage: [-h] -f &amp;lt;dump file&amp;gt; -d &amp;lt;database name&amp;gt; [-t &amp;lt;table&amp;gt;]&lt;br /&gt;
EOF&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
while getopts &amp;quot;hf:d:t:&amp;quot; flag ; do&lt;br /&gt;
  case $flag in&lt;br /&gt;
    h)&lt;br /&gt;
      help&lt;br /&gt;
      exit 255&lt;br /&gt;
      ;;&lt;br /&gt;
    f)&lt;br /&gt;
      EX_DFILE=${OPTARG}&lt;br /&gt;
      ;;&lt;br /&gt;
    d)&lt;br /&gt;
      EX_SCHEMA=${OPTARG}&lt;br /&gt;
      ;;&lt;br /&gt;
    t)&lt;br /&gt;
      EX_TABLE=${OPTARG}&lt;br /&gt;
      ;;&lt;br /&gt;
    \?)&lt;br /&gt;
      echo &amp;quot;Invalid option: -$OPTARG&amp;quot; &lt;br /&gt;
      ;;&lt;br /&gt;
  esac&lt;br /&gt;
done&lt;br /&gt;
&lt;br /&gt;
: ${EX_DFILE?&amp;quot;Missing argument: -f must be specified; try &amp;#039;$0 -h&amp;#039; for details&amp;quot;}&lt;br /&gt;
: ${EX_SCHEMA?&amp;quot;Missing argument: -d must be specified; try &amp;#039;$0 -h&amp;#039; for details&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
if [ ! ${EX_TABLE} ] ; then&lt;br /&gt;
  sed -n \&lt;br /&gt;
      -e &amp;#039;1,/^-- \(Current \)\?Database/{/^\(--.*\)*\?$/d ; p } ; /^-- \(Current \)\?Database: `&amp;#039;&amp;quot;${EX_SCHEMA}&amp;quot;&amp;#039;`/,/^-- \(Current \)\?Database/ { /^-- \(Current \)\?Database/b ; /^\/\*\![0-9]\+.*=@OLD/be ; /^--$/d ; p }&amp;#039; \&lt;br /&gt;
      -e &amp;#039;:e /^\/\*\![0-9]\+.*=@OLD/,${ p }&amp;#039; &amp;quot;${EX_DFILE}&amp;quot; &lt;br /&gt;
else&lt;br /&gt;
  sed -n \&lt;br /&gt;
      -e &amp;#039;1,/^-- \(Current \)\?Database/{/^\(--.*\)*\?$/d ; p } ; /^-- \(Current \)\?Database: `&amp;#039;&amp;quot;${EX_SCHEMA}&amp;quot;&amp;#039;`/,/^-- \(Current \)\?Database/{ /^-- Table.*`&amp;#039;&amp;quot;${EX_TABLE}&amp;quot;&amp;#039;`/,/^-- \(Table\|Temporary\|\(Current \)\?Database\)/ { /^-- \(Table\|Temporary\|\(Current \)\?Database\)/b ; /^\/\*\![0-9]\+.*=@OLD/be ; /^--$/d ; p } }&amp;#039; \&lt;br /&gt;
      -e &amp;#039;:e /^\/\*\![0-9]\+.*=@OLD/,${ p }&amp;#039; &amp;quot;${EX_DFILE}&amp;quot; &lt;br /&gt;
fi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Il s&amp;#039;utilise ainsi:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# ./extract.sh -h&lt;br /&gt;
  Usage: [-h] -f &amp;lt;dump file&amp;gt; -d &amp;lt;database name&amp;gt; [-t &amp;lt;table&amp;gt;]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Par exemple:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# mysqldump -uroot --databases world &amp;gt; world.sql&lt;br /&gt;
# mysql -e &amp;quot;use world; show tables;&amp;quot; &lt;br /&gt;
+-----------------+&lt;br /&gt;
| Tables_in_world |&lt;br /&gt;
+-----------------+&lt;br /&gt;
| City            |&lt;br /&gt;
| Country         |&lt;br /&gt;
| CountryLanguage |&lt;br /&gt;
+-----------------+&lt;br /&gt;
&lt;br /&gt;
[root@gilles-p4 scripts]# ./extract.sh -f world.sql -d world -t City &amp;gt; City_world.sql&lt;br /&gt;
[root@gilles-p4 scripts]# cat City_world.sql&lt;br /&gt;
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;&lt;br /&gt;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;&lt;br /&gt;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;&lt;br /&gt;
/*!40101 SET NAMES utf8 */;&lt;br /&gt;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;&lt;br /&gt;
/*!40103 SET TIME_ZONE=&amp;#039;+00:00&amp;#039; */;&lt;br /&gt;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;&lt;br /&gt;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;&lt;br /&gt;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE=&amp;#039;NO_AUTO_VALUE_ON_ZERO&amp;#039; */;&lt;br /&gt;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;&lt;br /&gt;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;&lt;br /&gt;
&lt;br /&gt;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;&lt;br /&gt;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;&lt;br /&gt;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;&lt;br /&gt;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;&lt;br /&gt;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;&lt;br /&gt;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;&lt;br /&gt;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;&lt;br /&gt;
&lt;br /&gt;
-- Dump completed on 2013-02-28 15:47:45&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Source: [http://www.dbasquare.com/2012/04/21/improved-script-for-extracting-data-from-mysql-text-dumps/]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Extraction de toutes les tables contenues dans un dump texte SQL (mysql_splitdump.sh): ===&lt;br /&gt;
&lt;br /&gt;
Cet autre script permet de faire à peu prés la même chose que ci-dessus, sauf que celui permet d&amp;#039;exporter chacune des tables contenues dans une base.&lt;br /&gt;
On prend donc le dump d&amp;#039;une base puis on exécute le script pour obtenir chaque dump de chacune des tables.&lt;br /&gt;
&lt;br /&gt;
Voici le script:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;Bash&amp;quot;&amp;gt;&lt;br /&gt;
#!/bin/bash&lt;br /&gt;
&lt;br /&gt;
####&lt;br /&gt;
# Split MySQL dump SQL file into one file per table&lt;br /&gt;
# based on http://blog.tty.nl/2011/12/28/splitting-a-database-dump&lt;br /&gt;
####&lt;br /&gt;
&lt;br /&gt;
if [ $# -lt 1 ] ; then&lt;br /&gt;
  echo &amp;quot;USAGE $0 DUMP_FILE [TABLE]&amp;quot; &lt;br /&gt;
  exit&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
if [ $# -ge 2 ] ; then&lt;br /&gt;
  csplit -s -ftable $1 &amp;quot;/-- Table structure for table/&amp;quot; &amp;quot;%-- Table structure for table \`$2\`%&amp;quot; &amp;quot;/-- Table structure for table/&amp;quot; &amp;quot;%40103 SET TIME_ZONE=@OLD_TIME_ZONE%1&amp;quot; &lt;br /&gt;
else&lt;br /&gt;
  csplit -s -ftable $1 &amp;quot;/-- Table structure for table/&amp;quot; {*}&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
[ $? -eq 0 ] || exit&lt;br /&gt;
&lt;br /&gt;
mv table00 head&lt;br /&gt;
&lt;br /&gt;
FILE=`ls -1 table* | tail -n 1`&lt;br /&gt;
if [ $# -ge 2 ] ; then&lt;br /&gt;
  mv $FILE foot&lt;br /&gt;
else&lt;br /&gt;
  csplit -b &amp;#039;%d&amp;#039; -s -f$FILE $FILE &amp;quot;/40103 SET TIME_ZONE=@OLD_TIME_ZONE/&amp;quot; {*}&lt;br /&gt;
  mv ${FILE}1 foot&lt;br /&gt;
fi&lt;br /&gt;
&lt;br /&gt;
for FILE in `ls -1 table*`; do&lt;br /&gt;
  NAME=`head -n1 $FILE | cut -d$&amp;#039;\x60&amp;#039; -f2`&lt;br /&gt;
  cat head $FILE foot &amp;gt; &amp;quot;$NAME.sql&amp;quot; &lt;br /&gt;
done&lt;br /&gt;
&lt;br /&gt;
rm head foot table*&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Utilisation du script:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# ./mysql_splitdump.sh&lt;br /&gt;
USAGE ./mysql_splitdump.sh DUMP_FILE [TABLE]&lt;br /&gt;
&lt;br /&gt;
# mysqldump -uroot --databases world &amp;gt; world.sql&lt;br /&gt;
# mysql -e &amp;quot;use world; show tables;&amp;quot; &lt;br /&gt;
+-----------------+&lt;br /&gt;
| Tables_in_world |&lt;br /&gt;
+-----------------+&lt;br /&gt;
| City            |&lt;br /&gt;
| Country         |&lt;br /&gt;
| CountryLanguage |&lt;br /&gt;
+-----------------+&lt;br /&gt;
&lt;br /&gt;
# ./mysql_splitdump.sh world.sql&lt;br /&gt;
# ll *.sql&lt;br /&gt;
-rw-r--r-- 1 root root 179386 28 févr. 16:51 City.sql&lt;br /&gt;
-rw-r--r-- 1 root root  28468 28 févr. 16:51 CountryLanguage.sql&lt;br /&gt;
-rw-r--r-- 1 root root  39097 28 févr. 16:51 Country.sql&lt;br /&gt;
-rw-r--r-- 1 root root 244153 28 févr. 16:49 world.sql&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Source: [https://gist.github.com/jasny/1608062]&lt;/div&gt;</summary>
		<author><name>Didier</name></author>
	</entry>
</feed>