Add --timestamp-format option

This commit is contained in:
Сергей Морозов 2022-05-31 16:50:01 +03:00
parent 6e99c6f43b
commit a9dbf7917f
3 changed files with 34 additions and 2 deletions

View File

@ -16,4 +16,6 @@ optional arguments:
-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
-t FORMAT, --timestamp-format FORMAT
format of timestamp, default: %Y%m%d
``` ```

View File

@ -8,6 +8,7 @@ import argparse
DEFAULT_DAILY = 7 DEFAULT_DAILY = 7
DEFAULT_WEEKLY = 4 DEFAULT_WEEKLY = 4
DEFAULT_MONTHLY = 3 DEFAULT_MONTHLY = 3
DEFAULT_TIMESTAMP_FORMAT = "%Y%m%d"
class BackupFile(): class BackupFile():
""" """
@ -168,6 +169,16 @@ parser.add_argument(
action="store_true", action="store_true",
help = "suppress remove confirmation" 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() args = parser.parse_args()
daily = args.daily daily = args.daily
@ -175,9 +186,15 @@ weekly = args.weekly
monthly = args.monthly monthly = args.monthly
force = args.force force = args.force
directory = args.path[0] directory = args.path[0]
timestamp_format = args.timestamp_format
# File processing # 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 # Generate file list with full paths
paths = [ paths = [
os.path.join(directory, f) for f in os.listdir(directory) os.path.join(directory, f) for f in os.listdir(directory)

View File

@ -10,6 +10,7 @@ import os
DEFAULT_NUMBER_OF_FILES = 200 DEFAULT_NUMBER_OF_FILES = 200
DEFAULT_PREFIX = "testfile-" DEFAULT_PREFIX = "testfile-"
DEFAULT_SUFFIX = ".bak" DEFAULT_SUFFIX = ".bak"
DEFAULT_TIMESTAMP_FORMAT = "%Y%m%d"
# Argument parser # Argument parser
parser = argparse.ArgumentParser(description="Cleanup old backups") parser = argparse.ArgumentParser(description="Cleanup old backups")
@ -45,14 +46,26 @@ parser.add_argument(
metavar = "SUFFIX", metavar = "SUFFIX",
help = f"use SUFFIX as file name suffix, default: {DEFAULT_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() args = parser.parse_args()
path = args.path[0] path = args.path[0]
number_of_files = args.number_of_files number_of_files = args.number_of_files
prefix = args.prefix prefix = args.prefix
suffix = args.suffix suffix = args.suffix
timestamp_format = args.timestamp_format
for i in range(number_of_files): 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}" filename = f"{prefix}{timestamp}{suffix}"
Path(os.path.join(path, filename)).touch() Path(os.path.join(path, filename)).touch()