Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/scripts/restore_project.py
Views: 687
#!/usr/bin/env python1###############################################################################2#3# CoCalc: Collaborative Calculation4#5# Copyright (C) 2016, Sagemath Inc.6#7# This program is free software: you can redistribute it and/or modify8# it under the terms of the GNU General Public License as published by9# the Free Software Foundation, either version 3 of the License, or10# (at your option) any later version.11#12# This program is distributed in the hope that it will be useful,13# but WITHOUT ANY WARRANTY; without even the implied warranty of14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15# GNU General Public License for more details.16#17# You should have received a copy of the GNU General Public License18# along with this program. If not, see <http://www.gnu.org/licenses/>.19#20###############################################################################2122import argparse, json, os, shutil, sys2324parser = argparse.ArgumentParser(description="Restore project from a backup.")25parser.add_argument(26"--project_id",27dest="project_id",28type=str,29help="project_id",30required=True)31parser.add_argument(32"--username", dest='username', type=str, help="username", required=True)33parser.add_argument(34"--host", dest='host', type=str, help='host', required=True)35parser.add_argument('--path', dest='path', type=str, help='path', default='.')36parser.add_argument('--port', dest='port', type=str, help='port', default='22')37args = parser.parse_args()3839SALVUS_ROOT = os.environ['SALVUS_ROOT']40BACKUP = os.path.join(SALVUS_ROOT, 'data', 'backup')4142project_id = args.project_id43#print "project_id =", project_id4445username = args.username46#print "username =", username47host = args.host48#print "host =", host49path = args.path50#print "path =", path51port = args.port5253#print "port =", port545556def bup(cmd):57s = "bup " + cmd58print s59exit_code = os.system(s)60print "exit_code =", exit_code61if exit_code:62raise RuntimeError636465restore_path = os.path.join(BACKUP, 'restore', project_id)6667try:68try:69os.makedirs(restore_path)70except Exception, mesg:71print mesg # non-fatal -- delete and try again72shutil.rmtree(restore_path, ignore_errors=True)73os.makedirs(restore_path)7475bup("restore --outdir '%s' %s/latest/." % (restore_path, project_id))76cmd = "rsync -axH '%s/' %s@%s:%s" % (restore_path, username, host, path)77print cmd78os.system(cmd)7980finally:81print "Deleting '%s'..." % restore_path82shutil.rmtree(restore_path, ignore_errors=True)838485