-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.sh
More file actions
150 lines (106 loc) · 3.46 KB
/
commands.sh
File metadata and controls
150 lines (106 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash
# Lab 10 - Migration and Rollback Runbook
# Commands executed during the lab
# 1. Update system packages
sudo apt update
# 2. Install required tools
sudo apt install -y postgresql postgresql-contrib git
# 3. Start PostgreSQL service
sudo systemctl start postgresql
sudo systemctl enable postgresql
# 4. Create working directory
mkdir -p ~/migration-lab
cd ~/migration-lab
pwd
# 5. Initialize Git repository
git init
git config user.name "Lab User"
git config user.email "lab@example.com"
# 1. Switch to postgres user and create database
sudo -u postgres psql -c "CREATE DATABASE inventory_db;"
sudo -u postgres psql -c "CREATE USER labuser WITH PASSWORD 'labpass123';"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE inventory_db TO labuser;"
sudo -u postgres psql -d inventory_db -c "GRANT ALL ON SCHEMA public TO labuser;"
# 2. Create environment configuration file
nano db_config.sh
# Contents of `db_config.sh`
chmod +x db_config.sh
source db_config.sh
# Step 2: Create Migration Directory Structure
mkdir -p migrations/up
mkdir -p migrations/down
mkdir -p migrations/validation
mkdir -p logs
# Step 3: Build Migration Template Script
nano migrate.sh
# Contents of `migrate.sh`
chmod +x migrate.sh
# Step 4: Create Migration Tracking System
nano migrations/up/000_init_tracking.sql
# Contents of `migrations/up/000_init_tracking.sql`
nano migrations/down/000_init_tracking.sql
# Step 5: Create Sample Migration
nano migrations/up/001_create_products.sql
# Contents of `migrations/up/001_create_products.sql`
nano migrations/down/001_create_products.sql
# Step 6: Create Validation Script
nano migrations/validation/001_validate_products.sql
# Step 1: Build Pre-Migration Checklist Script
nano pre_migration_check.sh
# Contents of `pre_migration_check.sh`
chmod +x pre_migration_check.sh
# Step 2: Create Backup and Restore Scripts
nano backup_database.sh
# Contents of `backup_database.sh`
chmod +x backup_database.sh
# Contents of `backup_database.sh`
nano restore_database.sh
# Contents of `restore_database.sh`
chmod +x restore_database.sh
# Step 3: Create Rollback Procedure Document
nano ROLLBACK_RUNBOOK.md
# Step 4: Create Migration Execution Runbook
nano MIGRATION_RUNBOOK.md
# Step 5: Create Validation Test Suite
nano validate_migration.sh
# Contents of `validate_migration.sh`
chmod +x validate_migration.sh
# 1. Test database connection
source db_config.sh
psql -c "SELECT version();"
# 2. Initialize tracking system
psql -f migrations/up/000_init_tracking.sql
psql -c "SELECT * FROM migration_history;"
# 3. Test backup script
./backup_database.sh
ls -lh backups/
# 4. Execute sample migration
psql -f migrations/up/001_create_products.sql
psql -c "\d products"
# 5. Test rollback
psql -f migrations/down/001_create_products.sql
psql -c "\d products"
# 1. Check all files created
ls -R migrations/
# 1. Check all files created
cat MIGRATION_RUNBOOK.md
# Success Criteria
cat ROLLBACK_RUNBOOK.md
# 2. Verify scripts are executable
ls -l *.sh
# 3. Test pre-migration checks
./pre_migration_check.sh
# Run the migration wrapper
./migrate.sh up 001_create_products.sql
# Run validation suite
./validate_migration.sh 001_create_products.sql
# Check migration history after wrapper execution
psql -c "SELECT * FROM migration_history ORDER BY executed_at DESC;"
# Issue: Cannot connect to PostgreSQL
sudo systemctl status postgresql
# Issue: Permission denied on scripts
chmod +x *.sh
# Issue: Permission denied on scripts
ls -l
# Issue: Backup file not created
df -h