As part of the python program, we were responsible for carrying out the KDNA project. This is the official repository for the DO 2023-2026 python CLI backup project.
KDNA is a python application for automating and generating document backups on remote servers. The software works as follows :
First, the application is installed on a local machine by a user with rights to that machine. Once the software has been installed, users can initialize their configurations.
This configuration is made up of two parts:
- Servers, which are the remote servers to which the user has access.
- Backups, which are instantiated by the user and thus depend on a previously configured server.
Backups, thus created, target a directory or a file, which, on the remote server, will be saved at the root of the user's repository (the one selected during server configuration) on his /home
directory in a kdna
folder.
Backups can be automated to run recurrently according to their configuration, and are also encrypted by default for security reasons.
In order to run the project you will need an SSH key, with no passphrase. This will allow us to connect to the server.
-
Without cloning the project run the following line in your terminal :
pip install -e "git+https://github.com/somoslosdorados/KDNA.git/#egg=kdna"
To create and initialize the kdna.conf file :
kdna server init
-
You can also install the KDNA project with the following lines :
git clone https://github.com/somoslosdorados/KDNA.git
After cloning the project, you must install all dependencies.
cd KDNA
pip install .
To create and initialize the kdna.conf file :
kdna server init
- See this link if you have this error : https://www.makeuseof.com/fix-pip-error-externally-managed-environment-linux/.
poetry run pytest
sphinx-apidoc -f -o docs/source kdna/
sphinx-build -M html docs/source/ docs/build/
- click # Parseur
- fabric # SSH client
- pycryptodome # Encrypt tool
- pylint # Linter
- mypy # Type checker
- pytest # Test framework
- tox # Test runner
- tox-gh-actions # Tox github action
- sphinx # Documentation generator
- sphinx-rtd-theme # Read the docs theme
- sphinx-autoapi # Auto documentation generator
- m2r2 # Markdown to reStructuredText converter
- pydocstyle # Docstring style checker
- chardet # Encoding detector
Once you've finished the installation, you should be able to create local server backups, as well as automatic backups.
In the following steps we will see how the project works.
Here you can find all the command you need to manage the application.
Get the information you need to use each command
kdna <command> --help
kdna <command> <subcommand> --help
Managing a backup server
kdna server <command> [options] [arguments]
-
To initialise config file.
kdna server init
-
To import server from
.ssh/config
file.kdna server ssh_import
-
To get status of the server.
kdna server status <alias>
Argument Type Description Required String alias True -
Adding a backup server.
kdna server add -i <id_server> -ad <connection_label> -a <alias_server> -r <repo_backup> -p <port_server>
kdna server add -i 1 -ad kdna@162.38.112.110 -a alias -r /my/repository/backup/ -p 22
Options Type Description Required -i String id_server True -ad String connection label True -a String alias True -r String backup repository True -p String port True -e Boolean encrypt False -
Remove a backup server
kdna server delete -a <alias_server> -i <id_server>
kdna server delete -i 1
Options Type Description Required -i String id_server False -a String alias False Please note: Only one of the two options must be chosen.
-
Updating a server
kdna server update <alias_server> -c <new_credentials> -p <new_port_server> -ad <new_address> -a <new_alias>
kdna server update alias_server -c new_credentials
Options Type Description Required String alias True -r String new_repo False -p String new_port_serveur False -ad String new_address False -a String new_alias False -e String encrypt False -
List backup servers:
kdna server list <project_name>
kdna server list project
Options Type Description Required String project_name True
Manage regular backups
kdna auto-backup <command> [options] [arguments]
-
Create a regular backup
kdna auto-backup create -i <id_backup> -n <backup_name> -t <tag> <cron_schedule> <custom_schedule> -d <start_date> -s <id_server> -p <path_file/file_backup>
kdna auto-backup create -i 1 -n backup -t tag -d 2021-01-01 -s 1 -p /home
Options Type Description Required -i String id_backup True -n String backup_name True -t String tag True String cron_schedule True String custom_schedule False -d String start_date True -s String id_server True -p String path_backup True -
Delete a regular backup
kdna auto-backup delete -i <id_backup>
kdna auto-backup delete -i 1
Options Type Description Required -i String id_server True -
Update a regular backup
kdna auto-backup update -i <id_backup> -t <tag> <tag_updated> cron_schedule <<custom_schedule>> -d <start_date> -p <path_backup>
kdna auto-backup update -i 1 -d 2021-01-01 -p /home
Options Type Description Required -i String id_backup True -t String tag & tag updated False String cron_schedule False String custom_schedule False -d String start_date False -p String path_backup False NOTE : you can't change the server id, as the backup is linked to it.
-
List regular backups
kdna auto-backup list
Managing manual backups
kdna backup <command> [options] [arguments]
-
Create a backup
kdna backup add <project_name> <local_path_backup> <tag> [--prefix <must_generate_an_unique_tag>]
kdna backup add project /home v1
or
kdna backup add project /home v1 --prefix true
Options Type Description Required String project name True String path to backup True String tag name True Boolean must generate a unique tag ? false -
Delete a backup
kdna backup delete -t <path:tag>
kdna backup delete -t /home:tag
Options Type Description Required -t String path:tag True -
Restore a backup
kdna backup restore -t <name:tag> <path_backup>
kdna backup restore -t backup:tag /home
Options Type Description Required -t String name:tag True String path_backup False -
List backups
kdna backup list
Backup encryption management
kdna encrypt <command> [options]
-
Generate an encryption key
kdna encrypt keygen <path>
kdna encrypt keygen /home
Options Type Description Required String path True -
Activate encryption
kdna encrypt activate
-
Deactivate encryption
kdna encrypt deactivate
Manage tags
kdna tag <command> [options]
-
Create a tag
kdna tag add -t <tag> -p <project_name> -f <file>
kdna tag -t tag -p project -f a_file
Options Type Description Required -t String tag True -p String project True -f String file True -
List tags
kdna tag list
This file contains all the configuration that we want to use.
It will be automatically created in ~/.kdna/kdna.conf
if it is not present in the project. The file is structured with the following tags:
[servers]
[auto-backups]
After adding servers/auto-backup, the knda.conf
file will contain the following data in order:
[servers]
id, address, path, port, alias, encrypted
[auto-backups]
id, frequency, name, timestamp, id_server, path
- id: server ID
- address: username@server address
- path: path to the SSH key
- port: port address
- alias: server tag
- encrypted: boolean indicating whether the backups for this server are encrypted
- id: auto-backup ID
- frequency: frequency of auto-backup
- name: name of the auto-backup
- timestamp: date of the last auto-backup
- id_server: ID of the server linked to the auto-backup
- path: local path of the auto-backup
Here's an example of what the file should look like:
[servers]
S4, test@debian12.local, /path, 5432, hello, true
[auto-backups]
B2, daily, fezf, 2022-09-02, S2, /home/backup
B3, daily, yes, 2021-09-02, S4, /home/backup
B4, monthly, okay, 2021-01-01, S4, /home/backup
Follow the instructions for the user option 2 installation then you can follow here.
In this project, we use the py venv
virtual environment. Here is the official documentation : https://docs.python.org/3/library/venv.html
Let's create the virtual environment by installing poetry.
apt install python3-poetry
The CI (Continuous Integration) will activate automatically each time a push or a pull request will append.
Inside the pipeline we have a job, named build, who will run on the matrix.platform
.
The platform will define the environnement where we are running the tests.
Here we are testing on three different platforms :
[ubuntu-latest, macos-latest, windows-latest]
.
On three different versions : [3.10.11, 3.11.6, 3.12.0]
.
This brings us to a total of nine test environements (3x3).
-
- uses: actions/checkout@v1
: we retrieve the project code, the equivalent of doing agit clone
. -
- name: Set up Python ${{ matrix.python-version }}
: we set up python. -
- name: Install dependencies
: we install all the poetry dependencies. -
- name: Test with tox
: we run poetry with the tox testing, using this command :poetry run tox run-parallel
.
poetry run tox run-parallel
#run the pipeline with a specific env
poetry run tox run-parallel -e (env)