A simple Elixir script for backing up directories in set intervals
usage:
elixir simple_interval_backup.exs source_path target_path interval_minutes
| defmodule SimpleIntervalBackup.Time do | |
| @moduledoc """ | |
| Interval time related helpers | |
| """ | |
| def minutes_to_millis(minutes) do | |
| minutes * 60000 | |
| end | |
| end | |
| defmodule SimpleIntervalBackup.Interval do | |
| @moduledoc """ | |
| Interval actions | |
| """ | |
| require Logger | |
| def start(source_path, target_path, interval_millis) do | |
| receive do | |
| after interval_millis -> | |
| file_copy_response = | |
| File.cp_r(source_path, target_path <> "/#{:os.system_time(:milli_seconds)}") | |
| file_copy_status = | |
| file_copy_response | |
| |> Tuple.to_list() | |
| |> Enum.at(0) | |
| case file_copy_status do | |
| :ok -> | |
| Logger.info("Directory copied.") | |
| start(source_path, target_path, interval_millis) | |
| :error -> | |
| Logger.warn("Error when copying source directory. Skipping this round.") | |
| start(source_path, target_path, interval_millis) | |
| end | |
| end | |
| end | |
| end | |
| [source_path, target_path, interval_minutes] = System.argv() | |
| interval_minutes = String.to_integer(interval_minutes) | |
| interval_millis = SimpleIntervalBackup.Time.minutes_to_millis(interval_minutes) | |
| cond do | |
| File.exists?(source_path) === false -> exit("Source path is not valid") | |
| File.exists?(target_path) === false -> exit("Target path is not valid") | |
| true -> SimpleIntervalBackup.Interval.start(source_path, target_path, interval_millis) | |
| end |
A simple Elixir script for backing up directories in set intervals
usage:
elixir simple_interval_backup.exs source_path target_path interval_minutes