----- SNIP ------
#!/bin/bash
TIMESTAMP=`date +%Y%m%d`
#Disable AUTOCOMMIT and FOREIGN_KEY_CHECKS for restore operation to make it faster, COMMIT
and KEY_CHECKS will be done once
everything was imported
echo "SET AUTOCOMMIT = 0; SET FOREIGN_KEY_CHECKS=0;" >
/tmp/oncoweb-backup-${TIMESTAMP}.sql
# resulting sql will have DROP DATABASE in front of each CREATE DATABASE
# same for DROP/CREATE TABLE
# During the dump, we use single-transaction and quick to make the whole operation faster
and non-locking
# we FLUSH LOGS and restart a new log file after backup
# user mysql_backup needs SELECT, RELOAD and LOCK TABLES rights
mysqldump --add-drop-database --add-drop-table --single-transaction --flush-logs --quick
-u mysql_backup --password="bckup" oncoweb
>> /tmp/oncoweb-backup-${TIMESTAMP}.sql
#Enable FOREIGN_KEY_CHECKS and COMMIT, enable AUTOCOMMIT afterwards
echo "SET FOREIGN_KEY_CHECKS = 1; COMMIT; SET AUTOCOMMIT = 1;" >>
/tmp/oncoweb-backup-${TIMESTAMP}.sql
#Compress backup but nice the operation so that it doesn't take all the cpu
pushd /tmp
nice -n 10 bzip2 oncoweb-backup-${TIMESTAMP}.sql
popd
#We're done!
----- SNIP ------
Il serait possible d'éviter de créer un fichier temporaire en faisant le suivant:
----- SNIP ------
(echo "SET AUTOCOMMIT = 0; SET FOREIGN_KEY_CHECKS=0;"; mysqldump --add-drop-database
--add-drop-table --single-transaction
--flush-logs --quick -u mysql_backup --password="bckup" foo; echo "SET FOREIGN_KEY_CHECKS
= 1; COMMIT; SET AUTOCOMMIT = 1;") |
bzip2 -c > /tmp/oncoweb-backup-${TIMESTAMP}.sql.bz2
----- SNIP ------
mais la compression n'est pas nicé, donc pourrait prendre tout le cpu, ce qui est à
éviter sur un serveur.
Il faudrait rajouter du code qui vérifie si tout s'est bien passé et qui envoie un email
si qqchose ne fonctionne pas.
--
Stefan Michalowski
Email: mitchptaff.ca
PGP Key: http://screamerone.zapto.org/k.asc