~svn/ubuntu/oneiric/subversion/ppa

« back to all changes in this revision

Viewing changes to tools/backup/hot-backup.py.in

  • Committer: Max Bowsher
  • Date: 2012-06-27 12:25:12 UTC
  • mfrom: (44.1.46 precise)
  • Revision ID: _@maxb.eu-20120627122512-kmo8fj0lr7mlkppj
Make tree identical to precise branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
#                 Berkeley DB.
8
8
#
9
9
#  Subversion is a tool for revision control. 
10
 
#  See http://subversion.tigris.org for more information.
 
10
#  See http://subversion.apache.org for more information.
11
11
#    
12
12
# ====================================================================
13
 
# Copyright (c) 2000-2009 CollabNet.  All rights reserved.
14
 
#
15
 
# This software is licensed as described in the file COPYING, which
16
 
# you should have received as part of this distribution.  The terms
17
 
# are also available at http://subversion.tigris.org/license-1.html.
18
 
# If newer versions of this license are posted there, you may use a
19
 
# newer version instead, at your option.
20
 
#
21
 
# This software consists of voluntary contributions made by many
22
 
# individuals.  For exact contribution history, see the revision
23
 
# history and logs, available at http://subversion.tigris.org/.
 
13
#    Licensed to the Apache Software Foundation (ASF) under one
 
14
#    or more contributor license agreements.  See the NOTICE file
 
15
#    distributed with this work for additional information
 
16
#    regarding copyright ownership.  The ASF licenses this file
 
17
#    to you under the Apache License, Version 2.0 (the
 
18
#    "License"); you may not use this file except in compliance
 
19
#    with the License.  You may obtain a copy of the License at
 
20
#
 
21
#      http://www.apache.org/licenses/LICENSE-2.0
 
22
#
 
23
#    Unless required by applicable law or agreed to in writing,
 
24
#    software distributed under the License is distributed on an
 
25
#    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 
26
#    KIND, either express or implied.  See the License for the
 
27
#    specific language governing permissions and limitations
 
28
#    under the License.
24
29
# ====================================================================
25
30
 
26
 
# $HeadURL: http://svn.apache.org/repos/asf/subversion/branches/1.6.x/tools/backup/hot-backup.py.in $
27
 
# $LastChangedDate: 2009-01-25 06:09:04 +0000 (Sun, 25 Jan 2009) $
28
 
# $LastChangedBy: arfrever $
29
 
# $LastChangedRevision: 875528 $
 
31
# $HeadURL: http://svn.apache.org/repos/asf/subversion/branches/1.7.x/tools/backup/hot-backup.py.in $
 
32
# $LastChangedDate: 2010-08-20 04:30:52 +0000 (Fri, 20 Aug 2010) $
 
33
# $LastChangedBy: cmpilato $
 
34
# $LastChangedRevision: 987379 $
30
35
 
31
36
######################################################################
32
37
 
48
53
archive_map = {
49
54
  'gz'  : ".tar.gz",
50
55
  'bz2' : ".tar.bz2",
51
 
  'zip' : ".zip"
 
56
  'zip' : ".zip",
 
57
  'zip64' : ".zip"
52
58
  }
53
59
 
54
60
# Chmod recursively on a whole subtree
55
61
def chmod_tree(path, mode, mask):
56
 
  def visit(arg, dirname, names):
57
 
    mode, mask = arg
58
 
    for name in names:
59
 
      fullname = os.path.join(dirname, name)
 
62
  for dirpath, dirs, files in os.walk(path):
 
63
    for name in dirs + files:
 
64
      fullname = os.path.join(dirpath, name)
60
65
      if not os.path.islink(fullname):
61
66
        new_mode = (os.stat(fullname)[stat.ST_MODE] & ~mask) | mode
62
67
        os.chmod(fullname, new_mode)
63
 
  os.path.walk(path, visit, (mode, mask))
64
68
 
65
69
# For clearing away read-only directories
66
70
def safe_rmtree(dirname, retry=0):
97
101
 
98
102
Options:
99
103
  --archive-type=FMT Create an archive of the backup. FMT can be one of:
100
 
                       bz2 : Creates a bzip2 compressed tar file.
101
 
                       gz  : Creates a gzip compressed tar file.
