<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>random utterings...: Tag database</title>
    <link>http://blog.randomutterings.com/articles/tag/database</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Mysql Backup Script</title>
      <description>&lt;p&gt;Need a way to backup all your mysql databases in seperate files without eating up your hard drive, then this is the script you need.&lt;/p&gt;

&lt;p&gt;Its a bash shell script that will export all your databases with mysqldump into seperate files named like 'database-date.sql'.  It will also delete your old backups so you don't fill up your hard drive.&lt;/p&gt;

&lt;blockquote&gt;
&lt;pre&gt;
#!/bin/bash

DBHOST='localhost'
DBUSER='root'
DBPASSWD='password'
LOCALDIR=/path/to/your/backup/directory/

# Using the date function's --date="last week" to delete backups that are 7 days old 
OLDSUFFIX=`date --date="last week" +%Y%m%d`
SUFFIX=`date +%Y%m%d`
DBS=`mysql -u$DBUSER -p$DBPASSWD -h$DBHOST -e"show databases"`
su_cmd=""
for DATABASE in $DBS; do
  if [ $DATABASE != "Database" ]; then
    OLDFILENAME=$OLDSUFFIX-$DATABASE.sql
    FILENAME=$SUFFIX-$DATABASE.sql
    su_cmd="${su_cmd} rm -f $LOCALDIR$OLDFILENAME;"
    mysqldump -u$DBUSER -p$DBPASSWD -h$DBHOST $DATABASE &gt; $LOCALDIR$FILENAME
  fi
done
su -c "${su_cmd}"
echo "done";
&lt;/pre&gt;
&lt;/blockquote&gt;</description>
      <pubDate>Thu, 06 Sep 2007 17:06:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:1062786c-44cc-4201-bd8d-5ae5f1916ffb</guid>
      <author>Chris Barnes</author>
      <link>http://blog.randomutterings.com/articles/2007/09/06/mysql-backup-script</link>
      <category>Infrastructure</category>
      <category>mysql</category>
      <category>database</category>
      <category>backup</category>
      <category>bash</category>
      <trackback:ping>http://blog.randomutterings.com/articles/trackback/21</trackback:ping>
    </item>
  </channel>
</rss>
