Add --timestamp-format option
This commit is contained in:
parent
6e99c6f43b
commit
a9dbf7917f
|
@ -16,4 +16,6 @@ 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
|
||||
-t FORMAT, --timestamp-format FORMAT
|
||||
format of timestamp, default: %Y%m%d
|
||||
```
|
||||
|
|
|
@ -8,6 +8,7 @@ import argparse
|
|||
DEFAULT_DAILY = 7
|
||||
DEFAULT_WEEKLY = 4
|
||||
DEFAULT_MONTHLY = 3
|
||||
DEFAULT_TIMESTAMP_FORMAT = "%Y%m%d"
|
||||
|
||||
class BackupFile():
|
||||
"""
|
||||
|
@ -168,6 +169,16 @@ parser.add_argument(
|
|||
action="store_true",
|
||||
help = "suppress remove confirmation"
|
||||
)
|
||||
# timestamp format
|
||||
parser.add_argument(
|
||||
"-t",
|
||||
"--timestamp-format",
|
||||
type = str,
|
||||
default = DEFAULT_TIMESTAMP_FORMAT,
|
||||
metavar = "FORMAT",
|
||||
help = f"format of timestamp, default: {DEFAULT_TIMESTAMP_FORMAT}".replace(r"%",r"%%")
|
||||
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
daily = args.daily
|
||||
|
@ -175,9 +186,15 @@ weekly = args.weekly
|
|||
monthly = args.monthly
|
||||
force = args.force
|
||||
directory = args.path[0]
|
||||
timestamp_format = args.timestamp_format
|
||||
|
||||
# File processing
|
||||
files = BackupFile(retention_daily = daily, retention_weekly = weekly, retention_monthly = monthly)
|
||||
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)
|
||||
|
|
|
@ -10,6 +10,7 @@ import os
|
|||
DEFAULT_NUMBER_OF_FILES = 200
|
||||
DEFAULT_PREFIX = "testfile-"
|
||||
DEFAULT_SUFFIX = ".bak"
|
||||
DEFAULT_TIMESTAMP_FORMAT = "%Y%m%d"
|
||||
|
||||
# Argument parser
|
||||
parser = argparse.ArgumentParser(description="Cleanup old backups")
|
||||
|
@ -45,14 +46,26 @@ parser.add_argument(
|
|||
metavar = "SUFFIX",
|
||||
help = f"use SUFFIX as file name suffix, default: {DEFAULT_SUFFIX}"
|
||||
)
|
||||
# timestamp format
|
||||
parser.add_argument(
|
||||
"-t",
|
||||
"--timestamp-format",
|
||||
type = str,
|
||||
default = DEFAULT_TIMESTAMP_FORMAT,
|
||||
metavar = "FORMAT",
|
||||
help = f"format of timestamp, default: {DEFAULT_TIMESTAMP_FORMAT}".replace(r"%",r"%%")
|
||||
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
path = args.path[0]
|
||||
number_of_files = args.number_of_files
|
||||
prefix = args.prefix
|
||||
suffix = args.suffix
|
||||
timestamp_format = args.timestamp_format
|
||||
|
||||
for i in range(number_of_files):
|
||||
timestamp = (date.today() - timedelta(days=i)).strftime("%Y%m%d")
|
||||
timestamp = (date.today() - timedelta(days=i)).strftime(timestamp_format)
|
||||
filename = f"{prefix}{timestamp}{suffix}"
|
||||
Path(os.path.join(path, filename)).touch()
|
||||
|
Loading…
Reference in New Issue