Skip to content

Recovery Script

Simon Urbanek edited this page Jul 20, 2018 · 2 revisions

This script is useful if metadata is lost and needs to be recovered. The description and fork information is lost and will not be recovered. All notebooks will have the description 00 Recovered.

for path in `ls -l ?/?/?/*/gist.json | awk '$5 == 0 {print $9}'`; do
    id=`echo $path | sed s:/gist.json:: | tr -d '/'`
    echo Recovering $id ...
    if [ -z "$id" ]; then
	echo ERROR: invalid id >&2
    else
	rm -rf "/tmp/$id"
	repo=`echo $path | sed s:/gist.json:/repo:`
	## clone repo
	git clone $repo "/tmp/$id"

	## find last commit
	t1=`(cd "/tmp/$id"; TZ=UTC git log -n1 --format='%at')`
	t1=`TZ=GMT perl -e "use POSIX qw(strftime); print strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($t1));"`
	
	## root commit
	c0=`(cd "/tmp/$id";git rev-list --max-parents=0 HEAD)`

	## get login and email
	login=`(cd "/tmp/$id"; TZ=UTC git log $c0 --format='%an')`
	email=`(cd "/tmp/$id"; TZ=UTC git log $c0 --format='%ae')`

	## date of fist commit
	t0=`(cd "/tmp/$id"; TZ=UTC git log $c0 --format='%at')`
	t0=`TZ=GMT perl -e "use POSIX qw(strftime); print strftime('%Y-%m-%dT%H:%M:%SZ', gmtime($t0));"`

	## gist service will only record name, GitHub migrated notebooks will have email
	name=`echo $email | sed s':@.*::'`
	if [ -z "$name" ]; then
	    name="$login"
	fi

	## NOTE: fork information is lost... we don't attempt to recover it
	echo '{"id":"'"$id"'","owner":"'"$name"'","description":"00 Recovered","public":false,"created_at":"'"$t0"'","updated_at":"'"$t1"'","forks":[]}'
	echo $c0 > `echo $path | sed s:/gist.json:/recovered:`
	echo '{"id":"'"$id"'","owner":"'"$name"'","description":"00 Recovered","public":false,"created_at":"'"$t0"'","updated_at":"'"$t1"'","forks":[]}' > "$path"
    fi
done
Clone this wiki locally