Server-Side Magazine

Backup Your SVN Repository with PHP

The process of backing up sensitive information became more and more important as software and hardware failures tend to happen more often with complex web applications.

If you’re never had any information loss, subversion repository corruption, etc. then consider yourself lucky. You don’t want to be in the shoes of a web developer that just lost a year of work, source code snippets, mockups, designs, proprietary code, etc.

If you don’t put your work in subversion repositories then it’s time to do it and let’s see how to write a PHP shell script to make the backup almost a transparent task.

Backup Meltdown
Copyright: John Boston

To backup a subversion repository a developer would go through several painful tasks, writing commands in the command prompt, copying the backup to another place (it’s silly to keep our backups on the same machine than our working repository), etc.

Also there are several methods to backup a Subversion repository. You can:

  • export the repository
  • make a hotcopy
  • dump the whole repository to a dump file

Although there are several Python scripts that wraps around the hotcopy method, I found that hotcopy is not as flexible than simply dumping the whole repository. By using this method we can even create incremental backups, meaning that we don’t have to dump the whole repository, but dump just a specific revision or range of revisions.

Backup from the command line

Subversion backup from the command line consists of several commands, I will illustrate the process by using the dump method from Terminal under Mac OS X.

Let’s open the Terminal (or command line window) and write the following command:

svnadmin dump [your repository location] > [repository destination file]
In my example it is:
svnadmin dump /Users/gyorgyfekete/svn_repo > /Users/gyorgyfekete/svn_backup.dump
The first argument [your repository location] is the directory where your repository is located. The second argument is the file where you want to dump your repository.

Also note the > between the arguments. This means that svnadmin should dump the repository into a file instead to output it in the Terminal window.

Open Terminal

This is it, from here you can copy this file to another location to save it in case of something horribly happens with your original repository.

If you want to reload your backup into your repository, first create a repository then use this command:

svnadmin load /path/to/repository < /path/to/svn_backup.dump

Note that the < (less than symbol) is used instead of > (greater than symbol).

Backup with the PHP shell script

Note: This script only works from command line and from UNIX environments. Following updates will make it possible to run in Windows too.

Now let’s see the fun part. How do we make all this automatic and transparent? We will create a PHP shell script to do all these commands for us and also to perform a little magic along the lines, for example to figure it out if the repository is already backed up, if so then use the incremental backup command and figure it out from which revision to start (the revision that is not backed up yet).

I will go through the code found in the PHP shell script file that is attached to this article. You can also download it from here.

Extract and open the PHP file with your favorite code editor.

On the first line you can see that we call this script from the command line using PHP CLI.

1
#!/usr/bin/php -q

The location /usr/bin/php is where PHP CLI is installed (you can modify it if your location is different). The option -q stands for quite mode.

17
18
19
20
21
22
23
//path to svnadmin	
$path['svnadmin'] = '/usr/local/bin/svnadmin';
 
//path to svnlook
$path['svnlook'] = '/usr/local/bin/svnlook';
 
$enable_compression = true;

You should set these variables if you have different paths for the commands svnadmin and svnlook. If you don’t know where these commands are installed then run the following commands from your command line:

1
2
locate svnadmin
locate svnlook

Locate Svnadmin

Next change the $enable_compression variable to false if you don’t want to compress your backup file (it is recommended to enable compression).

The syntax for the script is after you made it executable (with the chmod +x command):

./svnbackup.php /path/to/source_repository /destination/directory
Note: For the second argument you shouldn’t set a filename, only the destination directory, because the script will create the files automatically and name it by a specific pattern.

The script has several specific requirements that you should consider. All the dump files in the destination directory are noted as svnbackuprev[start revision]-[end revision]. The extension for full backup is .dump and for incremental backups .idump.

In each destination directory there is a svnbackup.nfo file (if the compression is disabled) which holds the last backup revision information. If you move this directory you should copy this file too.

For compression the script uses tar and gzip (included with almost every UNIX based distribution). If you enable compression then a file called svnbackup.tar.gz will be created in the destination directory which holds all the .dump and .idump files including the svnbackup.nfo file.

In order for the PHP script to work properly the files svnbackup.nfo and svnbackup.tar.gz names must remain as is.

Key features that this shell script has:

  • Automatic full backups or incremental backups
  • File compression. The incremental backups will be added to the compressed file too.
  • Destination directory can be any medium, e.g. USB flash memory
  • It can be hooked up to a cronjob process, making the backup truly transparent.

I hope you enjoyed this article and stay tuned.

Don’t forget that you too can contribute with articles, just let us know your proposed article title. You can contact us at: www.serversidemagazine.com/write-for-us/

Tags: , , ,

About the Author

Server-Side Magazine

Server-Side Magazine is a place where users can contribute articles. Strictly server-side posts are presented in the following programming languages: PHP, Ruby, ASP.Net, Java, Python

Our philosophy is that knowledge should be free and available to anyone. We designed this site to be an open platform, meaning that anyone can contribute and share their knowledge on the above areas.

Although, it's an open platform we don't accept all articles, because it would result in a "just another", mediocre website. A minimum standard of quality is required from every submitted article.

Comments

Leave a Comment

Your email address will not be published. Required fields are marked *

Markdown enabled. Click here to see the syntax.