KenKundert / emborg

Interactive command line interface to Borg Backup
GNU General Public License v3.0
94 stars 8 forks source link

run_after_last_backup may sometimes run too soon #58

Closed jjakob closed 2 years ago

jjakob commented 2 years ago

I use run_before_first_backup and run_after_last_backup to create a LVM snapshot of the root filesystem and mount it, unmount and delete it at the end, and also mount and unmount the remote repository location. This causes emborg to error after borg create as it runs the post-backup script immediately after, before it runs borg list, which means the repository is no longer there.

staging run_after_last_backup[0] post-backup script
running: mountpoint -q "/mnt/nas" && umount "/mnt/nas"
exit status: 0
staging run_after_last_backup[1] post-backup script
running: umount -v "/mnt/ubuntu-lv-backup-snapshot"
exit status: 0
staging run_after_last_backup[2] post-backup script
running: lvremove \
    -f \
    -S \
    'lv_role = snapshot' \
    ubuntu-vg/ubuntu-lv-backup-snapshot
exit status: 0
staging run_after_last_backup[3] post-backup script
running: rm -v -d "/mnt/ubuntu-lv-backup-snapshot"
exit status: 0
Checking archive ...
Setting BORG_PASSPHRASE.
Borg-related environment variables: {
    'BORG_DISPLAY_PASSPHRASE': 'no',
    'BORG_PASSPHRASE': '<redacted>',
}
running:
    borg list --prefix root- --json /mnt/nas/borg-foo-root
running in: /root/.config/emborg
starts at: 2022-01-27T15:54:35.244340+01:00
Borg terminates with exit status: 2
Unsetting BORG_PASSPHRASE.
emborg error: list: Repository /mnt/nas/borg-foo-root does not exist.
    This error occurred while checking the archives.
    No error was reported while creating the archive.
emborg: terminates with status 2.
emborg: log closed on Thursday, 27 January 2022 at 3:54:35 PM CET.

It means I need to move the pre/post scripts out of emborg config into a separate script. It's no problem for me, but it would maybe make more sense to run the post-backup script after all other commands, or make it configurable, as there is a good use-case for running commands right after create but before list, check, prune, ..., for example starting services back up. Or maybe it could be a separate command like "run_last" and leave run_after{,_last}_backup as-is.

This is my test config:

origin_vg="ubuntu-vg"
origin_lv="ubuntu-lv"
snap_name="{origin_lv}-backup-snapshot"
snap_mount="/mnt/{snap_name}"
dest_mount="/mnt/nas"
verb="-v"

src_dirs = snap_mount           # paths to directories to be backed up
one_file_system = True

run_before_first_backup = '''
  mountpoint -q "{dest_mount}" || mount "{dest_mount}"

  lvcreate -s -l 100%FREE --name "{snap_name}" "{origin_vg}/{origin_lv}"
  mkdir {verb} -p "{snap_mount}"
  mount {verb} "/dev/{origin_vg}/{snap_name}" "{snap_mount}"
'''

run_after_last_backup = '''
  mountpoint -q "{dest_mount}" && umount "{dest_mount}"

  umount {verb} "{snap_mount}"
  lvremove -f -S 'lv_role = snapshot' "{origin_vg}/{snap_name}"
  rm {verb} -d "{snap_mount}"
'''
KenKundert commented 2 years ago

Any commands specified in the run_⚹_backup settings are only run with the Borg create command. If I understand your situation, you need to run commands before almost every Borg command. Is that correct? Specifically, you would want to mount the repository before the first Borg command is run, and the umount command after the last Borg command is run.

jjakob commented 2 years ago

Yes. Although I already wrote a wrapper script to achieve that, it might be useful for other people.

KenKundert commented 2 years ago

I have added two new settings, run_before_borg and run_after_borg. They allow you to run a command or commands before the first and after the last calls to Borg. Hopefully, that is what you were looking for. Would you please try it out and make sure it meets expectations. It is not on PyPI yet, but you can get it from this GitHub repository (version 1.30.1).

jjakob commented 2 years ago

Thank you, I don't need the feature any more as I worked around it with my own shell wrapper script. I may try it in the future if I need it again.

KenKundert commented 2 years ago

You might consider switching to the the Emborg implementation as it will only run your extra commands before and after running Borg itself. If you use a script, your extra commands will be run every time you run Emborg even if Borg would not be run. For example, 'emborg due' does not run Borg. Neither does the configs or log commands.