svn-mirror.sh

  1. #!/bin/bash
  2.  
  3. # Set up some useful vars.
  4. FROMREPO="$1"
  5. TOREPO="$2"
  6.  
  7. if [ "$FROMREPO" = "" ];
  8. then
  9. echo "You must specify a 'from' repository (as a URL)."
  10. exit 1;
  11. fi
  12.  
  13. if [ "$TOREPO" = "" ];
  14. then
  15. echo "You must specify a 'to' directory."
  16. exit 1;
  17. fi
  18.  
  19. # Set up binary shortcuts.
  20. SVNADMIN=/usr/bin/svnadmin
  21. SVNSERVE=/usr/bin/svnserve
  22. SVNSYNC=/usr/bin/svnsync
  23. SVN_PORT=7860
  24. TOREPO_URL=svn://localhost:$SVN_PORT/
  25.  
  26. # Create an empty repository.
  27. "$SVNADMIN" "create" "$TOREPO"
  28.  
  29. # Set the pre-revprop-change hook.
  30. HOOK_PATH=$TOREPO/hooks/pre-revprop-change
  31. echo "#!/bin/bash" > "$HOOK_PATH"
  32. chmod 755 "$HOOK_PATH"
  33.  
  34. # Create a root user with full access.
  35. SVNSERVE_PASSWD=$TOREPO/conf/passwd
  36. echo "[users]" > "$SVNSERVE_PASSWD"
  37. echo "root = root" >> "$SVNSERVE_PASSWD"
  38.  
  39. # Allow authed users write access.
  40. SVNSERVE_CONF=$TOREPO/conf/svnserve.conf
  41. echo "[general]" > "$SVNSERVE_CONF"
  42. echo "anon-access = none" >> "$SVNSERVE_CONF"
  43. echo "auth-access = write" >> "$SVNSERVE_CONF"
  44. echo "password-db = passwd" >> "$SVNSERVE_CONF"
  45. echo "realm = $TOREPO" >> "$SVNSERVE_CONF"
  46.  
  47. # Launch svnserve as a background process.
  48. $SVNSERVE -d --foreground -r "$TOREPO" --listen-host localhost --listen-port "$SVN_PORT" &
  49.  
  50. # Preserve the PID.
  51. SVNSERVE_PID=$!
  52.  
  53. # Check the PID is sensible.
  54. if [ "$SVNSERVE_PID" = 0 ];
  55. then
  56. echo "Failed to start svnserve";
  57. exit 1;
  58. fi
  59.  
  60. # Output some info.
  61. echo "Started svnserve with PID $!";
  62.  
  63. # Create a var for our exit status.
  64. EXIT_STATUS=0
  65.  
  66. # Initialise the sync.
  67. "$SVNSYNC" init --sync-username root --sync-password root "$TOREPO_URL" "$FROMREPO"
  68.  
  69. if [ "$?" != 0 ];
  70. then
  71. echo "Initialising the sync failed." 1>&2;
  72. EXIT_STATUS=1;
  73. fi
  74.  
  75. if [ "$EXIT_STATUS" = 0 ];
  76. then
  77. # Start it off!
  78. "$SVNSYNC" sync "$TOREPO_URL";
  79.  
  80. if [ "$?" != 0 ];
  81. then
  82. echo "Sync failed." 1>&2;
  83. EXIT_STATUS=1;
  84. fi
  85. fi
  86.  
  87. # Kill the svnserve process.
  88. echo "Closing down svnserve mirror"
  89. kill -9 "$SVNSERVE_PID"
  90.  
  91. exit "$EXIT_STATUS"
  92.