Skip to content

Instantly share code, notes, and snippets.

@dstreev
Last active April 15, 2018 13:18
Show Gist options
  • Save dstreev/0906abb61d11e3efbfba9f7193b77950 to your computer and use it in GitHub Desktop.
Save dstreev/0906abb61d11e3efbfba9f7193b77950 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Expecting 2 parameters.
# - Snapshoted Directory
# - Snapshots older then x days.
while [ $# -gt 0 ]; do
case "$1" in
--snapshot-dir)
shift
SNAPSHOT_DIR=$1
shift
;;
--older-than)
shift
OLDER_THAN=$1
shift
;;
*)
break
;;
esac
done
if [ "${SNAPSHOT_DIR}" == "" ]; then
echo "Need to supply snapshot'ED' directory."
echo "Usage: $0 --snapshot-dir <dir> --older-than <in-days>"
exit -1
fi
if [ "${OLDER_THAN}" == "" ]; then
echo "Need to supply older than days."
echo "Usage: $0 --snapshot-dir <dir> --older-than <in-days>"
exit -1
fi
declare -i NOW
NOW=`date +'%s'`*1000
declare -i OLDER_THAN_EPOCH
OLDER_THAN_EPOCH=${NOW}-${OLDER_THAN}*86400*1000
echo "Snapshot Directory: ${SNAPSHOT_DIR}"
echo "Older than (epoch in ms): ${OLDER_THAN_EPOCH}"
for i in `hdfs dfs -stat "%Y,%n" ${SNAPSHOT_DIR}/.snapshot/*`;do
# Check if Snapshot is older than ...
CUR_FILE_EPOCH=`echo "${i}" | cut -d "," -f 1`
if [ ${CUR_FILE_EPOCH} \< ${OLDER_THAN_EPOCH} ]; then
CUR_SNAPSHOT=`echo "${i}" | cut -d "," -f 2`
echo "OLDER(epoch): ${CUR_FILE_EPOCH} vs ${OLDER_THAN_EPOCH} Snapshot:(dir) ${SNAPSHOT_DIR} - (snapshot)${CUR_SNAPSHOT}"
echo "Removing snapshot: ${CUR_SNAPSHOT} from ${SNAPSHOT_DIR}"
hdfs dfs -deleteSnapshot ${SNAPSHOT_DIR} ${CUR_SNAPSHOT}
else
echo "NEWER: ${CUR_FILE_EPOCH} - ${OLDER_THAN_EPOCH}"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment