diff --git a/subjects/devops/backup_manager/README.md b/subjects/devops/backup_manager/README.md index 0e5aa17fc..9397d01a8 100644 --- a/subjects/devops/backup_manager/README.md +++ b/subjects/devops/backup_manager/README.md @@ -8,12 +8,72 @@ You will create two scripts that will manage and perform scheduled backups. The script `backup_manager.py` will orchestrate the backup service and if necessary update the file `backup_schedules.txt`. In order to do so it will accept the following command-line arguments: -- `start`: run `backup_service.py` in the background. -- `stop`: stop the process `backup_service.py`. -- `create [schedule]`: add a new backup schedule in `backup_schedules.txt`. -- `delete [index]`: delete the backup schedule at line `index` (starting by 0) in `backup_schedules.txt`. -- `list`: print the scheduled backups in `backup_schedules.txt`, adding an index before each schedule. -- `backups`: list the backups files in `./backups`. +- `start`: + + - Runs `backup_service.py` in the background. + - Checks if the service is already running. + - If the service is already running and you try to run it again it should send an error message to the log file. + +- `stop` does the same as start but in order to sop or kill the process `backup_service.py`. +- `create [schedule]`: + + - Adds a new backup schedule in `backup_schedules.txt`. + - This schedule will have a specific format which is `"file_name;hour:minutes;backup_file_name"` + - If the schedule format is wrong, an error message should be sent to the log file. + +```bash +$ python3 ./backup_manager.py create "wrong_format" +$ cat logs/backup_manager.log +[14/02/2023 15:07] Error: invalid schedule format +``` + + - If the schedule format is correct and the schedule is created successfully, you should also send a the log file. + +```bash +$ python3 ./backup_manager.py create "testing;15:44;backup_testing" +$ cat logs/backup_manager.log +[14/02/2023 15:44] Schedule created +``` + +- `list`: + - Prints the scheduled backups in `backup_schedules.txt`, adding an index before each schedule. + - Sends a message to the logs saying that the list is being accessed. + +```bash +$ python3 ./backup_manager.py list +0: testing;15:46;backup_test +$ cat logs/backup_manager.log +[14/02/2023 15:46] Show schedule list +``` + +- `delete [index]`: + + - Delete the backup schedule at line `index` (starting by 0) in `backup_schedules.txt`. + - If the schedule is deleted, you must send a message to the log file. + - If the `index` does not exist or isn't valid, you must send an error message to the log file. + +```bash +$ python3 ./backup_manager.py delete 1 +0: testing; 15:54;backup_testing +$ cat logs/backup_manager.log +[14/02/2023 15:54] Error: index does not exist +[14/02/2023 15:54] Error: invalid index +[14/02/2023 15:54] Schedule deleted +``` + +- `backups`: + + - List the backups files in `./backups`. + - If the `backups` folder is empty it should print an error message informing the user. + - If the `backups` folder is not found it should be sent an error message to the log. + - If it works correctly it should send a messafe to the lo file informing the user. + +```bash +$ python3 ./backup_manager.py backups +backup_testing.tar +$ cat logs/backup_manager.log +[14/02/2023 16:05] Backup list +``` #### Second script: backup_service.py @@ -102,6 +162,7 @@ backup_test.tar office_docs.tar personal_data.tar ### Hints +- Familiarize yourself with the Python, including the use of modules such as `subprocess` and `shlex`, and file `I/O` operations. - To run a script from another python script you could use `subprocess.Popen` with the flag `start_new_session=True`. - To kill a process you should find its process id and call `os.kill`. - Play with the command `ps -A -f`, it will show a list of all active processes with the arguments attached to them and their process ids. @@ -113,4 +174,8 @@ backup_test.tar office_docs.tar personal_data.tar ### References - [Error handling in Python](https://docs.python.org/3.10/tutorial/errors.html) -- [Spawn a subprocess in Python](https://docs.python.org/3.10/library/subprocess.html) \ No newline at end of file +- [Spawn a subprocess in Python](https://docs.python.org/3.10/library/subprocess.html) +- [Simple lexical analysis, shlex](https://docs.python.org/3/library/shlex.html) +- [Reading and writing files](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files) +- [Set handlers for asynchronous events, signal](https://docs.python.org/3/library/signal.html) +- [Try and Except](https://pythonbasics.org/try-except/) diff --git a/subjects/devops/backup_manager/audit/README.md b/subjects/devops/backup_manager/audit/README.md new file mode 100644 index 000000000..d43edd433 --- /dev/null +++ b/subjects/devops/backup_manager/audit/README.md @@ -0,0 +1,131 @@ +#### Functional + +###### Can you confirm that the `backup_manager.py` and `backup_service.py` are present? + +##### Run the following command `python3 ./backup_manager.py create "test2;18:15;backup_test2"` + +###### Can you confirm that the `backup_schedules.txt` was created? + +##### Run the following command `cat backup_schedules.txt`. + +```bash +$ cat backup_schedules.txt +test2;18:15;backup_test2 +``` + +###### Was the schedule created properly, with the same format as the example above? + +##### Run the following command `python3 ./backup_manager.py stop`. + +###### Can you confirm that the `logs` folder was created and inside of it is the `backup_manager.log`? + +##### Now run the command `cat logs/backup_manager.log`. + +```bash +$ cat logs/backup_manager.log +[13/02/2023 17:14] Error: cannot kill the service +``` + +###### Can you confirm that the `backup_manager.log` file contains an error like the one above, stating that the service is already stopped? + +##### Create a couple more backup schedules using the command `python3 ./backup_manager.py create "file;HH:MM;backup_name"` + +##### Run the command `python3 ./backup_manager.py list`. + +```bash +$ python3 ./backup_manager.py list +0: test2;18:15;backup_test2 +1: test3;18:16;backup_test3 +2: test4;18:17;backup_test4 +``` + +###### Did you get a result like the one above, with the schedule you created attached with an index at the beginning, on your terminal? + +##### Run the command `python3 ./backup_manager.py delete 1`. + +```bash +$ python3 ./backup_manager.py list +0: test2;18:15;backup_test2 +1: test4;18:17;backup_test4 +``` + +###### Was the task with the index `1` removed from the list? Use `python3 ./backup_manager.py list`to. + +##### Verify that the following command runs without errors: `python3 backup_manager.py start`. + +###### Can you confirm that the `backup_service.py` process is running by using the command `ps -ef | grep backup_service`? + +##### Now run the command `python3 backup_manager.py start` again. + +```bash +$ python3 ./backup_manager.py start +$ cat logs/backup_manager.log +[13/02/2023 17:14] Error: service already running +``` + +###### Can you confirm bu running the command `cat logs/backup_manager.log` if the log file contains an error like the one above, stating that the service is already running? + +##### Run `python3 backup_manager.py stop` and delete everything that was created until now, the `logs` folder and the `backup_schedules.txt`. Lets create the backup manually. + +##### Run the following command by order: + + 1- mkdir testing; cd testing; touch 1 2 3; cd ../ + 2- python3 ./backup_manager.py create "testing;"Current_hour";backup_test". + 3- python3 ./backup_manager.py start + 4- python3 ./backup_manager.py backups + +```bash +$ mkdir testing; cd testing; touch 1 2 3; cd ../ +$ ls +backup_manager.py backup_service.py testing/ +$ python3 ./backup_manager.py create "testing;18:21;backup_test" +$ python3 ./backup_manager.py list +0: testing;18:21;backup_test +$ python3 ./backup_manager.py start +$ python3 ./backup_manager.py backups +backup_test.tar +$ ls +backup_manager.py backups/ backup_schedules.txt backup_service.py logs/ testing/ +$ cat logs/backup_manager.log +[13/02/2023 18:20] Schedule created +[13/02/2023 18:20] Show schedule list +[13/02/2023 18:21] Backup list +$ cat backup_schedules.txt +testing;18:21;backup_test +$ cat logs/backup_service.log +[13/02/2023 18:21] Backup done for testing in backups/backup_test.tar +``` + +##### Can you confirm that the backup was created successfully? Follow the example above. + +### Error handling + +##### Verify error handling for incorrect commands and incorrect arguments. For example, confirm that an error message is displayed when attempting to run the command `python3 backup_manager.py invalid_command`. + +```bash +$ python3 backup_manager.py invalid_command +Error: unknown instruction +``` + +###### Was the error message displayed? + +##### Run `python3 backup_manager.py stop` and delete everything that was created until now, the `logs` folder and the `backup_schedules.txt`. After that run the following commands: + + 1- python3 backup_manager.py stop + 2- python3 ./backup_manager.py create "wrong_format" + 3- python3 ./backup_manager.py backups + 4- python3 ./backup_manager.py start + 5- python3 ./backup_manager.py start + +```bash +$ cat logs/backup_manager.log +[14/02/2023 15:07] Error: cannot kill the service +[14/02/2023 15:07] Error: invalid schedule format +[14/02/2023 15:08] Error: [Errno 2] No such file or directory: "./backups" +[14/02/2023 15:08] Error: service already running +$ cat logs/backup_service.log +[14/02/2023 15:11] Error: cannot open backup_schedules + +``` + +###### Can you confirm that all error messages have been saved in the log files like in the example above?