Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backup and restore crontab file #263

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions shallow_backup/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ def backup_configs(backup_path, skip=False):
safe_mkdir(parent_dir)
copyfile(path_to_backup, quote(dest))

# backup crontab
command = "crontab -l"
alichtman marked this conversation as resolved.
Show resolved Hide resolved
dest = os.path.join(backup_path, "crontab.txt")
run_cmd_write_stdout(command, dest)
alichtman marked this conversation as resolved.
Show resolved Hide resolved


def backup_packages(backup_path, skip=False):
"""
Expand Down
8 changes: 8 additions & 0 deletions shallow_backup/reinstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ def reinstall_configs_sb(configs_path):
elif os.path.isfile(path_to_backup):
copyfile(path_to_backup, dest_path)

# reinstall crontab
with open(os.path.join(configs_path, "crontab.txt"), "r") as f:
alichtman marked this conversation as resolved.
Show resolved Hide resolved
for x in f:
# the replace sanitizes the crontab line of any present " characters.
sanitized = x.replace('"', '\\"')
cmd = f"(crontab -l ; echo \"{sanitized}\") | sort - | uniq - | crontab -"
Copy link
Owner

@alichtman alichtman Apr 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also not convinced this is the best way to do this. This escaping may not work properly in all cases. Make sure to test it with some complex commands that require proper escaping.

Also maybe use shlex.quote()? https://docs.python.org/3.5/library/shlex.html#shlex.quote

run_cmd(cmd)

print_section_header("CONFIG REINSTALLATION COMPLETED", Fore.BLUE)


Expand Down