Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 0x0B-ssh/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
- What is SSH
- How to create an SSH RSA key pair
- How to connect to a remote host using an SSH RSA key pair
- The advantage of using #!/usr/bin/env bash instead of /bin/bash
- The advantage of using #!/usr/bin/env bash instead of /bin/b
19 changes: 13 additions & 6 deletions 0x14-mysql/4-mysql_configuration_primary
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
bind-address = 0.0.0.0
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Distinguish servers in a replication setup
#bind-address = 127.0.0.1
server-id = 1
# MySQL's Binary Log File
log_bin = /var/log/mysql/mysql-bin.log
# Database we want to replicate
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

binlog_do_db = tyrell_corp
13 changes: 5 additions & 8 deletions 0x14-mysql/4-mysql_configuration_replica
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Distinguish servers in a replication setup
# bind-address = 127.0.0.1
server-id = 2
# MySQL's Binary Log File
log_bin = /var/log/mysql/mysql-bin.log
# Database we want to replicate
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
binlog_do_db = tyrell_corp
# Defines the location of the replica's relay log
relay-log = /var/log/mysql/mysql-relay-bin.log
12 changes: 3 additions & 9 deletions 0x14-mysql/5-mysql_backup
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
#!/usr/bin/env bash
# backup and compress my databases
# This script generates a MySQL dump and creates a compressed archive of it

# variables
day=$(date +"%d")
month=$(date +"%m")
year=$(date +"%Y")
file_name="$day-$month-$year.tar.gz"

mysqldump --all-databases -u root --password="$1" > backup.sql
tar -czvf "$file_name" backup.sql
mysqldump -uroot -p"$1" --all-databases > backup.sql
tar -czf $(date +%d-%m-%Y).tar.gz backup.sql
139 changes: 1 addition & 138 deletions 0x14-mysql/README.md
Original file line number Diff line number Diff line change
@@ -1,138 +1 @@
# 0x14. MySQL

<p align="center">
<img src="https://s3.amazonaws.com/intranet-projects-files/holbertonschool-sysadmin_devops/280/KkrkDHT.png"
</p>

## Resource

