Subversion

Subversion is a version control system. Like other version control systems, Subversion (also known as svn) allows you to track changes to files over time, to make it easier to combine multiple authors’ changes to files and to maintain a record of “known good” revisions.

Send Email Notifications with a Post-Commit Hook

You can use Subversion’s post-commit hook to execute a script after a complete commit has taken place. In this example, a post-commit sends notification email after each commit to the Subversion repository.

To create a post-commit hook to send an email notification:

  1. Create the following script as a file named post-commit in the ~/webapps/svn_app/hooks directory such that:

    • from_addr is the address from which the notifications will be sent,
    • dest_addrN are the destination addresses for the notification email,
    • mailbox_name is the name of the WebFaction mailbox which will be used to send the email, and
    • mailbox_password is the password for the WebFaction mailbox which will be used to send the email.
    #!/usr/local/bin/python2.5
    
    # Settings
    mail_server = 'smtp.webfaction.com'
    from_email = 'from_addr'
    to_email = ['dest_addr1', 'dest_addr2']
    mailbox_name = 'mailbox_name'
    mailbox_password = 'mailbox_password'
    
    import os
    import smtplib
    import sys
    
    server = smtplib.SMTP(mail_server)
    server.login(mailbox_name, mailbox_password)
    
    repository, revision = sys.argv[1:]
    
    # Get the diff from svnlook
    cmd = "svnlook diff %(repository)s -r %(revision)s" % locals()
    diff = os.popen(cmd).read()
    
    msg = (
    "To: %(to_email)s\r\n"
    "From: %(from_email)s\r\n"
    "Subject: Subversion post-commit: r%(revision)s\r\n"
    "Content-type: text/plain\r\n"
    "\r\n"
    "Repository: %(repository)s\r\n"
    "Revision: %(revision)s\r\n"
    "Diff:\r\n"
    "%(diff)s\r\n"
    )
    
    msg = msg % locals()
    
    server.sendmail(from_email, to_email, msg)
    server.quit()
    
  2. Open an SSH session.

  3. Switch to the ~/webapps/svn_app/hooks directory.

  4. Enter chmod ogu+x post-commit and press Enter.

Now, every time a new change is committed to the repository, an email will be sent with a diff of the changes committed to the list of recipients.

Managing Users

Subversion users are managed with .htpasswd file in ~/webapps/svn, where svn is the name of the Subversion application.

To add a user or change an existing user’s password:

  1. Open an SSH session.

  2. Switch to the Subversion directory. Enter cd ~/webapps/svn and press Enter.

  3. Enter htpasswd .htpasswd username, where username is the user, and press Enter. A password prompt appears.

  4. Enter the new password and press Enter.

    See also

    See Strengthening Passwords for important information about choosing passwords.

  5. Reenter the new password and press Enter.

To delete a user:

  1. Open an SSH session.
  2. Switch to the Subversion directory. Enter cd ~/webapps/svn and press Enter.
  3. Enter htpasswd -D .htpasswd username, where username is the the user, and press Enter.

Reusing Usernames and Passwords Between Subversion and Trac

Subversion and Trac use ~/webapps/application/.htpasswd, where application is the name of the application, to store usernames and passwords for authorized users. To use the same usernames and passwords between the two applications, create a symlink from one .htpasswd file to the other:

  1. Open an SSH session to your account.
  2. Delete one of the application’s (here called the secondary application) .htpasswd files. Enter rm ~/webapps/secondary_application/.htpasswd and press Enter.
  3. Create a symlink from the primary application‘s .htpasswd file to the secondary application‘s .htpasswd location. Enter ln -s ~/webapps/primary_application/.htpasswd ~/webapps/secondary_applicaiton/.htpasswd.

Backing Up and Restoring Subversion Repositories

While WebFaction makes regular backups of your account, it’s advisable to create your own backups. You can use Subversion’s svnadmin tool to create and restore Subversion backups.

Creating a Complete Backup

To create a single, complete backup of your entire Subversion repository in its current state:

  1. Open an SSH session to your account.

  2. Switch to the directory of your Subversion application. Enter cd ~/webapps/svn, where svn is the name of the Subversion application as it appears in the control panel, and press Enter.

  3. Enter svnadmin dump . > dump_file, where dump_file is the path to the file in which to store the backup, and press Enter.

    Note

    To prevent your dump from being stopped for excessive processor utilization (particularly for repositories with many commits or a large total file size), prefix the svnadmin command with the nice command for deference to other processes:

    nice -n 19 svnadmin dump . > dump_file

Creating an Incremental Backup

Another popular way to back up Subversion repositories is to use an incremental backup. With an incremental backup, the repository can be backed up in such a way as to export only the changes made since a particular revision.

To create an incremental backup:

  1. Open an SSH session to your account.

  2. Switch to the directory of your Subversion application. Enter cd ~/webapps/svn, where svn is the name of the Subversion application as it appears in the control panel, and press Enter.

  3. Enter svnadmin dump --incremental --revision n . > dump_file, where n is the revision number from which to begin backing up and dump_file is the path to the file in which to store the backup, and press Enter.

    For example, to backup all of the repository activity following revision 100, enter svnadmin dump --incremental --revision 101 . > backup-101.dump and press Enter.

The most common uses for incremental backups are in a post-commit hook to create a new incremental backup after each commit or in a cron job to create new incremental backups on a schedule.

Restoring from Backups

Backups can be restored with the svnadmin‘s load command. To restore a backup:

  1. Open an SSH session to your account.

  2. Switch to the directory of a new Subversion application. Enter cd ~/webapps/svn, where svn is the name of the Subversion application as it appears in the control panel, and press Enter.

  3. Enter svnadmin load . < dump_file where dump_file is the path to the backup, and press Enter.

    If you are restoring incremental backups, begin with the oldest backup first and progress to the newest. For example, suppose there are three incremental backup files, r0to100.dump, r101to200.dump, and r201to300.dump; the backups would be restored like so:

    $ svnadmin load . < r0to100.dump
    $ svnadmin load . < r101to200.dump
    $ svnadmin load . < r201to300.dump
    

    Alternatively, restoration can be completed in one command by concatenating the dump files:

    svnadmin load . <(cat r0to100.dump r101to200.dump r201to300.dump)

    Note

    To prevent your restoration from being stopped for excessive processor utilization (particularly for repositories with many commits or a large total file size), prefix the svnadmin command with the nice command for deference to other processes:

    nice -n 19 svnadmin load . < dump_file