Created
May 3, 2018 17:18
-
-
Save a5ync/6ed84436ca9817367368aa3ed2192a73 to your computer and use it in GitHub Desktop.
Prune files based on their date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script is for manual cleanup of the hourly sql backups generated by the sqlbackup container. | |
# It is expected this would be run from the VM that is mounting the sqlbackup volume. | |
DAYS=$1 | |
if [ -z "$DAYS" ]; then | |
echo 'USAGE: $ prunebackups.sh <number of days to keep>' | |
echo 'Number of days must be an integer and includes today.' | |
echo 'To wipe out every thing except today: "./prunebackups.sh 1"' | |
exit 1 | |
fi | |
if [ "$DAYS" -lt 1 ]; then | |
echo 'SAFETY FEATURE!: Please select a number of days greater than 0.' | |
echo 'Seriously, if you want to wipe everything, you will have to rm -f * manually.' | |
exit 1 | |
fi | |
# Getting date $DAYS days ago, in comparison format. | |
CUTOFF=$(date --date="$DAYS days ago" +%Y%m%d) | |
# If the file is older than the number of days we gave as a command line argument, it gets deleted. | |
# NOTE: Filenames are in UTC, not necessarily in local timezone, so they may not match file date. | |
for i in /opt/mysql/sqlbackups/hourly/*backup.sql*; do | |
FILEDATE=$(stat --format='%Y' "$i") # unix epoch modtime of file | |
FILEDATE=$(date --date="@$FILEDATE" +%Y%m%d) # converted to YYYYmmdd | |
if [ "$FILEDATE" -le "$CUTOFF" ]; then | |
rm -vf "$i" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment