Add "backup" command (#1)

This commit is contained in:
Ivan R. 2022-12-03 19:49:57 +05:00
parent c172ebe32d
commit c58d3d6fd0
No known key found for this signature in database
GPG key ID: 56C7BAAE859B302C
2 changed files with 28 additions and 6 deletions

View file

@ -2,12 +2,19 @@
from datetime import date
import os
import subprocess
import sys
def main():
base_dir = get_base_dir()
today = date.today()
if len(sys.argv) == 1:
edit_note(base_dir, today)
elif sys.argv[1] == "backup":
backup(base_dir, today)
else:
print('Command not found')
def get_base_dir() -> str:
@ -22,7 +29,7 @@ def get_base_dir() -> str:
return base_dir
def edit_note(base_dir: str, note_date: date):
def edit_note(base_dir: str, note_date):
"""
Creates the required directories and opens a text editor
so that the user can describe the day.
@ -46,5 +53,21 @@ def edit_note(base_dir: str, note_date: date):
print("Your editor returned non-zero exit code")
def backup(base_dir: str, current_date):
"""
Creates an archive with all notes
"""
date_str = f'{current_date.year}-{current_date.month:02}-{current_date.day:02}'
archive_path = os.path.expanduser(f"~/histd-{date_str}.tar.xz")
cmd = ["tar", "cfJ", archive_path, "."]
try:
subprocess.run(cmd, check=True, cwd=base_dir)
print(f'Saved to {archive_path}')
except FileNotFoundError:
print("Error: I can't find tar program")
except subprocess.CalledProcessError:
print("Archiver returned non-zero exit code")
if __name__ == '__main__':
main()

View file

@ -34,10 +34,9 @@ sudo curl -o /usr/local/bin/histd "https://raw.githubusercontent.com/ordinary-de
To create a new note, you can simply type `histd` in the terminal.
## Backup
To create an archive of all notes, run the following commands:
```sh
cd ~/.local/share
tar -cJf histd.tar.xz histd
To create an archive of all notes, run the following command:
```bash
histd backup
```
## Merge all notes