Backup strategy and script of MySQL and versioning tool for dev machine

We often think about automating the backups of your production server but you easily forget your development machine. It may not be very important to back up databases but it is not the same for svn and git repositories. If your production server can crash, so can your server or development PC.

Until now, I've been wrongly using the same backup strategy for my prod server and dev server. Why is it wrong? Because making a daily backup at a fixed time is not a good idea when you don't have to do it every day and especially when you don't let your machine on 24 hours a day. Even if my eeebox only needs 20W, over the year it would be a nice useless consumption to leave it on all the time.
So I reviewed my strategy and my scripts. Not being able to determine in advance what time my dev server will be turned on, I start the backup every hour. And of course in order not to redo a backup already made, I added a test if the destination file exists or not.

Backing up MySQL databases


<a>#!/bin/bash
 
#date du jour
DATE=`date +%y_%m_%d`
 
#liste des bases
LISTEBDD=$( echo 'show databases' | mysql -uroot -pxxxx)
 
for SQL in $LISTEBDD
 
do
 
  if [ $SQL != "information_schema" ] && [ $SQL != "mysql" ] && [ $SQL != "Database" ]; then
 
    if [ -e /home/backup/sql/$SQL"_mysql_"$DATE.sql.gz ]; then
 
      #exit
 
    else
 
      mysqldump -uroot -pxxxx $SQL | gzip > /home/backup/sql/$SQL"_mysql_"$DATE.sql.gz
 
    fi
 
  fi
 
done
</a>

I added the test for each of the databases, to handle the case where I shut down the server before all the databases are backed up. If you are not interested in this option just remove the # in front of the “exit” command.
The list of databases is obtained by the “show database” query specific to MySQL.

Saving the SVN repository

<a>#!/bin/bash
 
#date du jour
DATE=`date +%y_%m_%d`
 
#liste des dossier
LISTEDIR=$(ls  /var/svn/)
 
#on boucle sur chaque dossier
for DOSSIER in $LISTEDIR
do
  if [-e /home/backup/svn/$DOSSIER"_svn_"$DATE.gz ]; then
    #exit
  else
    svnadmin dump /var/svn/$DOSSIER | gzip  > $DOSSIER"_svn_"$DATE.gz && mv $DOSSIER"_svn_"$DATE.gz /home/backup/svn
  fi
done</a>

I built this script on the same model as the one for databases. The list of repositories is obtained by listing the contents of the “/var/svn” folder.

All that remains is to add these 2 scripts to the crontab list with an execution every hour or other depending on your use.

<a> > crontab -e
</a>

Then we add the lines:

<a>0 * * * * sh /root/backup_mysql.sh
0 * * * * sh /root/backup_svn.sh</a>


You could also start the scripts when starting or shutting down the machine. And of course you would have to store the files on another machine, an external drive or ideally a NAS.

Add a comment