102
 
                       zip : Creates a compressed zip file.
 
104
                       bz2  : Creates a bzip2 compressed tar file.
 
105
                       gz   : Creates a gzip compressed tar file.
 
106
                       zip  : Creates a compressed zip file.
 
107
                       zip64: Creates a zip64 file (can be > 2GB).
103
108
  --num-backups=N    Number of prior backups to keep around (0 to keep all).
 
109
  --verify           Verify the backup.
104
110
  --help      -h     Print this help message and exit.
105
111
 
106
112
""" % (scriptname,))
109
115
try:
110
116
  opts, args = getopt.gnu_getopt(sys.argv[1:], "h?", ["archive-type=",
111
117
                                                      "num-backups=",
 
118
                                                      "verify",
112
119
                                                      "help"])
113
120
except getopt.GetoptError, e:
114
121
  sys.stderr.write("ERROR: %s\n\n" % e)
117
124
  sys.exit(2)
118
125
 
119
126
archive_type = None
 
127
verify_copy = False
120
128
 
121
129
for o, a in opts:
122
130
  if o == "--archive-type":
123
131
    archive_type = a
124
132
  elif o == "--num-backups":
125
133
    num_backups = int(a)
 
134
  elif o == "--verify":
 
135
    verify_copy = True
126
136
  elif o in ("-h", "--help", "-?"):
127
137
    usage()
128
138
    sys.exit()
261
271
else:
262
272
  print("Done.")
263
273
 
 
274
### Step 4: Verify the hotcopy
 
275
if verify_copy:
 
276
  print("Verifying backup...")
 
277
  err_code = subprocess.call([svnadmin, "verify", "--quiet", backup_subdir])
 
278
  if err_code != 0:
 
279
    sys.stderr.write("Backup verification failed.\n")
 
280
    sys.stderr.flush()
 
281
    sys.exit(err_code)
 
282
  else:
 
283
    print("Done.")
264
284
 
265
 
### Step 4: Make an archive of the backup if required.
 
285
### Step 5: Make an archive of the backup if required.
266
286
if archive_type:
267
287
  archive_path = backup_subdir + archive_map[archive_type]
268
288
  err_msg = ""
281
301
      err_msg = "Tar failed: " + str(e)
282
302
      err_code = -3
283
303
 
284
 
  elif archive_type == 'zip':
 
304
  elif archive_type == 'zip' or archive_type == 'zip64':
285
305
    try:
286
306
      import zipfile
287
307
      
288
 
      def add_to_zip(baton, dirname, names):
289
 
        zp = baton[0]
290
 
        root = os.path.join(baton[1], '')
 
308
      def add_to_zip(zp, root, dirname, names):
 
309
        root = os.path.join(root, '')
291
310
        
292
311
        for file in names:
293
312
          path = os.path.join(dirname, file)
294
313
          if os.path.isfile(path):
295
314
            zp.write(path, path[len(root):])
296
315
          elif os.path.isdir(path) and os.path.islink(path):
297
 
            os.path.walk(path, add_to_zip, (zp, path))
 
316
            for dirpath, dirs, files in os.walk(path):
 
317
              add_to_zip(zp, path, dirpath, dirs + files)
298
318
            
299
 
      zp = zipfile.ZipFile(archive_path, 'w', zipfile.ZIP_DEFLATED)
300
 
      os.path.walk(backup_subdir, add_to_zip, (zp, backup_dir))
 
319
      zp = zipfile.ZipFile(archive_path, 'w', zipfile.ZIP_DEFLATED, archive_type == 'zip64')
 
320
      for dirpath, dirs, files in os.walk(backup_subdir):
 
321
        add_to_zip(zp, backup_dir, dirpath, dirs + files)
301
322
      zp.close()
302
323
    except ImportError, e:
303
324
      err_msg = "Import failed: " + str(e)
315
336
    print("Archive created, removing backup '" + backup_subdir + "'...")
316
337
    safe_rmtree(backup_subdir, 1)
317
338
 
318
 
### Step 5: finally, remove all repository backups other than the last
 
339
### Step 6: finally, remove all repository backups other than the last
319
340
###         NUM_BACKUPS.
320
341
 
321
342
if num_backups > 0: