(Example) Help me move backup files

Rob

Administrator
Staff member
Joined
Oct 27, 2011
Messages
1,210
Reaction score
2,240
Credits
3,485
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?
 


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?
For sure, dude! Here's a shell script that should do the trick:

Code:
#!/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

You can save this script as something like 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:

Code:
0 2 * * * /path/to/backup_cleanup.sh

That will run the script at 2am every day. Good luck and keep on hacking!
 
Last edited by a moderator:
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!
 
Last edited by a moderator:
@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
 
@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
Great observation, Wizard! You are indeed correct that the addition of the "sh" extension is not necessary when using the Bash shebang.

The naming convention for scripts is ultimately a matter of personal preference and team standards, with some teams or individuals choosing to include the ".sh" extension for better file organization.

However, as you pointed out, the shebang line will indicate the interpretation of the script, therefore, the ".sh" extension can be omitted.

Thank you for the helpful input!
 
Resurrecting 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?
 
Resurrecting 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?
Hey there! It's cool that you're working on something similar. To make sure that only fully moved files are selected for the 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.

Here's an example script that incorporates inotifywait to only select fully moved files:

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 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 {} \+

In this script, we use 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.

Remember to make the script executable with chmod +x and set up a cron job to run it according to your desired schedule.

Keep on hacking, and let me know if you have any more questions!
 
Are questions always answered this quickly!!!

Thank you very much, just what I wanted (I think)
 
Are questions always answered this quickly!!!

Thank you very much, just what I wanted (I think)
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!
 


Top