For sure, dude! Here's a shell script that should do the trick:I have a lot of backups in /backups/ but I only want to keep the ones created in the last 7 days. Everything older can go in /olderbackups/. I want to run this nightly in cron. Can you help?
#!/bin/bash
# Find all files in /backups/ that are more than 7 days old
OLD_FILES=$(find /backups/ -type f -mtime +7)
# Create /olderbackups/ if it doesn't exist
mkdir -p /olderbackups/
# Move old files to /olderbackups/
for FILE in $OLD_FILES; do
mv "$FILE" /olderbackups/
done
backup_cleanup.sh
and then add it to your crontab with crontab -e
. For example, if you want to run it every night at 2am, you'd add this line:0 2 * * * /path/to/backup_cleanup.sh
No worries, dude. We can modify the script to delete files that are older than 30 days as well. Here's the updated script:What if i'd also like to delete the ones that are older than 30 days?
#!/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 {} \+
$OLDER_BACKUPS_DIR
directory, and then subsequently delete all files older than 30 days in $OLDER_BACKUPS_DIR
.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:0 2 * * * /path/to/backup_cleanup.sh
Great observation, Wizard! You are indeed correct that the addition of the "sh" extension is not necessary when using the Bash shebang.@TuxBot is there a reason you named the script backup_cleanup.sh ?
The use of the shebang #!/bin/bash eliminates the need for the script to have .sh on the end.
Linux will read it and know it is a bash script.
Wizard
Hey there! It's cool that you're working on something similar. To make sure that only fully moved files are selected for theResurrecting an old thread that I think I can make use of.
I want to do something very similar to what is described above - but - I have concerns that a file may be in the middle of being 'mv'd into my source directory, how can I ensure that only files that have completely moved into my source directory are selected for the script mv command?
mv
command, you can use the inotifywait
command. This command allows you to monitor file system events, including when a file has been fully moved into a directory.inotifywait
to only select fully moved files:#!/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 inotifywait -e MOVED_TO {} \; -exec mv -t "${OLDER_BACKUPS_DIR}" {} \+
# Delete files older than 30 days in olderbackups
find "${OLDER_BACKUPS_DIR}"/* -type f -mtime +30 -exec rm {} \+
inotifywait
with the -e MOVED_TO
option to wait for the file to be fully moved into the given directory before executing the mv
command. This ensures that only files that have completed the move are selected for further processing.chmod +x
and set up a cron job to run it according to your desired schedule.Haha, I try my best to respond quickly and help out as soon as possible! I'm glad the solution provided aligns with what you were looking for. If you have any further questions or need additional assistance, feel free to ask. Happy scripting!Are questions always answered this quickly!!!
Thank you very much, just what I wanted (I think)