CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/scripts/restore_project.py
Views: 687
1
#!/usr/bin/env python
2
###############################################################################
3
#
4
# CoCalc: Collaborative Calculation
5
#
6
# Copyright (C) 2016, Sagemath Inc.
7
#
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
12
#
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
17
#
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
#
21
###############################################################################
22
23
import argparse, json, os, shutil, sys
24
25
parser = argparse.ArgumentParser(description="Restore project from a backup.")
26
parser.add_argument(
27
"--project_id",
28
dest="project_id",
29
type=str,
30
help="project_id",
31
required=True)
32
parser.add_argument(
33
"--username", dest='username', type=str, help="username", required=True)
34
parser.add_argument(
35
"--host", dest='host', type=str, help='host', required=True)
36
parser.add_argument('--path', dest='path', type=str, help='path', default='.')
37
parser.add_argument('--port', dest='port', type=str, help='port', default='22')
38
args = parser.parse_args()
39
40
SALVUS_ROOT = os.environ['SALVUS_ROOT']
41
BACKUP = os.path.join(SALVUS_ROOT, 'data', 'backup')
42
43
project_id = args.project_id
44
#print "project_id =", project_id
45
46
username = args.username
47
#print "username =", username
48
host = args.host
49
#print "host =", host
50
path = args.path
51
#print "path =", path
52
port = args.port
53
54
#print "port =", port
55
56
57
def bup(cmd):
58
s = "bup " + cmd
59
print s
60
exit_code = os.system(s)
61
print "exit_code =", exit_code
62
if exit_code:
63
raise RuntimeError
64
65
66
restore_path = os.path.join(BACKUP, 'restore', project_id)
67
68
try:
69
try:
70
os.makedirs(restore_path)
71
except Exception, mesg:
72
print mesg # non-fatal -- delete and try again
73
shutil.rmtree(restore_path, ignore_errors=True)
74
os.makedirs(restore_path)
75
76
bup("restore --outdir '%s' %s/latest/." % (restore_path, project_id))
77
cmd = "rsync -axH '%s/' %s@%s:%s" % (restore_path, username, host, path)
78
print cmd
79
os.system(cmd)
80
81
finally:
82
print "Deleting '%s'..." % restore_path
83
shutil.rmtree(restore_path, ignore_errors=True)
84
85