Add recursive deletion support
This commit is contained in:
		
							parent
							
								
									6225af0308
								
							
						
					
					
						commit
						e20dcce913
					
				@ -3,14 +3,14 @@
 | 
			
		||||
## clean-backups.py
 | 
			
		||||
 | 
			
		||||
Tool to cleanup backup directory with daily, weekly and monthly backups.
 | 
			
		||||
This tool removes **everything** in the specified directory (not recursively) except files, containing timestamps in the filename that can identified by this tool as fresh backups.
 | 
			
		||||
This tool removes **everything** in the specified directory (not recursively by default) except files, containing timestamps in the filename that can identified by this tool as fresh backups.
 | 
			
		||||
 | 
			
		||||
By default, this tool keeps last 7 daily backups, 4 weekly backups (4 last monday backups) and 3 monthly backups (1st day of last 3 months). You can redefine this by command line parameters.
 | 
			
		||||
 | 
			
		||||
This tool expects timestamps like this: 20220531 (May 31, 2022), but of course, you can redefine this.
 | 
			
		||||
 | 
			
		||||
```text
 | 
			
		||||
usage: clean-backups.py [-h] [-d N] [-w N] [-m N] [-f] PATH
 | 
			
		||||
usage: clean-backups.py [-h] [-d N] [-w N] [-m N] [-f] [-r] [-t FORMAT] PATH
 | 
			
		||||
 | 
			
		||||
Cleanup old backups
 | 
			
		||||
 | 
			
		||||
@ -23,6 +23,7 @@ optional arguments:
 | 
			
		||||
  -w N, --weekly N      keep N weekly backups, default: 4
 | 
			
		||||
  -m N, --monthly N     keep N monthly backups, default: 3
 | 
			
		||||
  -f, --force           suppress remove confirmation
 | 
			
		||||
  -r, --recursive       remove files recursively
 | 
			
		||||
  -t FORMAT, --timestamp-format FORMAT
 | 
			
		||||
                        format of timestamp, default: %Y%m%d
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
@ -176,6 +176,12 @@ parser.add_argument(
 | 
			
		||||
    action="store_true",
 | 
			
		||||
    help="suppress remove confirmation"
 | 
			
		||||
)
 | 
			
		||||
# force removal
 | 
			
		||||
parser.add_argument(
 | 
			
		||||
    "-r", "--recursive",
 | 
			
		||||
    action="store_true",
 | 
			
		||||
    help="remove files recursively"
 | 
			
		||||
)
 | 
			
		||||
# timestamp format
 | 
			
		||||
parser.add_argument(
 | 
			
		||||
    "-t",
 | 
			
		||||
@ -190,22 +196,26 @@ args = parser.parse_args()
 | 
			
		||||
daily = args.daily
 | 
			
		||||
weekly = args.weekly
 | 
			
		||||
monthly = args.monthly
 | 
			
		||||
recursive = args.recursive
 | 
			
		||||
force = args.force
 | 
			
		||||
directory = args.path[0]
 | 
			
		||||
timestamp_format = args.timestamp_format
 | 
			
		||||
 | 
			
		||||
#
 | 
			
		||||
# File processing
 | 
			
		||||
#
 | 
			
		||||
 | 
			
		||||
# Create BackupFile object with retention settings
 | 
			
		||||
files = BackupFile(
 | 
			
		||||
    retention_daily=daily,
 | 
			
		||||
    retention_weekly=weekly,
 | 
			
		||||
    retention_monthly=monthly,
 | 
			
		||||
    dateformat=timestamp_format
 | 
			
		||||
)
 | 
			
		||||
# Generate file list with full paths
 | 
			
		||||
paths = [
 | 
			
		||||
    os.path.join(directory, f) for f in os.listdir(directory)
 | 
			
		||||
    if os.path.isfile(os.path.join(directory, f))
 | 
			
		||||
]
 | 
			
		||||
for path in paths:
 | 
			
		||||
    f = files.new_file(path)
 | 
			
		||||
# Walk through subdirectory tree
 | 
			
		||||
for root, dirs, filenames in os.walk(directory):
 | 
			
		||||
    for filename in filenames:
 | 
			
		||||
        f = files.new_file(os.path.join(root, filename))
 | 
			
		||||
        f.remove_if_needed(force_remove=force)
 | 
			
		||||
    if not recursive:
 | 
			
		||||
        break
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user