Add recursive deletion support
This commit is contained in:
parent
6225af0308
commit
e20dcce913
19
README.md
19
README.md
|
@ -3,28 +3,29 @@
|
||||||
## clean-backups.py
|
## clean-backups.py
|
||||||
|
|
||||||
Tool to cleanup backup directory with daily, weekly and monthly backups.
|
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.
|
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.
|
This tool expects timestamps like this: 20220531 (May 31, 2022), but of course, you can redefine this.
|
||||||
|
|
||||||
```text
|
```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
|
Cleanup old backups
|
||||||
|
|
||||||
positional arguments:
|
positional arguments:
|
||||||
PATH directory path
|
PATH directory path
|
||||||
|
|
||||||
optional arguments:
|
optional arguments:
|
||||||
-h, --help show this help message and exit
|
-h, --help show this help message and exit
|
||||||
-d N, --daily N keep N daily backups, default: 7
|
-d N, --daily N keep N daily backups, default: 7
|
||||||
-w N, --weekly N keep N weekly backups, default: 4
|
-w N, --weekly N keep N weekly backups, default: 4
|
||||||
-m N, --monthly N keep N monthly backups, default: 3
|
-m N, --monthly N keep N monthly backups, default: 3
|
||||||
-f, --force suppress remove confirmation
|
-f, --force suppress remove confirmation
|
||||||
|
-r, --recursive remove files recursively
|
||||||
-t FORMAT, --timestamp-format FORMAT
|
-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: <https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior>
|
For a complete timestamp format description, see the python strftime() documentation: <https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior>
|
||||||
|
|
|
@ -176,6 +176,12 @@ parser.add_argument(
|
||||||
action="store_true",
|
action="store_true",
|
||||||
help="suppress remove confirmation"
|
help="suppress remove confirmation"
|
||||||
)
|
)
|
||||||
|
# force removal
|
||||||
|
parser.add_argument(
|
||||||
|
"-r", "--recursive",
|
||||||
|
action="store_true",
|
||||||
|
help="remove files recursively"
|
||||||
|
)
|
||||||
# timestamp format
|
# timestamp format
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-t",
|
"-t",
|
||||||
|
@ -190,22 +196,26 @@ args = parser.parse_args()
|
||||||
daily = args.daily
|
daily = args.daily
|
||||||
weekly = args.weekly
|
weekly = args.weekly
|
||||||
monthly = args.monthly
|
monthly = args.monthly
|
||||||
|
recursive = args.recursive
|
||||||
force = args.force
|
force = args.force
|
||||||
directory = args.path[0]
|
directory = args.path[0]
|
||||||
timestamp_format = args.timestamp_format
|
timestamp_format = args.timestamp_format
|
||||||
|
|
||||||
|
#
|
||||||
# File processing
|
# File processing
|
||||||
|
#
|
||||||
|
|
||||||
|
# Create BackupFile object with retention settings
|
||||||
files = BackupFile(
|
files = BackupFile(
|
||||||
retention_daily=daily,
|
retention_daily=daily,
|
||||||
retention_weekly=weekly,
|
retention_weekly=weekly,
|
||||||
retention_monthly=monthly,
|
retention_monthly=monthly,
|
||||||
dateformat=timestamp_format
|
dateformat=timestamp_format
|
||||||
)
|
)
|
||||||
# Generate file list with full paths
|
# Walk through subdirectory tree
|
||||||
paths = [
|
for root, dirs, filenames in os.walk(directory):
|
||||||
os.path.join(directory, f) for f in os.listdir(directory)
|
for filename in filenames:
|
||||||
if os.path.isfile(os.path.join(directory, f))
|
f = files.new_file(os.path.join(root, filename))
|
||||||
]
|
f.remove_if_needed(force_remove=force)
|
||||||
for path in paths:
|
if not recursive:
|
||||||
f = files.new_file(path)
|
break
|
||||||
f.remove_if_needed(force_remove=force)
|
|
||||||
|
|
Loading…
Reference in New Issue