DEV Community

Cover image for Python 3 for System Administrators: The Plot!
Sachin Jha
Sachin Jha

Posted on • Edited on

Python 3 for System Administrators: The Plot!

📓 Shell or Python? Should I replace my Shell scripts with Python?

It’s debatable!

That’s what Nick, who has been a mentor for Ajay, added.

It certainly depends, whether you work in Sysadmin, Cloud operations, DevOps, or SRE. But that love for Shell would always be there. We all started with it and it’s the glue!

Ajay: But isn’t Shell scripts slow? In addition, it lacks data structure and gets highly complex with long scripts.

Nick: Maybe, but that’s not the point 😄 There are day-to-day tasks that can be performed in both, say checking application ports, starting/stopping the server, checking the resources consumption/load, backing up your database and exporting it to object storage.

🐍 Python is clean, it feels like writing pseudocode. In other words, it’s easier to write and maintain, provides better error handling, can perform OOP (object-oriented programming), and the best part is argument parsing and modules.

🐚 Shell is something that we all have started with, hence I describe it with words “omnipresence” or “has ubiquity”. Quick filtering or command chaining with Pipe ("|") and CLI utilities like sed, awk, grep, etc.

Alt Text

Ajay: Awesome, let me give it a shot; it would certainly help to add new skills to my knowledge bank, at the very least.

Hence, Ajay- who has been a sysadmin all the while writing scripts in shell, just wants to try Python and see how he can make use of both to add more efficiency to his day-to-day work.

So, this is Ajay’s review on Linux academy’s course for Python which he found out while browsing the internet and it simply said: “Learn or start only with modules and structure that would be useful for you as Sysadmin and not the whole Python at once!”

Ajay can be any person who has spent a good time with shell, and not specially sysadmin. So this is for anyone who just wants to add an element to fun to work and has been wanting to learn Python for a long time!

The entire story/review can be found with the following link, it was a bit long to add here!

Review: https://sachcode.com/tech/linux-academy-review-system-administrators-python/
Acloudguru: https://acloudguru.com/

Note: This is in no way a connection or recommendation from me to try only Linux Academy; I just loved the course and this was an honest review. 
Enter fullscreen mode Exit fullscreen mode

It contains the use-case on who this course is for, my experience on the operations side, and some key takeaways. Each section’s outcome(s), and the overall links to codes, as well as entire Python projects-repos on GitHub that came out as learning.

Some of the fun exercise(s) and key outcomes- snip!

Project:
We have many database servers that we manage, and we want to create a 
single tool that we can use to easily back up the databases to either 
AWS S3 or locally. We would like to be able to:

> Specify the database URL to backup.
> Specify a “driver” (local or s3)
> Specify the backup “destination”. This will be a file path for local 
and a bucket name for s3.
> Depending on the “driver”, create a local backup of the database or 
upload the backup to an S3 bucket.
Enter fullscreen mode Exit fullscreen mode

https://github.com/linuxacademy/content-python-for-sys-admins/tree/master/pgbackup

Usage:
..............
Pass in a full database URL, the storage driver, and destination.
S3 Example w/ bucket name:
$ pgbackup postgres://bob@example.com:5432/db_one --driver s3 backups

Local Example w/ local path:
$ pgbackup postgres://bob@example.com:5432/db_one --driver local \
/var/local/db_one/backups/dump.sql
Enter fullscreen mode Exit fullscreen mode

🖊️ https://sachcode.com/

Top comments (2)

Collapse
 
waylonwalker profile image
Waylon Walker

Love the dialogue.

I find it easy to do simple things in very short bash scripts. They startup and run fast.

For more complicated things its much easier to read python. It's just such a human readable layer of abstraction. It does cost at least 1s or so for startup no matter what. This can be a bit annoying for certain things.

Collapse
 
jhasach profile image
Sachin Jha

I'm glad you liked it Waylon. Yup, that's the overall thing bash is the glue- and we all can get things done faster for those simple filtering and when the implementation needs a structure we can plan to do it on Python :)

Thanks for the discussion!