2019-08-30 16:14:18 -07:00
|
|
|
#!/usr/bin/env python3
|
2019-02-27 15:10:35 -08:00
|
|
|
|
2019-03-13 03:13:23 -07:00
|
|
|
"""
|
|
|
|
|
|
|
|
Mike's Backup Rotator
|
|
|
|
|
|
|
|
A simple script to help automatically rotate backup files
|
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
Copyright 2022 Mike Peralta; All rights reserved
|
2019-03-13 03:13:23 -07:00
|
|
|
|
|
|
|
Released under the GNU GENERAL PUBLIC LICENSE v3 (See LICENSE file for more)
|
|
|
|
|
|
|
|
"""
|
2019-02-27 15:10:35 -08:00
|
|
|
|
2019-02-27 15:50:56 -08:00
|
|
|
import datetime
|
2019-02-27 16:25:10 -08:00
|
|
|
import os
|
2019-02-27 16:51:43 -08:00
|
|
|
import shutil
|
2019-02-27 15:50:56 -08:00
|
|
|
import sys
|
2020-01-02 18:06:09 -08:00
|
|
|
import syslog
|
2022-01-31 14:35:48 -08:00
|
|
|
import time
|
2019-02-27 15:50:56 -08:00
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
2019-02-27 15:10:35 -08:00
|
|
|
class BackupRotator:
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
2019-03-13 01:35:34 -07:00
|
|
|
self.__dry_run = False
|
|
|
|
self.__configs = []
|
|
|
|
self.__config_paths = []
|
|
|
|
self.__calculated_actions = []
|
2019-02-27 15:10:35 -08:00
|
|
|
|
2022-01-31 12:35:08 -08:00
|
|
|
def run(self, configs, dry_run: bool = False):
|
2019-02-27 15:10:35 -08:00
|
|
|
|
2019-02-27 15:50:56 -08:00
|
|
|
self.log("Begin")
|
2022-01-31 12:35:08 -08:00
|
|
|
|
|
|
|
self.__dry_run = dry_run
|
|
|
|
self.__config_paths = configs
|
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
self._consume_configs(self.__config_paths)
|
2019-02-27 15:50:56 -08:00
|
|
|
|
2019-03-13 01:35:34 -07:00
|
|
|
# Rotate once per config
|
|
|
|
for config_index in range(len(self.__configs)):
|
|
|
|
|
|
|
|
#
|
|
|
|
config = self.__configs[config_index]
|
|
|
|
|
|
|
|
#
|
|
|
|
self.log("Rotating for config " + str(config_index + 1) + " of " + str(len(self.__configs)), config["__path"])
|
2022-01-31 14:35:48 -08:00
|
|
|
self._do_rotate(config)
|
2019-02-27 15:50:56 -08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def current_time():
|
|
|
|
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
now_s = now.strftime("%b-%d-%Y %I:%M%p")
|
|
|
|
return str(now_s)
|
|
|
|
|
|
|
|
def log(self, s, o=None):
|
|
|
|
|
|
|
|
now = self.current_time()
|
|
|
|
|
|
|
|
to_log = "[" + now + "][Backup Rotator] " + str(s)
|
|
|
|
if o is not None:
|
|
|
|
to_log += " " + str(o)
|
|
|
|
|
2020-01-02 18:06:09 -08:00
|
|
|
syslog.syslog(to_log)
|
|
|
|
|
2019-02-27 15:50:56 -08:00
|
|
|
print(to_log)
|
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
def _consume_configs(self, paths: list=None):
|
2019-03-13 01:35:34 -07:00
|
|
|
|
2022-01-31 12:35:08 -08:00
|
|
|
assert paths is not None, "Config paths cannot be None"
|
|
|
|
assert len(paths) > 0, "Must provide at least one config file path"
|
2019-02-27 15:50:56 -08:00
|
|
|
|
2019-03-13 01:35:34 -07:00
|
|
|
# Use each config path
|
|
|
|
for path in paths:
|
|
|
|
|
|
|
|
# If this is a single path
|
|
|
|
if os.path.isfile(path):
|
2022-01-31 14:35:48 -08:00
|
|
|
self._consume_config(path)
|
2019-03-13 01:35:34 -07:00
|
|
|
|
|
|
|
# If this is a directory
|
|
|
|
elif os.path.isdir(path):
|
|
|
|
|
|
|
|
# Iterate over each file inside
|
|
|
|
for file_name in os.listdir(path):
|
2022-01-31 14:35:48 -08:00
|
|
|
self._consume_config(os.path.join(path, file_name))
|
2019-03-13 01:35:34 -07:00
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
def _consume_config(self, path: str):
|
2019-03-13 01:35:34 -07:00
|
|
|
|
|
|
|
# Open the file
|
2019-02-27 15:50:56 -08:00
|
|
|
f = open(path)
|
2019-03-13 01:35:34 -07:00
|
|
|
if not f:
|
|
|
|
raise Exception("Unable to open config file: " + path)
|
|
|
|
|
|
|
|
# Parse
|
2019-07-16 12:06:20 -07:00
|
|
|
config = yaml.safe_load(f)
|
2019-02-27 15:50:56 -08:00
|
|
|
|
2019-03-13 01:35:34 -07:00
|
|
|
# Add its own path
|
|
|
|
config["__path"] = path
|
|
|
|
|
|
|
|
# Consume to internal
|
|
|
|
self.__configs.append(config)
|
|
|
|
self.log("Consumed config from path:", path)
|
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
def _do_rotate(self, config):
|
2019-03-13 01:35:34 -07:00
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
self._rotate_paths(config)
|
2019-02-27 15:50:56 -08:00
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
def _rotate_paths(self, config):
|
2019-02-27 15:50:56 -08:00
|
|
|
|
2019-03-13 01:35:34 -07:00
|
|
|
self.log("Begin rotating " + str(len(config["paths"])) + " paths")
|
|
|
|
for path in config["paths"]:
|
2022-01-31 14:35:48 -08:00
|
|
|
self._rotate_path(config, path)
|
2019-02-27 15:50:56 -08:00
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
def _rotate_path(self, config, path):
|
2019-02-27 15:50:56 -08:00
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
assert os.path.isdir(path), "Path should be a directory: {}".format(path)
|
2019-02-27 16:25:10 -08:00
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
self.log("Rotating path: {}".format(path))
|
2019-02-27 16:25:10 -08:00
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
found_any_rotation_keys = False
|
|
|
|
if "maximum-items" in config.keys():
|
|
|
|
found_any_rotation_keys = True
|
|
|
|
self._rotate_path_for_maximum_items(config=config, path=path, max_items=config["maximum-items"])
|
|
|
|
if "maximum-age" in config.keys():
|
|
|
|
found_any_rotation_keys = True
|
|
|
|
self._rotate_path_for_maximum_age(config=config, path=path, max_age_days=config["maximum-age"])
|
|
|
|
|
|
|
|
assert found_any_rotation_keys is True, \
|
|
|
|
"Config needs one of the following keys: \"maximum-items\""
|
|
|
|
|
|
|
|
def _rotate_path_for_maximum_items(self, config, path: str, max_items: int):
|
|
|
|
|
|
|
|
assert os.path.isdir(path), "Path should be a directory: {}".format(path)
|
2019-02-27 16:25:10 -08:00
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
self.log("Rotating path for maximum items: {}".format(path))
|
|
|
|
|
|
|
|
children = self._gather_rotation_candidates(config, path)
|
2019-02-27 16:25:10 -08:00
|
|
|
|
|
|
|
# Do we need to rotate anything out?
|
|
|
|
if len(children) <= max_items:
|
|
|
|
self.log(
|
2022-01-31 14:35:48 -08:00
|
|
|
"Path only has {} items, but needs more than {} for rotation; Won't rotate this path.".format(
|
|
|
|
len(children), max_items
|
|
|
|
)
|
2019-02-27 16:25:10 -08:00
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
#
|
|
|
|
purge_count = len(children) - max_items
|
2022-01-31 14:35:48 -08:00
|
|
|
self.log("Need to purge {} items".format(purge_count))
|
2019-02-27 16:25:10 -08:00
|
|
|
|
|
|
|
for purge_index in range(purge_count):
|
2019-03-13 01:35:34 -07:00
|
|
|
|
|
|
|
#
|
2022-01-31 14:35:48 -08:00
|
|
|
item_to_purge, item_ctime = self._pick_oldest_item(config, children)
|
2019-02-27 16:25:10 -08:00
|
|
|
children.remove(item_to_purge)
|
2022-01-31 14:35:48 -08:00
|
|
|
self.log("Found next item to purge: ({}) {} (ctime: {})".format(
|
|
|
|
purge_index + 1,
|
|
|
|
os.path.basename(item_to_purge), item_ctime
|
|
|
|
))
|
2019-03-13 01:35:34 -07:00
|
|
|
|
|
|
|
#
|
2022-01-31 14:35:48 -08:00
|
|
|
self._remove_item(config, item_to_purge)
|
2019-02-27 16:25:10 -08:00
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
def _rotate_path_for_maximum_age(self, config, path: str, max_age_days: int):
|
|
|
|
|
|
|
|
assert os.path.isdir(path), "Path should be a directory: {}".format(path)
|
|
|
|
|
|
|
|
self.log("Rotating path for max age of {} days: {}".format(max_age_days, path))
|
|
|
|
|
|
|
|
children = self._gather_rotation_candidates(config, path)
|
|
|
|
|
|
|
|
self.log("Examining {} items for deletion")
|
|
|
|
children_to_delete = []
|
|
|
|
for child in children:
|
|
|
|
|
|
|
|
age_seconds = self._detect_item_age_seconds(config, child)
|
|
|
|
age_days = self._detect_item_age_days(config, child)
|
|
|
|
age_formatted = self.seconds_to_time_string(age_seconds)
|
|
|
|
child_basename = os.path.basename(child)
|
|
|
|
|
|
|
|
if age_days > max_age_days:
|
|
|
|
self.log("Old enough to delete: {} ({})".format(
|
|
|
|
child_basename, age_formatted
|
|
|
|
))
|
|
|
|
children_to_delete.append(child)
|
|
|
|
else:
|
|
|
|
self.log("Not old enough to delete: {} ({})".format(
|
|
|
|
child_basename, age_formatted
|
|
|
|
))
|
|
|
|
|
|
|
|
self.log("Removing old items ...")
|
|
|
|
for child_to_delete in children_to_delete:
|
|
|
|
basename = os.path.basename(child_to_delete)
|
|
|
|
self.log("> {}".format(basename))
|
|
|
|
self._remove_item(config, child_to_delete)
|
|
|
|
|
|
|
|
|
2019-03-13 01:35:34 -07:00
|
|
|
@staticmethod
|
2022-01-31 14:35:48 -08:00
|
|
|
def _gather_rotation_candidates(config, path):
|
2019-02-27 16:25:10 -08:00
|
|
|
|
|
|
|
candidates = []
|
|
|
|
|
2019-03-13 01:35:34 -07:00
|
|
|
if "target-type" not in config.keys():
|
2019-02-27 16:25:10 -08:00
|
|
|
raise Exception("Please provide the configuration key: target-type")
|
|
|
|
|
|
|
|
for item_name in os.listdir(path):
|
|
|
|
|
|
|
|
item_path = os.path.join(path, item_name)
|
|
|
|
|
2019-03-13 01:35:34 -07:00
|
|
|
if config["target-type"] == "file":
|
2019-02-27 16:25:10 -08:00
|
|
|
if not os.path.isfile(item_path):
|
|
|
|
continue
|
2019-03-13 01:35:34 -07:00
|
|
|
elif config["target-type"] == "directory":
|
2019-02-27 16:25:10 -08:00
|
|
|
if not os.path.isdir(item_path):
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
raise Exception("Configuration key \"target-type\" must be \"file\" or \"directory\"")
|
|
|
|
|
|
|
|
candidates.append(item_path)
|
|
|
|
|
|
|
|
return candidates
|
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
def _pick_oldest_item(self, config, items):
|
2019-02-27 16:25:10 -08:00
|
|
|
|
|
|
|
best_item = None
|
2019-02-27 16:47:10 -08:00
|
|
|
best_ctime = None
|
2019-02-27 16:25:10 -08:00
|
|
|
for item in items:
|
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
ctime = self._detect_item_date(config, item)
|
|
|
|
if best_ctime is None or ctime < best_ctime:
|
|
|
|
best_ctime = ctime
|
|
|
|
best_item = item
|
2019-02-27 16:25:10 -08:00
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
return best_item, best_ctime
|
2019-03-13 01:35:34 -07:00
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
@staticmethod
|
|
|
|
def _detect_item_date(config, item):
|
|
|
|
|
|
|
|
assert "date-detection" in config.keys(), "Please provide config key: \"date-detection\""
|
|
|
|
detection = config["date-detection"]
|
|
|
|
|
|
|
|
if detection == "file":
|
|
|
|
ctime = os.path.getctime(item)
|
|
|
|
else:
|
|
|
|
raise AssertionError("Invalid value for \"date-detection\"; Should be one of {file}: {}".format(
|
|
|
|
detection
|
|
|
|
))
|
|
|
|
|
|
|
|
return ctime
|
|
|
|
|
|
|
|
def _detect_item_age_seconds(self, config, item):
|
|
|
|
|
|
|
|
now = time.time()
|
|
|
|
ctime = self._detect_item_date(config, item)
|
|
|
|
delta = now - ctime
|
|
|
|
|
|
|
|
return delta
|
|
|
|
|
|
|
|
def _detect_item_age_days(self, config, item):
|
|
|
|
|
|
|
|
age_seconds = self._detect_item_age_seconds(config, item)
|
|
|
|
age_days = int(age_seconds / 86400)
|
|
|
|
|
|
|
|
return age_days
|
|
|
|
|
|
|
|
def seconds_to_time_string(self, seconds: float):
|
|
|
|
|
|
|
|
if isinstance(seconds, float):
|
|
|
|
pass
|
|
|
|
elif isinstance(seconds, int):
|
|
|
|
seconds = float * 1.0
|
|
|
|
else:
|
|
|
|
raise AssertionError("Seconds must be an int or float")
|
|
|
|
|
|
|
|
# Map
|
|
|
|
map = {
|
|
|
|
"year": 31536000.0,
|
|
|
|
"month": 2592000.0,
|
|
|
|
"week": 604800.0,
|
|
|
|
"day": 86400.0,
|
|
|
|
"hour": 3600.0,
|
|
|
|
"minute": 60.0,
|
|
|
|
"second": 1.0
|
|
|
|
}
|
|
|
|
|
|
|
|
s_parts = []
|
|
|
|
for unit_label in map.keys():
|
|
|
|
unit_seconds = map[unit_label]
|
|
|
|
if seconds >= unit_seconds:
|
|
|
|
unit_count = int(seconds / unit_seconds)
|
|
|
|
s_parts.append("{} {}{}".format(
|
|
|
|
unit_count, unit_label,
|
|
|
|
"" if unit_count == 1 else "s"
|
|
|
|
))
|
|
|
|
seconds -= unit_seconds * unit_count
|
|
|
|
|
|
|
|
s = ", ".join(s_parts)
|
|
|
|
|
|
|
|
return s
|
|
|
|
|
|
|
|
def _remove_item(self, config, path):
|
|
|
|
|
|
|
|
if os.path.isfile(path):
|
|
|
|
self._remove_file(config, path)
|
|
|
|
elif os.path.isdir(path):
|
|
|
|
self._remove_directory(config, path)
|
|
|
|
else:
|
|
|
|
raise AssertionError("Don't know how to remove this item: {}".format(path))
|
|
|
|
|
|
|
|
def _remove_file(self, config, file_path):
|
2019-03-13 01:35:34 -07:00
|
|
|
|
|
|
|
if not os.path.isfile(file_path):
|
|
|
|
raise Exception("Tried to remove a file, but this path isn't a file: " + str(file_path))
|
|
|
|
|
|
|
|
if self.__dry_run:
|
|
|
|
self.log("Won't purge file during global-level dry run: ", file_path)
|
|
|
|
elif "dry-run" in config.keys() and config["dry-run"] is True:
|
|
|
|
self.log("Won't purge file during config-level dry run: ", file_path)
|
|
|
|
else:
|
|
|
|
self.log("Purging file:", file_path)
|
|
|
|
os.remove(file_path)
|
|
|
|
|
2022-01-31 14:35:48 -08:00
|
|
|
def _remove_directory(self, config, dir_path):
|
2019-03-13 01:35:34 -07:00
|
|
|
|
|
|
|
if not os.path.isdir(dir_path):
|
|
|
|
raise Exception("Tried to remove a directory, but this path isn't a directory: " + str(dir_path))
|
|
|
|
|
|
|
|
if self.__dry_run:
|
|
|
|
self.log("Won't purge directory during global-level dry run: ", dir_path)
|
|
|
|
elif "dry-run" in config.keys() and config["dry-run"] is True:
|
|
|
|
self.log("Won't purge directory during config-level dry run: ", dir_path)
|
|
|
|
else:
|
|
|
|
self.log("Purging directory:", dir_path)
|
|
|
|
shutil.rmtree(dir_path)
|