Skip to content

Instantly share code, notes, and snippets.

@anax32
Last active November 7, 2020 01:19
Show Gist options
  • Save anax32/aabb876127220625616624086e13a2ac to your computer and use it in GitHub Desktop.
Save anax32/aabb876127220625616624086e13a2ac to your computer and use it in GitHub Desktop.
archive a directory of large files into tars grouped by month
#/bin/bash -u
#
# take a dir with a bunch of files
# and archive into a collection of tar.gz grouped by month
#
# i.e. get all your phone photos into
# a dir, run this script to create monthly
# tarballs and upload to S3
#
# NB:
# output directory should not be inside input, or you suck
#
# usage:
# ./monthly-archive-dir.sh <input-dir> <output-dir>
#
INPUT_DIR=$1
OUTPUT_DIR=$2
echo "reading files from ${INPUT_DIR}"
echo "writing to ${OUTPUT_DIR}"
mkdir -p ${OUTPUT_DIR}
# create the tarball lists
for f in ${INPUT_DIR}/*
do
LIST_FILENAME=${OUTPUT_DIR}/$(date -r $f "+%m-%Y").lst
echo $(basename $f) >> ${LIST_FILENAME}
done
# archive all the lists
# NOTE: if we do this in the background we cannot ctrl-z the tars
# NOTE: 'tar: child died with signal 15' if we do tar cfz, so gzip separately
for f in ${OUTPUT_DIR}/*.lst
do
TAR_FILENAME=${OUTPUT_DIR}/$(basename ${f%.lst}).tar
echo $f ${TAR_FILENAME}
tar cf \
${TAR_FILENAME} \
-C ${INPUT_DIR} \
-T $f
gzip ${TAR_FILENAME}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment