Skip to content

Instantly share code, notes, and snippets.

@wa1kb0y
Created September 5, 2021 20:38
Show Gist options
  • Save wa1kb0y/825894bf2748c1e832be0fc848fd2153 to your computer and use it in GitHub Desktop.
Save wa1kb0y/825894bf2748c1e832be0fc848fd2153 to your computer and use it in GitHub Desktop.
Deleting files older from Dropbox than date with using ./dropbox_uploader.sh
#!/bin/bash
#
# Deleting files from dropbox with names like YYYY-MM-DD_blabla
#
if [ "$*" == "" ]; then
echo "No arguments provided. Example usage: ./dropbox_cleanup.sh mydir '2 months' --dry-run"
exit 1
fi
if [ "$1" == "" ]; then
echo "No destination dir provided"
exit 1
fi
if [ "$2" == "" ]; then
echo "No limit time provided. Examples: '2 months', '1 day'"
exit 1
fi
echo "Destination dir: $1"
echo "Limit: $2"
DEST_DIR="$1" # Dropbox backups base folder
# limit date to erase files
LIMIT_DATE=$(date --date="-$2" +%s) # date in seconds
# Retrieve the list of files
files=$(./dropbox_uploader.sh list $DEST_DIR | awk {' print $3 '} | tail -n +2)
#echo $files
# Process each file
for file in $files; do
fileDate=$(echo $file | tr "_" "\t" | tr "." "\t" | awk {' print $1 '})
# Retrieve file date in seconds
fileDateInSeconds=$(date -d ${fileDate} +%s)
# Erase the file if it exceeds the limit date
#echo "[DEBUG] Comparing ${fileDateInSeconds} - ${LIMIT_DATE}"
if [ ${fileDateInSeconds} -lt ${LIMIT_DATE} ]; then
echo "[INFO] Erasing file $DEST_DIR/$file"
if [ "$3" != "--dry-run" ]; then
#echo "Deleted"
./dropbox_uploader.sh delete ${DEST_DIR}/${file}
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment