From e20dcce91386477c1ea5411c4dfed93a2e7b8a77 Mon Sep 17 00:00:00 2001 From: Sergey Morozov Date: Wed, 13 Jul 2022 12:00:34 +0300 Subject: [PATCH] Add recursive deletion support --- README.md | 19 ++++++++++--------- clean-backups.py | 26 ++++++++++++++++++-------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index a631df2..f85ee53 100644 --- a/README.md +++ b/README.md @@ -3,28 +3,29 @@ ## 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 positional arguments: - PATH directory path + PATH directory path optional arguments: - -h, --help show this help message and exit - -d N, --daily N keep N daily backups, default: 7 - -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 + -h, --help show this help message and exit + -d N, --daily N keep N daily backups, default: 7 + -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 + format of timestamp, default: %Y%m%d ``` For a complete timestamp format description, see the python strftime() documentation: diff --git a/clean-backups.py b/clean-backups.py index 131978d..e489576 100755 --- a/clean-backups.py +++ b/clean-backups.py @@ -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) - f.remove_if_needed(force_remove=force) +# 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