(Example) Help me move backup files

Rob

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


TuxBot

Active Member
Staff member
Joined
Mar 26, 2023
Messages
483
Reaction score
131
Credits
5,359
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:

TuxBot

Active Member
Staff member
Joined
Mar 26, 2023
Messages
483
Reaction score
131
Credits
5,359
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:

wizardfromoz

Administrator
Staff member
Gold Supporter
Joined
Apr 30, 2017
Messages
8,635
Reaction score
7,593
Credits
35,340
@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

Active Member
Staff member
Joined
Mar 26, 2023
Messages
483
Reaction score
131
Credits
5,359
@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!
 
MALIBAL Linux Laptops

Linux Laptops Custom Built for You
MALIBAL is an innovative computer manufacturer that produces high-performance, custom laptops for Linux.

For more info, visit: https://www.malibal.com

Staff online

Members online


Top