Introduction to SubversionPresented by David Brownlee BELUG March 13th, 2007 Software Configuration Management uses tools and procedures to reproduce software in a consistent state at different points in time. A key tool for doing this is a version control system. Version control systems can also be used outside of software development. You might want to use a version control system to track changes in system configuration files or an experimental chili recipe. Subversion is a version control system. It allows you to track changes to and keep multiple versions of files and directories. In Software Configuration Management (SCM) terminology, a collection of one or more files and directories at a point in time is a configuration. You save your configurations in a central repository which is like a database. When you want to make a change to a configuration, you must check it out of the repository. Checking out a configuration creates a copy of the configuration's files and directories in your workspace. The workspace is a directory where you make all your changes. When you're finished making changes, you check the new files back into the repository. Subversion allows multiple people to work on the same files either locally or over a TCP/IP network. This is a (very) brief introduction to installing and using Subversion and I only cover a single user accessing a local (on the same machine) repository. The Subversion web site is http://subversion.tigris.org/. The documentation can be found at http://svnbook.red-bean.com/. Installing SubversionInstall Subversion as you would any other package on your Linux distribution. Here is an example using yum on CentOS Linux: $ sudo yum install subversion [ ...snip... ] Installed: subversion.i386 0:1.1.4-2.ent Configuring SubversionFirst, you need to configure the repository to store your file revisions. In this case, I'm putting the repository in my home directory. If you are setting up a repository for multiple users, you might want to put the repository in /var/svn or a similar location. You will also want to create an svn group and set filesystem permissions on the repository to control access. I'm also specifying that the fsfs (as opposed to the bdb) backend storage be used. You can read about the the differences in the Subversion documentation. $ svnadmin create –fs-type fsfs /home/davidb/svn This creates the repository in /home/davidb/svn. You will NOT edit the files in the repository directly. All configurations in the repository share the same revision number (but not every file is changed every revision). You may want to have multiple repositories or organize all you projects in a single repository. Again, reading the Subversion documentation will help you plan for a larger installation than presented here. Importing A Project Into The RepositoryVersion control systems are commonly used to store software projects. However, you can use Subversion for managing changes to other text files such as host configuration files or chili recipes. In our example, we will track changes to our Linux firewall configuration. First, make sure your EDITOR environment variable is set to your editor of choice: $ export EDITOR=vim Then, we make a temporary directory in our home directory and collect the files: $ mkdir -p
/home/davidb/tmp/trunk/etc/sysconfig [ edit your file ] $ cd ../../.. The trunk, tags, and branches directories are a convention explaned in the Subversion documentNow, import your project into the repository using the file URL notation: $ svn import -m "Initial Import"
file:///home/davidb/svn/firewall Do NOT delete your tmp directory yet! You must first verify that you can check out your work. Create Your WorkspaceNow you need to create your workspace, the directory where you will edit files: $ mkdir /home/davidb/workspace Since the checkout worked, you can delete the temporary directory: $ cd /home/davidb Editing FilesYou can edit the files in your workspace normally. After you are satisfied with your changes, check your changes back into the repository: $ cd /home/davidb/workspace/firewall/etc/sysconfig [ make changes ] svn commit iptables When you commit a change, the editor is started so you can add a note about what you have changed: Another change Adding New FilesTo add a file, create or copy the file in your workspace, mark the file for addition to the repository, and then commit the file to the repository: $ cd /home/davidb/workspace/firewall/etc Again, the editor will start so you can leave a note about what you did. Showing HistoryTo see how the files in your repository have changed, you can list their history: $ svn log iptables Check The State Of Your WorkspaceSo you had fun playing with Subversion a couple weeks ago before life interrupted. Now you have some free time again but cannot remember which files in your workspace have been changed or still need to be added to the repository. Use svn status $ svn status The 'M' shows that the iptables file has been modified since you last checked it out. The '?' shows that Subversion doesn't know anything about the named file in your workspace. Either delete named or use svn add to add it to the repository. See What Has ChangedSo you made edits to the iptables file but cannot remember what you did. Use svn diff to see the differences between what is in your workspace and what is in the repository. $ svn diff iptables [ diff output ] Recover Your RepositoryYour backups are not finished until you have proved you can recover from them. Backup your repository using the svnadmin tool: $ svnadmin dump /home/davidb/svn > svn-backup-r17 Using svnadmin dump guarantees your backups are in a consistent state even if other users are checking files into the repository while your backing it up. The dump command also has –incremental and –revision options for making incremental backups. Now create a new, empty repository and restore the repository using the dumpfile you created in the step above and the svnadmin load command: $ svnadmin create –fs-type fsfs /home/davidb/svn-recovery-test End NoteThis barely scratches the surface of what Subversion can do. In particular, you will want to read about update and diff since you will use them often. Read the documentation and experiment. Also, you can ask Subversion for help on the command line: $ svn help [ and more... ] |