0
|
1 #!/usr/bin/env python
|
|
2
|
|
3 # The MIT License
|
|
4 #
|
|
5 # Copyright (c) 2010 Louis Opter <kalessin@kalessin.fr>
|
|
6 #
|
|
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8 # of this software and associated documentation files (the "Software"), to deal
|
|
9 # in the Software without restriction, including without limitation the rights
|
|
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11 # copies of the Software, and to permit persons to whom the Software is
|
|
12 # furnished to do so, subject to the following conditions:
|
|
13 #
|
|
14 # The above copyright notice and this permission notice shall be included in
|
|
15 # all copies or substantial portions of the Software.
|
|
16 #
|
|
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
23 # THE SOFTWARE.
|
|
24
|
|
25 import errno
|
|
26 import os
|
|
27 import shutil
|
|
28 import subprocess
|
|
29 import sys
|
|
30 import tarfile
|
|
31 import tempfile
|
|
32
|
|
33 class TempDir(object):
|
|
34 """Wrap tempfile.mkdtemp to use the with statement"""
|
|
35
|
|
36 def __init__(self):
|
|
37 pass
|
|
38
|
|
39 def __enter__(self):
|
|
40 self.__path = tempfile.mkdtemp()
|
|
41 return self.__path
|
|
42
|
|
43 def __exit__(self, exc_type, exc_value, traceback):
|
|
44 shutil.rmtree(self.__path)
|
|
45
|
|
46 def status(message):
|
|
47 print '%s: %s.' % (os.path.basename(sys.argv[0]), message)
|
|
48
|
|
49 def die(message):
|
|
50 status(message)
|
|
51 status('a log has been recorded in %s' % log_path)
|
|
52 sys.exit(1)
|
|
53
|
|
54 def hotcopy_repo():
|
|
55 """Use trac-admin to hotcopy the trac environnement into `root'"""
|
|
56
|
|
57 dest = os.path.join(root, name)
|
|
58 if subprocess.call(['sudo', 'trac-admin', trac_env_root, 'hotcopy', dest],
|
|
59 stdout=log, stderr=log) != 0:
|
|
60 die('error while copying the Trac environnement into a temporary directory')
|
|
61
|
|
62 subprocess.call(['sudo', 'chown', '-R', 'ubuntu:ubuntu', dest])
|
|
63
|
|
64 status('Trac environnement copied')
|
|
65
|
|
66 def archive_repo():
|
|
67 """Create a tar bzipped archive of the trac environnement"""
|
|
68
|
|
69 try:
|
|
70 archive = tarfile.open(os.path.join(root, archive_name), 'w:bz2')
|
|
71 archive.add(os.path.join(root, name), 'backup-tracenv-%s' % name)
|
|
72 archive.close()
|
|
73 except tarfile.TarError, ex:
|
|
74 die('Can\'t create the tar archive %s: %s' % (archive_name, str(ex)))
|
|
75
|
|
76 status('Archive %s created' % archive_name)
|
|
77
|
|
78 def send_repo():
|
|
79 """Send the tar to archive using scp"""
|
|
80
|
|
81 dest = sys.argv[2]
|
|
82 archive = os.path.join(root, archive_name)
|
|
83 if subprocess.call(['scp', archive, dest]) != 0:
|
|
84 die('error while scpying %s to %s' % (archive_name, dest))
|
|
85
|
|
86 status('Archive %s copied to "%s"' % (archive_name, dest))
|
|
87
|
|
88 if __name__ == '__main__':
|
|
89 if len(sys.argv) != 3 or sys.argv[1] == '--help':
|
|
90 print 'Usage: %s project-name [[user@]host:]path' % os.path.basename(sys.argv[0])
|
|
91 sys.exit(0)
|
|
92
|
|
93 # globals
|
|
94 name = sys.argv[1]
|
|
95 archive_name = 'backup-tracenv-%s.tar.bz2' % name
|
|
96 log_path = os.path.join(os.getenv('TMP') or '/tmp/', name + '.log')
|
|
97 trac_env_root = os.path.join('/var/trac/envs/', name)
|
|
98
|
|
99 if not os.path.exists(trac_env_root):
|
|
100 status('%s: %s' % (name, os.strerror(errno.ENOENT)))
|
|
101 sys.exit(1)
|
|
102
|
|
103 with open(log_path, 'w') as log:
|
|
104 with TempDir() as root:
|
|
105 status('root=%s' % root)
|
|
106 hotcopy_repo()
|
|
107 archive_repo()
|
|
108 send_repo()
|
|
109 os.remove(log_path)
|
|
110
|
|
111 sys.exit(0)
|