- [What is a database](https://searchdatamanagement.techtarget.com/definition/database)
- [What is a database primary/replicate cluster](https://www.digitalocean.com/community/tutorials/how-to-choose-a-redundancy-plan-to-ensure-high-availability#sql-replication)
- [MySQL primary/replicate setup](https://www.digitalocean.com/community/tutorials/how-to-set-up-replication-in-mysql)
- [Build a robust database backup strategy](https://www.databasejournal.com/ms-sql/developing-a-sql-server-backup-strategy/)
- [Privileges Provided by MySQL](https://dev.mysql.com/doc/refman/8.0/en/privileges-provided.html#priv_replication-client) (***Replication Client***)
- [Creating User for Replication](https://dev.mysql.com/doc/refman/8.0/en/replication-howto-repuser.html)
- [Setting up replicas](https://dev.mysql.com/doc/refman/5.7/en/replication-setup-replicas.html) (***MySQL 5.7.x***)

## Tasks

<details>
<summary>0. Install MySQL</summary><br>
<a href='https://postimages.org/' target='_blank'><img src='https://i.postimg.cc/wMPwtg5K/image.png' border='0' alt='image'/></a>
</details>

<details>
<summary>1. Let us in!</summary><br>

<a href='https://postimages.org/' target='_blank'><img src='https://i.postimg.cc/zB1QFncd/image.png' border='0' alt='image'/></a>
```sh
mysql> CREATE USER 'holberton_user'@'localhost' IDENTIFIED BY 'projectcorrection280hbtn';
mysql> GRANT REPLICATION CLIENT ON *.* to 'holberton_user'@'localhost';
mysql> FLUSH PRIVILEGES;
```

</details>

<details>
<summary>2. If only you could see what I've seen with your eyes</summary><br>

<a href='https://postimages.org/' target='_blank'><img src='https://i.postimg.cc/sgDm766T/image.png' border='0' alt='image'/></a>
```sh
mysql> CREATE DATABASE tyrell_corp;
mysql> USE tyrell_corp;
mysql> CREATE TABLE nexus6 (id INT, name VARCHAR(256));
mysql> INSERT INTO nexus6 (id, name) VALUES ('1', 'Leon');
mysql> GRANT SELECT ON tyrell_corp.nexus6 TO 'holberton_user'@'localhost';
```

</details>

<details>
<summary>3. Quite an experience to live in fear, isn't it?</summary><br>

<a href='https://postimages.org/' target='_blank'><img src='https://i.postimg.cc/D0CmW3vT/image.png' border='0' alt='image'/></a>
```sh
msql> CREATE USER 'replica_user'@'%' IDENTIFIED BY 'password';
mysql> GRANT SELECT ON mysql.user TO 'holberton_user'@'localhost';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'replica_user'@'%';
```

</details>

<details>
<summary>4. Setup a Primary-Replica infrastructure using MySQL</summary><br>

<a href='https://postimages.org/' target='_blank'><img src='https://i.postimg.cc/MKBBLGVn/09e83e914f0d6865ce320a47f2f14837a5b190b6.gif' border='0' alt='09e83e914f0d6865ce320a47f2f14837a5b190b6'/></a>
<a href='https://postimages.org/' target='_blank'><img src='https://i.postimg.cc/9fkyDg7k/image.png' border='0' alt='image'/></a>
<a href='https://postimages.org/' target='_blank'><img src='https://i.postimg.cc/Jhhb4DpP/image.png' border='0' alt='image'/></a>
<a href='https://postimg.cc/4nLx8dpx' target='_blank'><img src='https://i.postimg.cc/28mLSL6h/image.png' border='0' alt='image'/></a>

+ [MySQL primary configuration](./4-mysql_configuration_primary)
+ [MySQL replica configuration](./4-mysql_configuration_replica)

</details>

<details>
<summary>5. MySQL backup</summary><br>

[![IMAGE ALT TEXT HERE](https://i.postimg.cc/3NtKg0gR/verizon.jpg)](https://www.youtube.com/watch?v=ANU-oSE5_hU)
<a href='https://postimages.org/' target='_blank'><img src='https://i.postimg.cc/J7YV5LfG/image.png' border='0' alt='image'/></a>

```sh
ubuntu@03-web-01:~$ ls
5-mysql_backup
ubuntu@03-web-01:~$ ./5-mysql_backup mydummypassword
backup.sql
ubuntu@03-web-01:~$ ls
01-03-2017.tar.gz 5-mysql_backup backup.sql
ubuntu@03-web-01:~$ more backup.sql
-- MySQL dump 10.13 Distrib 5.7.25, for debian-linux-gnu (x86_64)
--
-- Host: localhost Database:
-- ------------------------------------------------------
-- Server version 5.7.25-0ubuntu0.14.04.1

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Current Database: `tyrell_corp`
--

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `tyrell_corp` /*!40100 DEFAULT CHARACTER SET latin1 */;

USE `tyrell_corp`;

--
-- Table structure for table `nexus6`
--

DROP TABLE IF EXISTS `nexus6`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `nexus6` (
`id` int(6) unsigned NOT NULL AUTO_INCREMENT,
`firstname` varchar(30) NOT NULL,
`lastname` varchar(30) NOT NULL,
`email` varchar(50) DEFAULT NULL,
`reg_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
ubuntu@03-web-01:~$
ubuntu@03-web-01:~$ file 01-03-2017.tar.gz
01-03-2017.tar.gz: gzip compressed data, from Unix, last modified: Wed Mar 1 23:38:09 2017
ubuntu@03-web-01:~$
```

+ [Backup script](./5-mysql_backup)

</details>
# Solutions to tasks on MySQL
47 changes: 24 additions & 23 deletions 0x15-api/0-gather_data_from_an_API.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
#!/usr/bin/python3
'''A script that gathers employee name completed
tasks and total number of tasks from an API
'''
"""Accessing a REST API for todo lists of employees"""

import re
import requests
import sys

REST_API = "https://jsonplaceholder.typicode.com"

if __name__ == '__main__':
if len(sys.argv) > 1:
if re.fullmatch(r'\d+', sys.argv[1]):
id = int(sys.argv[1])
emp_req = requests.get('{}/users/{}'.format(REST_API, id)).json()
task_req = requests.get('{}/todos'.format(REST_API)).json()
emp_name = emp_req.get('name')
tasks = list(filter(lambda x: x.get('userId') == id, task_req))
completed_tasks = list(filter(lambda x: x.get('completed'), tasks))
print(
'Employee {} is done with tasks({}/{}):'.format(
emp_name,
len(completed_tasks),
len(tasks)
)
)
if len(completed_tasks) > 0:
for task in completed_tasks:
print('\t {}'.format(task.get('title')))
employeeId = sys.argv[1]
baseUrl = "https://jsonplaceholder.typicode.com/users"
url = baseUrl + "/" + employeeId

response = requests.get(url)
employeeName = response.json().get('name')

todoUrl = url + "/todos"
response = requests.get(todoUrl)
tasks = response.json()
done = 0
done_tasks = []

for task in tasks:
if task.get('completed'):
done_tasks.append(task)
done += 1

print("Employee {} is done with tasks({}/{}):"
.format(employeeName, done, len(tasks)))

for task in done_tasks:
print("\t {}".format(task.get('title')))
24 changes: 24 additions & 0 deletions 0x15-api/1-export_to_CSV.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/python3
"""Accessing a REST API for todo lists of employees"""

import requests
import sys


if __name__ == '__main__':
employeeId = sys.argv[1]
baseUrl = "https://jsonplaceholder.typicode.com/users"
url = baseUrl + "/" + employeeId

response = requests.get(url)
username = response.json().get('username')

todoUrl = url + "/todos"
response = requests.get(todoUrl)
tasks = response.json()

with open('{}.csv'.format(employeeId), 'w') as file:
for task in tasks:
file.write('"{}","{}","{}","{}"\n'
.format(employeeId, username, task.get('completed'),
task.get('title')))
29 changes: 29 additions & 0 deletions 0x15-api/2-export_to_JSON.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/python3
"""Accessing a REST API for todo lists of employees"""

import json
import requests
import sys


if __name__ == '__main__':
employeeId = sys.argv[1]
baseUrl = "https://jsonplaceholder.typicode.com/users"
url = baseUrl + "/" + employeeId

response = requests.get(url)
username = response.json().get('username')

todoUrl = url + "/todos"
response = requests.get(todoUrl)
tasks = response.json()

dictionary = {employeeId: []}
for task in tasks:
dictionary[employeeId].append({
"task": task.get('title'),
"completed": task.get('completed'),
"username": username
})
with open('{}.json'.format(employeeId), 'w') as filename:
json.dump(dictionary, filename)
20 changes: 20 additions & 0 deletions 0x15-api/2.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"2","Antonette","False","suscipit repellat esse quibusdam voluptatem incidunt"
"2","Antonette","True","distinctio vitae autem nihil ut molestias quo"
"2","Antonette","False","et itaque necessitatibus maxime molestiae qui quas velit"
"2","Antonette","False","adipisci non ad dicta qui amet quaerat doloribus ea"
"2","Antonette","True","voluptas quo tenetur perspiciatis explicabo natus"
"2","Antonette","True","aliquam aut quasi"
"2","Antonette","True","veritatis pariatur delectus"
"2","Antonette","False","nesciunt totam sit blanditiis sit"
"2","Antonette","False","laborum aut in quam"
"2","Antonette","True","nemo perspiciatis repellat ut dolor libero commodi blanditiis omnis"
"2","Antonette","False","repudiandae totam in est sint facere fuga"
"2","Antonette","False","earum doloribus ea doloremque quis"
"2","Antonette","False","sint sit aut vero"
"2","Antonette","False","porro aut necessitatibus eaque distinctio"
"2","Antonette","True","repellendus veritatis molestias dicta incidunt"
"2","Antonette","True","excepturi deleniti adipisci voluptatem et neque optio illum ad"
"2","Antonette","False","sunt cum tempora"
"2","Antonette","False","totam quia non"
"2","Antonette","False","doloremque quibusdam asperiores libero corrupti illum qui omnis"
"2","Antonette","True","totam atque quo nesciunt"
31 changes: 31 additions & 0 deletions 0x15-api/3-dictionary_of_list_of_dictionaries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/python3
"""Accessing a REST API for todo lists of employees"""

import json
import requests
import sys


if __name__ == '__main__':
url = "https://jsonplaceholder.typicode.com/users"

response = requests.get(url)
users = response.json()

dictionary = {}
for user in users:
user_id = user.get('id')
username = user.get('username')
url = 'https://jsonplaceholder.typicode.com/users/{}'.format(user_id)
url = url + '/todos/'
response = requests.get(url)
tasks = response.json()
dictionary[user_id] = []
for task in tasks:
dictionary[user_id].append({
"task": task.get('title'),
"completed": task.get('completed'),
"username": username
})
with open('todo_all_employees.json', 'w') as file:
json.dump(dictionary, file)
Loading