Linux automatically deletes N days before backup

  
 

The Linux backup server saves the DB backup file for one month. It needs to delete the backup one month before. The following is an automatic deletion script.

Statement: find corresponding directory-mtime + days-name “filename” -exec rm -rf {} \\;

Example 1: find /root/Backup -mtime + 30 -name “*.*” -exec rm -rf {} \\; Delete all files in the /root/Backup directory with ”.” 30 days ago

find:linux lookup Command, the user finds the file with the specified condition /root/Backup: the directory to be cleaned up -mtime: standard statement +30: find the file 30 days ago, here the number represents the number of days “*.*“: hope to find The data type, ”*.jpg” means to find all files with the extension jpg, ”*” means to find all files - exec: fixed writing rm -rf: force delete files, including directory {} \\; : fixed Write, a pair of braces + space + \\ +;

write to the shell script, then set crontab execution, then you can let the system automatically clean up the relevant files

Create a new automatic delete script AutoDelBackup.sh
#!/bin/bashfind /root/Backup -mtime +30 -name "*.*" -exec rm -rf {} \\;

#crontab -e* 2 * * * /root/AutoDelBac Kup.sh executes script every day at 2am

Copyright © Windows knowledge All Rights Reserved