You have a lot of duplicate code. In such cases (in any language) you need to refactor your code.
Extract the duplicate parts into a separate procedure, passing all the necessary parameters and redesigning the main procedure to (re) use the extracted code.
DELIMITER $$ CREATE PROCEDURE DO_BACKUP(table_name text) BEGIN SET @sql = CONCAT("SELECT * FROM ", table_name, " INTO OUTFILE '/home/aimad/GestionStock/" , DATE_FORMAT( NOW(), '%Y%m%d') , table_name, ".csv'" ); PREPARE s1 FROM @sql; EXECUTE s1; DEALLOCATE PREPARE s1; END$$ CREATE DEFINER=`root`@`localhost` EVENT `Backup` ON SCHEDULE EVERY 1 WEEK STARTS '2013-06-14 18:19:02' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN CALL DO_BACKUP('BonInterne'); CALL DO_BACKUP('LigneBonInterne'); CALL DO_BACKUP('BonExterne'); CALL DO_BACKUP('LigneBonExterne'); CALL DO_BACKUP('BonEntrée'); CALL DO_BACKUP('LigneBonEntrée'); CALL DO_BACKUP('Inventaire'); CALL DO_BACKUP('LigneInterventaire'); END $$ DELIMITER ;
It will also make it easier to read and maintain your code. See how easy it would be to add another table.
After such a reorganization, you can easily manage this path of values stored in a table. Especially if you changed the procedure to a function:
CREATE FUNCTION DO_BACKUP(table_name text) RETURNS text BEGIN
then create and fill in the table (once):
CREATE TABLE backup_table ( table_name text); INSERT INTO backup_table values ('BonInterne'), ('LigneBonInterne'), etc;
then you can just do this:
select do_backup(table_name) from table_name;
Adding and removing tables from your backup will be as simple as inserting and deleting rows from a table.
source share