DevOps Scripting SQL Cheat Sheet
DevOps Scripting SQL Cheat Sheet
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 .