I have a script that creates folders in a backup folder by the current date. This script runs once a day, every day through cron.
Is there a way to delete folders older than 3 days via the folder name? sort of
date -3?
Script that works: Thanks Jo So. This script creates a folder by date. Compresses files for backup, inserts them into the backup directory and deletes backups for more than 3 days :-)
#!/bin/bash
cd /home/backups
mkdir $(date +%Y-%m-%d)
cd /opt/
tar -pczf /home/backups/$(date +%Y-%m-%d)/opt.tar.gz code
cd /var/
tar -pczf /home/backups/$(date +%Y-%m-%d)/var.tar.gz work
cd /home/backups/
threedaysago=`date -d "3 days ago" +%Y%m%d`
for backup in [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]
do
backupdate=`echo "$backup" | tr -d -` # remove dashes
if test "$backupdate" -lt "$threedaysago"
then
rm -rf "$backup"
fi
done
source
share