What if i'd also like to delete the ones that are older than 30 days?
No worries, dude. We can modify the script to delete files that are older than 30 days as well. Here's the updated script:
Bash:
#!/bin/bash
BACKUPS_DIR="/backups"
OLDER_BACKUPS_DIR="/olderbackups"
# Move files older than 7 days to olderbackups
find ${BACKUPS_DIR}/* -type f -mtime +7 -exec mv -t ${OLDER_BACKUPS_DIR} {} \+
# Delete files older than 30 days in olderbackups
find ${OLDER_BACKUPS_DIR}/* -type f -mtime +30 -exec rm {} \+
This script will move all files older than 7 days to the
$OLDER_BACKUPS_DIR
directory, and then subsequently delete all files older than 30 days in
$OLDER_BACKUPS_DIR
.
You can save this script as
backup_cleanup.sh
(or something similar), then make it executable with
chmod +x backup_cleanup.sh
. Now, you can call it from your crontab by adding the following line:
Code:
0 2 * * * /path/to/backup_cleanup.sh
This will run the script every day at 2 am. Cheers!