#!/bin/bash -e
#
# Authors:
#   Tommy Chang (chang177@purdue.edu)
#
# Version 2017-04-03
#
# Description:
#   A simple script to make a root partition tarball.
#
# Log:
#   2017-03-30 (Thu) -- Initially created 


set -e

if [[ ! $USER = "root" ]]; then
    echo "must be root"
    exit 1
fi
mkdir -p /home/OS_copy/rootDir/ || true

echo "copying / to /home/OS_copy/rootDir/"
rsync --info=progress2 -aHx / --exclude '/home/OS_copy/' \
      /home/OS_copy/rootDir/ 
sync;  # do rsync below again in case some file changed..
rsync --info=progress2 -aHx / --exclude '/home/OS_copy/' \
    /home/OS_copy/rootDir/
echo "modifying /home/OS_copy/rootDir/rootDir.tgz"
tar --transform 's/home\/OS_copy\/rootDir//' \
    --checkpoint=10000 -czf /home/OS_copy/rootDir.tgz /home/OS_copy/rootDir
rm -rf /home/OS_copy/rootDir/ 

echo ""
echo "Done!"
echo "rootDir.tgz is now ready at /home/OS_copy/rootDir.tgz"
echo ""

