#!/bin/bash


#--------------------------------------------------------------------#
# Backup of gstudio 
# File name    : adhock-research-quantitative-data-collection.sh
# File version : 1.0
# Created by   : Mr. Mrunal M. Nachankar
# Created on   : 20-11-2014 11:00:AM
# Modified by  : None
# Modified on  : Not yet
# Description  : This file is used for taking backup of gstudio quantitative data (gstudio-exported-users-analytics-csv) on ad-hock basis
#--------------------------------------------------------------------#

set -e

# platform name
platform="gstudio"

# fetch server_settings.py from gstudio container(/home/docker/code/gstudio/gnowsys-ndf/gnowsys_ndf/) to host system (/tmp/)
docker cp gstudio:/home/docker/code/gstudio/gnowsys-ndf/gnowsys_ndf/server_settings.py /tmp/

# get server id (Remove single quote {'} and Remove double quote {"})
ss_id=`echo  $(echo $(more /tmp/server_settings.py | grep -w GSTUDIO_INSTITUTE_ID | sed 's/.*=//g')) | sed "s/'//g" | sed 's/"//g'`

# get state code
state_code=${ss_id:0:2};

# copy csvs directory in /tmp/
rsync -avzPh /home/core/data/gstudio-exported-users-analytics-csvs /tmp/

# rename the csvs directory with gstudio id (like rj36)
mv /tmp/gstudio-exported-users-analytics-csvs /tmp/${ss_id}

# change directory to /tmp/
cd /tmp/

# create tar file of csvs directory (like rj36.tar.gz for rj36 directory name)
tar cvzf ${ss_id}.tar.gz ${ss_id}

# copy it in /mnt/ (pendrive or portable HDD)
rsync -avzPh ${ss_id}.tar.gz ${ss_id} /mnt/

# remove directory and tar.gz file from /tmp/
rm -rf /tmp/${ss_id}

exit 0