{"id":357,"date":"2012-05-28T15:08:34","date_gmt":"2012-05-28T14:08:34","guid":{"rendered":"http:\/\/www.jbahillo.com\/?page_id=357"},"modified":"2013-02-21T19:26:20","modified_gmt":"2013-02-21T18:26:20","slug":"bash-mysql-backup-v-1-2","status":"publish","type":"page","link":"https:\/\/www.jbahillo.com\/?page_id=357","title":{"rendered":"[Bash] Mysql Backup v 1.2"},"content":{"rendered":"<p>Este script realiza copias de la base de datos indicada<\/p>\n<pre class=\"brush: bash; gutter: true; first-line: 1; highlight: []; html-script: false\">#!\/bin\/bash\n####################SECCION DE CONFIGURACION###################3\nDB_NAME=db_name\nDB_USER=db_user\nDB_PASS=db_pass\nDB_HOST=db_host\nADMIN_EMAIL= tu.direccion@de.correo.com\nDOMINIO=tu.dominio.com\nMAIL_DEBUG=1 \nBACKUP_DIR=\/ruta\/donde\/se\/almacenaran\/los\/backups\n############################################################\n\nLAST_FILE=`ls -1t $BACKUP_DIR | head -n1`\n\n#fichero temporal\nmysqldump --user=${DB_USER} --password=${DB_PASS} --host=${DB_HOST} ${DB_NAME}  | bzip2 -c &gt; \/tmp\/db-$(date +%Y%m%d).sql.bz2\n\nif [ &quot;$?&quot; -ne &quot;0&quot; ]; then\n        if [ $MAIL_DEBUG = 0 ]; then\n\t\techo -e &quot;\\nmysqldump failed!&quot;\n\t\texit 1\n\telse\n\t\techo &quot;Ha habido un problema al ejecutar el backup de la base de datos. El comando mysqldump ha fallado&quot; | mail -s &quot;Backup de $DOMINIO&quot; $ADMIN_EMAIL\n\n        \texit 1\n\tfi\nfi\n\ntar -cjf \/tmp\/db-$(date +%Y%m%d).tar.bz2 \/tmp\/db-$(date +%Y%m%d).sql.bz2\nif [ &quot;$?&quot; -ne &quot;0&quot; ]; then\n\tif [ $MAIL_DEBUG = 0 ]; then\t\n\t\techo -ne &quot;tarball creation failed!\\\\n&quot;\n       \t\texit 1\n\telse \n\t\techo &quot;Ha habido un problema al comprimir el backup de la base de datos. El comando tar ha fallado&quot; | mail -s &quot;Backup de $DOMINIO&quot; $ADMIN_EMAIL\n\tfi\nfi\n\nTEMP_FILE=\/tmp\/db-$(date +%Y%m%d).tar.bz2\n######################comprobacion de tama\u00f1o#######################################\n\nif [ `du -h $TEMP_FILE | cut -f1` == `du -h $BACKUP_DIR\/$LAST_FILE | cut -f1` ]; then \n\tif [ $MAIL_DEBUG = 0 ]; then\n\t\techo &quot; No se hace nada, no ha habido cambios&quot;\n\telse\n\t\techo &quot;No se ha completado el backup, ya que no ha habido cambios&quot; | mail -s &quot;Backup de $DOMINIO&quot; $ADMIN_EMAIL\n\tfi\t\nelse\n\tif [ $MAIL_DEBUG = 0 ]; then\n\t\techo &quot;Ha habido cambios, procedemos a almacenar el backup&quot;\n\tfi\t\nmv \/tmp\/db-$(date +%Y%m%d).tar.bz2 $BACKUP_DIR\necho &quot;&quot; | mutt -a  $BACKUP_DIR\/db-$(date +%Y%m%d).tar.bz2 -s &quot;Backup de $DOMINIO&quot; $ADMIN_EMAIL\nfi\n\ncontadortotal=0\nfor e in $( ls .\/ | nl | cut -f2 )\ndo\nlet contadortotal=$contadortotal+1\ndone\nlet contadortotal=$contadortotal-30\n\ncontadorparcial=0\n\nfor e in $( ls -tr .\/ | nl | cut -f2 )\ndo\nlet contadorparcial=$contadorparcial+1\nif [ $contadorparcial -lt $contadortotal ] ; then\nrm $e\nfi\ndone\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Este script realiza copias de la base de datos indicada #!\/bin\/bash ####################SECCION DE CONFIGURACION###################3 DB_NAME=db_name DB_USER=db_user DB_PASS=db_pass DB_HOST=db_host ADMIN_EMAIL= tu.direccion@de.correo.com DOMINIO=tu.dominio.com MAIL_DEBUG=1 BACKUP_DIR=\/ruta\/donde\/se\/almacenaran\/los\/backups ############################################################ LAST_FILE=`ls -1t $BACKUP_DIR | head -n1` #fichero temporal mysqldump &#8211;user=${DB_USER} &#8211;password=${DB_PASS} &#8211;host=${DB_HOST} ${DB_NAME} | bzip2 -c&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"https:\/\/www.jbahillo.com\/?page_id=357\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":21,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"ep_exclude_from_search":false,"footnotes":""},"class_list":["post-357","page","type-page","status-publish","hentry"],"aioseo_notices":[],"jetpack_shortlink":"https:\/\/wp.me\/P74T96-5L","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jbahillo.com\/index.php?rest_route=\/wp\/v2\/pages\/357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jbahillo.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.jbahillo.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.jbahillo.com\/index.php?rest_route=\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jbahillo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=357"}],"version-history":[{"count":8,"href":"https:\/\/www.jbahillo.com\/index.php?rest_route=\/wp\/v2\/pages\/357\/revisions"}],"predecessor-version":[{"id":564,"href":"https:\/\/www.jbahillo.com\/index.php?rest_route=\/wp\/v2\/pages\/357\/revisions\/564"}],"wp:attachment":[{"href":"https:\/\/www.jbahillo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}