0% found this document useful (0 votes)
9 views4 pages

DevOps Scripting SQL Cheat Sheet

This document is a cheat sheet for freshers covering essential DevOps scripting and SQL basics. It includes examples of Bash, Python, and PowerShell scripting for file operations, automation, and AWS integration using Boto3. Additionally, it provides fundamental SQL commands for creating, inserting, selecting, updating, and deleting data in a database.

Uploaded by

titleofyour123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

DevOps Scripting SQL Cheat Sheet

This document is a cheat sheet for freshers covering essential DevOps scripting and SQL basics. It includes examples of Bash, Python, and PowerShell scripting for file operations, automation, and AWS integration using Boto3. Additionally, it provides fundamental SQL commands for creating, inserting, selecting, updating, and deleting data in a database.

Uploaded by

titleofyour123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DevOps + Scripting + SQL Cheat Sheet for Freshers

Bash Scripting Basics

# For loop

for i in 1 2 3

do

echo "Number $i"

done

# If-else

if [ -f "[Link]" ]; then

echo "File exists"

else

echo "File not found"

fi

# File Operations

touch [Link] # Create file

echo "Hello" > [Link] # Write to file

cat [Link] # Show file content

rm [Link] # Delete file

# Automation Scripts

# Backup

cp /var/log/syslog /backup/syslog-$(date +%F).log

# Start/Stop Services

sudo systemctl start nginx

sudo systemctl stop nginx

Python Scripting for DevOps


DevOps + Scripting + SQL Cheat Sheet for Freshers

# Loop and condition

for i in range(1, 4):

print(f"Number {i}")

import os

if [Link]("[Link]"):

print("File exists")

# File operations

with open("[Link]", "w") as f:

[Link]("Hello")

print(open("[Link]").read())

[Link]("[Link]")

Python + Boto3 for AWS

# Upload file to S3

import boto3

s3 = [Link]('s3')

s3.upload_file('[Link]', 'your-bucket', '[Link]')

# Launch EC2 instance

ec2 = [Link]('ec2')

ec2.create_instances(

ImageId='ami-0abcdef1234567890',

MinCount=1, MaxCount=1,

InstanceType='[Link]',

KeyName='your-key'

)
DevOps + Scripting + SQL Cheat Sheet for Freshers

# List EC2 instances

ec2 = [Link]('ec2')

for res in ec2.describe_instances()['Reservations']:

for inst in res['Instances']:

print(inst['InstanceId'], inst['State']['Name'])

PowerShell Basics

# Loop

foreach ($i in 1..3) { Write-Output "Number $i" }

# If-else

if (Test-Path "[Link]") {

Write-Output "File exists"

} else {

Write-Output "File not found"

# File operations

New-Item [Link] # Create file

Set-Content [Link] "Hi" # Write to file

Get-Content [Link] # Read file

Remove-Item [Link] # Delete file

SQL Basics for DevOps Interviews

-- Create Table

CREATE TABLE employees (

id INT PRIMARY KEY,

name VARCHAR(50),
DevOps + Scripting + SQL Cheat Sheet for Freshers

department VARCHAR(50)

);

-- Insert Data

INSERT INTO employees (id, name, department) VALUES (1, 'Geena', 'IT');

-- Select Data

SELECT * FROM employees;

SELECT name FROM employees WHERE department = 'IT';

-- Update Data

UPDATE employees SET department = 'HR' WHERE id = 1;

-- Delete Data

DELETE FROM employees WHERE id = 1;

Common questions

Powered by AI

Use Python's context manager to open the file: `with open("file.txt", "w") as f: f.write("Hello")`. Check file existence with `if os.path.exists("file.txt"):` and open the file for reading: `print(open("file.txt").read())` to display its content .

Use Boto3's EC2 resource; create instances with `ec2.create_instances()`, manage them with `ec2.describe_instances()`, and terminate using `ec2.terminate_instances(InstanceIds=[...])`. This provides full control over EC2 lifecycle, aiding automated scaling and cleanup .

PowerShell scripts facilitate automated tasks and integrate well with Windows environments, enhancing efficiency. However, managing AWS resources involves security risks, requiring strict IAM roles and script integrity checks to prevent unauthorized access and potential resource mismanagement .

Create the table using `CREATE TABLE employees ( id INT PRIMARY KEY, name VARCHAR(50), department VARCHAR(50) );`. Insert data with `INSERT INTO employees (id, name, department) VALUES (1, 'Geena', 'IT');`. Select specific department data using `SELECT name FROM employees WHERE department = 'IT';` .

First, create the file by using the command `touch file.txt` and then write the specific string using `echo "Hello" > file.txt`. Verify its existence by using an if-else statement: `if [ -f "file.txt" ]; then echo "File exists"; else echo "File not found"; fi` .

Using PowerShell with AWS CLI, first install and configure AWS CLI. Then, execute the script: `aws ec2 describe-instances --query "Reservations[*].Instances[*].[InstanceId,State.Name]" --output table` to list all EC2 instances and their state .

To automate a daily log backup, use the command `cp /var/log/syslog /backup/syslog-$(date +%F).log`. This copies the syslog file to a backup directory and appends the current date in YYYY-MM-DD format to the filename .

Write a backup script using the command `cp /var/log/syslog /backup/syslog-$(date +%F).log`. Schedule it with cron by editing the crontab `crontab -e` and adding the entry `0 2 * * * /path/to/backup-script.sh` to run daily at 2 AM .

Using Boto3 automates infrastructure as code. Upload files to S3 with `s3.upload_file('file.txt', 'your-bucket', 'file.txt')`, ensuring data accessibility and durability. Launch EC2 instances with `ec2.create_instances()` to efficiently scale resources with minimal manual intervention, improving reliability and consistency of deployments .

Use Python's `os` module: check for existence with `if os.path.exists("file.txt"):` and delete with `os.remove("file.txt")` if the file exists. This approach allows automation of file system operations in DevOps .

You might also like