Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added solution.md for Day-09 : Automate Backup Creation and Rotation with Cron Job #341

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

Amitabh-DevOps
Copy link

@Amitabh-DevOps Amitabh-DevOps commented Dec 2, 2024

This PR adds a shell script backup_with_rotation.sh that automates the process of creating backups for a specified directory and manages backup rotation by retaining only the latest three backups. Key features include:

  • Backup Creation: Generates compressed backups with timestamps for unique identification.
  • Backup Rotation: Deletes older backups beyond the latest three to manage storage efficiently.
  • Automation with Cron Jobs: The backup process is scheduled to run automatically using cron jobs, ensuring periodic backups without manual intervention.

Implementation Details:

  1. Created the backup_with_rotation.sh script to handle backup creation and rotation.
  2. Scheduled the script to run every minute (for testing purposes) using a cron job.
  3. Verified the backup files' creation and rotation in the /home/ubuntu/documents directory.

Testing:

  • The script was tested by running it manually and validating backup creation and rotation.
  • Automation was confirmed by scheduling the script with a cron job and monitoring the results.

This solution ensures efficient backup management and automation, making it suitable for systems requiring frequent data backups.

Summary by CodeRabbit

  • New Features

    • Updated image references in markdown documents for better accessibility.
    • Introduced new scripts for directory creation, backups, user management, and permission handling.
    • Added detailed instructions for automating tasks and managing services.
    • New comprehensive task added for a complete bash script solution encapsulating previous tasks.
  • Bug Fixes

    • Enhanced clarity and structure of documentation to improve user understanding.
  • Documentation

    • Updated content across multiple days to provide clearer explanations and step-by-step guides.

Copy link

coderabbitai bot commented Dec 2, 2024

Walkthrough

The pull request includes updates to multiple markdown documents related to Linux commands and shell scripting for various days. Key changes involve updating image references from GitHub URLs to relative paths, adding new scripts for tasks, restructuring content for clarity, and enhancing explanations of concepts such as file permissions and package management. Several tasks have been introduced or expanded, including detailed instructions for creating backups, managing users, and automating processes with cron jobs.

Changes

File Path Change Summary
2024/day03/solution.md Updated image references from GitHub URLs to relative paths for tasks 1-12; modified tasks 7 and 8 to include separate images for two methods.
2024/day04/solution.md Removed old image links; added new images for tasks 3, 4, and 5 while preserving original content.
2024/day05/solution.md Introduced new scripts: createDirectories.sh and backup.sh; expanded instructions for tasks 3 and 4.
2024/day06/solution.md Added new scripts for managing file permissions and ACLs; enhanced explanations and structure of tasks.
2024/day07/solution.md Changed title; expanded tasks and explanations regarding package management and service management.
2024/day08/solution.md Updated image links for tasks 1-6; added new task 7 with a complete bash script solution.
2024/day09/solution.md Updated title and structure; added detailed instructions for creating a backup script with rotation.

Poem

🐇 In the land of code where scripts do play,
Images now dance in a relative way.
With tasks anew and scripts to share,
Linux commands fill the air!
From backups to users, oh what a sight,
A rabbit's delight in the coding night! 🐇


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 7

🧹 Outside diff range and nitpick comments (3)
2024/day09/solution.md (3)

60-67: Enhance error handling for the zip command

The current error handling could be improved by:

  1. Checking if the source directory exists
  2. Validating if we have write permissions to the target directory
  3. Providing more specific error messages
+    # Check if source directory exists
+    if [ ! -d "$1" ]; then
+        echo "Error: Source directory '$1' does not exist"
+        return 1
+    fi
+
+    # Check write permissions
+    if [ ! -w "$(dirname "$backup_file")" ]; then
+        echo "Error: No write permission in target directory"
+        return 1
+    fi
+
     zip -r "$backup_file" "$1"
     if [ $? -eq 0 ]; then
         echo "Backup created: ${backup_file}"
     else
-        echo "Error: Failed to create backup."
+        echo "Error: Failed to create backup. Exit code: $?"
         return 1
     fi

79-79: Add error handling for empty directory case

The ls command might fail if there are no zip files. Consider adding error handling for this case.

-        old_backups=$(ls -t *.zip | tail -n +4)
+        # Check if any zip files exist
+        if ls *.zip >/dev/null 2>&1; then
+            old_backups=$(ls -t *.zip | tail -n +4)
+        else
+            echo "No backup files found"
+            return 0
+        fi

144-144: Add logging for cron job execution

For production use:

  1. Consider reducing the frequency to a more reasonable interval
  2. Add logging to track execution history
-*/1 * * * * bash /home/ubuntu/Day-09/backup_with_rotation.sh /home/ubuntu/Day-09 
+# Run daily at midnight and log output
+0 0 * * * bash /home/ubuntu/Day-09/backup_with_rotation.sh /home/ubuntu/Day-09 >> /var/log/backup_rotation.log 2>&1
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 2071066 and 2655fc2.

⛔ Files ignored due to path filters (136)
  • 2024/day03/image/task 1.png is excluded by !**/*.png
  • 2024/day03/image/task 10.png is excluded by !**/*.png
  • 2024/day03/image/task 11.png is excluded by !**/*.png
  • 2024/day03/image/task 12.png is excluded by !**/*.png
  • 2024/day03/image/task 2.png is excluded by !**/*.png
  • 2024/day03/image/task 3.png is excluded by !**/*.png
  • 2024/day03/image/task 4.png is excluded by !**/*.png
  • 2024/day03/image/task 5.png is excluded by !**/*.png
  • 2024/day03/image/task 6.png is excluded by !**/*.png
  • 2024/day03/image/task 66.png is excluded by !**/*.png
  • 2024/day03/image/task 7.png is excluded by !**/*.png
  • 2024/day03/image/task 8.png is excluded by !**/*.png
  • 2024/day03/image/task 9.png is excluded by !**/*.png
  • 2024/day03/image/task1.png is excluded by !**/*.png
  • 2024/day03/image/task10.png is excluded by !**/*.png
  • 2024/day03/image/task11.png is excluded by !**/*.png
  • 2024/day03/image/task12.png is excluded by !**/*.png
  • 2024/day03/image/task2.png is excluded by !**/*.png
  • 2024/day03/image/task3.png is excluded by !**/*.png
  • 2024/day03/image/task4.png is excluded by !**/*.png
  • 2024/day03/image/task5.png is excluded by !**/*.png
  • 2024/day03/image/task6.png is excluded by !**/*.png
  • 2024/day03/image/task7.1.png is excluded by !**/*.png
  • 2024/day03/image/task7.2.png is excluded by !**/*.png
  • 2024/day03/image/task8.1.png is excluded by !**/*.png
  • 2024/day03/image/task8.2.png is excluded by !**/*.png
  • 2024/day03/image/task9.png is excluded by !**/*.png
  • 2024/day04/image/task 1.png is excluded by !**/*.png
  • 2024/day04/image/task 11.png is excluded by !**/*.png
  • 2024/day04/image/task 2.png is excluded by !**/*.png
  • 2024/day04/image/task 3.png is excluded by !**/*.png
  • 2024/day04/image/task 4.png is excluded by !**/*.png
  • 2024/day04/image/task 5.png is excluded by !**/*.png
  • 2024/day04/image/task3.1.png is excluded by !**/*.png
  • 2024/day04/image/task3.2.png is excluded by !**/*.png
  • 2024/day04/image/task4.1.png is excluded by !**/*.png
  • 2024/day04/image/task4.2.png is excluded by !**/*.png
  • 2024/day04/image/task5.1.png is excluded by !**/*.png
  • 2024/day04/image/task5.2.png is excluded by !**/*.png
  • 2024/day05/image/task 1-2.png is excluded by !**/*.png
  • 2024/day05/image/task 1-3.png is excluded by !**/*.png
  • 2024/day05/image/task 1.png is excluded by !**/*.png
  • 2024/day05/image/task 2-1.png is excluded by !**/*.png
  • 2024/day05/image/task 2.png is excluded by !**/*.png
  • 2024/day05/image/task 3-1.png is excluded by !**/*.png
  • 2024/day05/image/task 3.png is excluded by !**/*.png
  • 2024/day05/image/task 4.png is excluded by !**/*.png
  • 2024/day05/image/task1.1.png is excluded by !**/*.png
  • 2024/day05/image/task1.2.png is excluded by !**/*.png
  • 2024/day05/image/task2.1.png is excluded by !**/*.png
  • 2024/day05/image/task2.2.png is excluded by !**/*.png
  • 2024/day05/image/task2.3.png is excluded by !**/*.png
  • 2024/day05/image/task3.1.png is excluded by !**/*.png
  • 2024/day05/image/task3.2.png is excluded by !**/*.png
  • 2024/day05/image/task3.3.png is excluded by !**/*.png
  • 2024/day05/image/task3.4.png is excluded by !**/*.png
  • 2024/day05/image/task3.5.png is excluded by !**/*.png
  • 2024/day05/image/task3.6.png is excluded by !**/*.png
  • 2024/day05/image/task4.1.png is excluded by !**/*.png
  • 2024/day05/image/task4.2.png is excluded by !**/*.png
  • 2024/day06/image/task1.1.png is excluded by !**/*.png
  • 2024/day06/image/task1.2.png is excluded by !**/*.png
  • 2024/day06/image/task1.3.png is excluded by !**/*.png
  • 2024/day06/image/task1.4.png is excluded by !**/*.png
  • 2024/day06/image/task1.png is excluded by !**/*.png
  • 2024/day06/image/task3.1.png is excluded by !**/*.png
  • 2024/day06/image/task3.png is excluded by !**/*.png
  • 2024/day06/image/task4-1.png is excluded by !**/*.png
  • 2024/day06/image/task4.1.1.png is excluded by !**/*.png
  • 2024/day06/image/task4.1.2.png is excluded by !**/*.png
  • 2024/day06/image/task4.2.1.png is excluded by !**/*.png
  • 2024/day06/image/task4.png is excluded by !**/*.png
  • 2024/day06/image/task5-1.png is excluded by !**/*.png
  • 2024/day06/image/task5-2.png is excluded by !**/*.png
  • 2024/day06/image/task5.1.png is excluded by !**/*.png
  • 2024/day06/image/task5.2.1.png is excluded by !**/*.png
  • 2024/day06/image/task5.2.2.png is excluded by !**/*.png
  • 2024/day06/image/task5.3.png is excluded by !**/*.png
  • 2024/day06/image/task5.png is excluded by !**/*.png
  • 2024/day06/image/task6-1.png is excluded by !**/*.png
  • 2024/day06/image/task6.1.png is excluded by !**/*.png
  • 2024/day06/image/task6.2.png is excluded by !**/*.png
  • 2024/day06/image/task6.png is excluded by !**/*.png
  • 2024/day07/image/task1-2.png is excluded by !**/*.png
  • 2024/day07/image/task1.1.png is excluded by !**/*.png
  • 2024/day07/image/task1.2.png is excluded by !**/*.png
  • 2024/day07/image/task1.3.png is excluded by !**/*.png
  • 2024/day07/image/task1.4.png is excluded by !**/*.png
  • 2024/day07/image/task1.png is excluded by !**/*.png
  • 2024/day07/image/task2.1.png is excluded by !**/*.png
  • 2024/day07/image/task3.2.png is excluded by !**/*.png
  • 2024/day07/image/task4.1.png is excluded by !**/*.png
  • 2024/day07/image/task4.2.png is excluded by !**/*.png
  • 2024/day07/image/task4.3.png is excluded by !**/*.png
  • 2024/day07/image/task4.png is excluded by !**/*.png
  • 2024/day07/image/task5-1.png is excluded by !**/*.png
  • 2024/day07/image/task5.1.1.png is excluded by !**/*.png
  • 2024/day07/image/task5.1.2.png is excluded by !**/*.png
  • 2024/day07/image/task5.2.1.png is excluded by !**/*.png
  • 2024/day07/image/task5.2.2.png is excluded by !**/*.png
  • 2024/day07/image/task5.png is excluded by !**/*.png
  • 2024/day07/image/task6-1.png is excluded by !**/*.png
  • 2024/day07/image/task6.1.png is excluded by !**/*.png
  • 2024/day07/image/task6.2.png is excluded by !**/*.png
  • 2024/day07/image/task6.png is excluded by !**/*.png
  • 2024/day07/image/taskj2.png is excluded by !**/*.png
  • 2024/day08/image/task1.1.png is excluded by !**/*.png
  • 2024/day08/image/task1.2.png is excluded by !**/*.png
  • 2024/day08/image/task1.png is excluded by !**/*.png
  • 2024/day08/image/task2.1.png is excluded by !**/*.png
  • 2024/day08/image/task2.2.png is excluded by !**/*.png
  • 2024/day08/image/task2.png is excluded by !**/*.png
  • 2024/day08/image/task3.1.png is excluded by !**/*.png
  • 2024/day08/image/task3.2.png is excluded by !**/*.png
  • 2024/day08/image/task3.png is excluded by !**/*.png
  • 2024/day08/image/task4.1.png is excluded by !**/*.png
  • 2024/day08/image/task4.2.png is excluded by !**/*.png
  • 2024/day08/image/task4.png is excluded by !**/*.png
  • 2024/day08/image/task5.1.png is excluded by !**/*.png
  • 2024/day08/image/task5.2.png is excluded by !**/*.png
  • 2024/day08/image/task5.png is excluded by !**/*.png
  • 2024/day08/image/task6.1.png is excluded by !**/*.png
  • 2024/day08/image/task6.2.png is excluded by !**/*.png
  • 2024/day08/image/task6.png is excluded by !**/*.png
  • 2024/day08/image/task7.1.png is excluded by !**/*.png
  • 2024/day08/image/task7.2.png is excluded by !**/*.png
  • 2024/day09/Screenshot 2024-12-02 213231.png is excluded by !**/*.png
  • 2024/day09/image/bash1.png is excluded by !**/*.png
  • 2024/day09/image/task1-2.png is excluded by !**/*.png
  • 2024/day09/image/task10.png is excluded by !**/*.png
  • 2024/day09/image/task11.png is excluded by !**/*.png
  • 2024/day09/image/task2.png is excluded by !**/*.png
  • 2024/day09/image/task3.png is excluded by !**/*.png
  • 2024/day09/image/task5.png is excluded by !**/*.png
  • 2024/day09/image/task6.png is excluded by !**/*.png
  • 2024/day09/image/task8.png is excluded by !**/*.png
📒 Files selected for processing (7)
  • 2024/day03/solution.md (1 hunks)
  • 2024/day04/solution.md (1 hunks)
  • 2024/day05/solution.md (1 hunks)
  • 2024/day06/solution.md (2 hunks)
  • 2024/day07/solution.md (1 hunks)
  • 2024/day08/solution.md (1 hunks)
  • 2024/day09/solution.md (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • 2024/day04/solution.md
  • 2024/day03/solution.md
🧰 Additional context used
🪛 Markdownlint (0.35.0)
2024/day09/solution.md

3-3: Punctuation: ':'
Trailing punctuation in heading

(MD026, no-trailing-punctuation)


7-7: Punctuation: ':'
Trailing punctuation in heading

(MD026, no-trailing-punctuation)


37-37: Punctuation: ':'
Trailing punctuation in heading

(MD026, no-trailing-punctuation)


154-154: Punctuation: ':'
Trailing punctuation in heading

(MD026, no-trailing-punctuation)

2024/day07/solution.md

3-3: Expected: h2; Actual: h3
Heading levels should only increment by one level at a time

(MD001, heading-increment)


46-46: null
Multiple headings with the same content

(MD024, no-duplicate-heading)


39-39: null
Bare URL used

(MD034, no-bare-urls)


74-74: null
Bare URL used

(MD034, no-bare-urls)

2024/day06/solution.md

47-47: Expected: 0; Actual: 2
Unordered list indentation

(MD007, ul-indent)


91-91: Expected: 0; Actual: 2
Unordered list indentation

(MD007, ul-indent)


96-96: Expected: 0; Actual: 3
Unordered list indentation

(MD007, ul-indent)


103-103: Expected: 0; Actual: 3
Unordered list indentation

(MD007, ul-indent)


35-35: null
Bare URL used

(MD034, no-bare-urls)


98-98: null
Emphasis used instead of a heading

(MD036, no-emphasis-as-heading)


166-166: Expected: 0; Actual: 2
Unordered list indentation

(MD007, ul-indent)


204-204: Expected: 0; Actual: 2
Unordered list indentation

(MD007, ul-indent)

🪛 LanguageTool
2024/day09/solution.md

[uncategorized] ~37-~37: Loose punctuation mark.
Context: ...ith_[rotation.sh`](http://rotation.sh):

#!/bin/bash

<<info
Auth...

(UNLIKELY_OPENING_PUNCTUATION)

---

[uncategorized] ~168-~168: Use a comma before “and” if it connects two independent clauses (unless they are closely connected and short).
Context: ...ures that backups are taken periodically and that older backups are cleaned up to sa...

(COMMA_COMPOUND_SENTENCE_2)

</details>
<details>
<summary>2024/day08/solution.md</summary>

[duplication] ~28-~28: Possible typo: you repeated a word
Context: ...![task2.2](image/task2.2.png)


3. **Variables**
   - Variables in bash are used to store data and can ...

(ENGLISH_WORD_REPEAT_RULE)

---

[duplication] ~61-~61: Possible typo: you repeated a word
Context: ...k5.2](image/task5.2.png)     


6. **Wildcards**
   - Wildcards are special characters used to perform ...

(ENGLISH_WORD_REPEAT_RULE)

</details>
<details>
<summary>2024/day07/solution.md</summary>

[grammar] ~3-~3: The correct preposition appears to be “on”.
Context: ...temctl

### What is a Package Manager in Linux?

In simpler words, a package m...

(IN_WINDOWS)

---

[uncategorized] ~11-~11: Use a comma before ‘but’ if it connects two independent clauses (unless they are closely connected and short).
Context: ...is usually referred to as an application but it could be a GUI application, command ...

(COMMA_COMPOUND_SENTENCE)

---

[uncategorized] ~15-~15: Use a comma before ‘but’ if it connects two independent clauses (unless they are closely connected and short).
Context: ...ers differ based on the packaging system but the same packaging system may have more...

(COMMA_COMPOUND_SENTENCE_2)

</details>
<details>
<summary>2024/day05/solution.md</summary>

[misspelling] ~136-~136: Did you mean “their”?
Context: ...
   
   - Creating users and printing there names : 

   ![task4.1](image/task4.1...

(THERE_THEIR)

</details>

</details>

</details>

<details>
<summary>🔇 Additional comments (2)</summary>

<details>
<summary>2024/day05/solution.md (1)</summary>

`48-82`: **Consider consolidating backup implementations**

This backup implementation is now superseded by the more feature-complete version in day09. Consider removing this version to avoid confusion and maintenance overhead.

</details>
<details>
<summary>2024/day08/solution.md (1)</summary>

`75-131`: **Documentation looks good**

The shell script examples are well-documented and serve their educational purpose effectively.

</details>

</details>

</details>

<!-- This is an auto-generated comment by CodeRabbit for review status -->

timestamp=$(date '+%Y-%m-%d_%H-%M-%S')

# Target directory where backups are stored
target_dir="/home/ubuntu/documents"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider making the target directory configurable

The backup directory is currently hardcoded. Consider making it configurable through environment variables or command-line arguments for better flexibility and portability.

-    target_dir="/home/ubuntu/documents"
+    target_dir="${BACKUP_DIR:-/home/ubuntu/documents}"

Also applies to: 73-73

Comment on lines +114 to +135
```bash
#!/bin/bash

<<Info
Author : Amitabh Soni
date : 30/11/24
description : This script is used to automate the starting of Docker and Jenkins services.
Info

# Starting Docker service
sudo systemctl start docker

# Starting Jenkins service
sudo systemctl start jenkins

# Checking if the previous commands ran successfully
if [[ $? -eq 0 ]]; then
echo "Docker and Jenkins services have been started successfully."
else
echo "An error occurred while starting Docker and Jenkins services."
fi
```
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve error handling in start_services.sh

Similar to stop_services.sh, the error handling should be independent for each service operation.

Consider this improved implementation:

 #!/bin/bash

 <<Info
 Author : Amitabh Soni
 date : 30/11/24
 description : This script is used to automate the starting of Docker and Jenkins services.
 Info

 # Starting Docker service
 sudo systemctl start docker
-
+if [[ $? -eq 0 ]]; then
+    echo "Docker service started successfully."
+else
+    echo "Failed to start Docker service."
+    exit 1
+fi
+
 # Starting Jenkins service
 sudo systemctl start jenkins
-
-# Checking if the previous commands ran successfully
 if [[ $? -eq 0 ]]; then
-    echo "Docker and Jenkins services have been started successfully."
+    echo "Jenkins service started successfully."
 else
-    echo "An error occurred while starting Docker and Jenkins services."
+    echo "Failed to start Jenkins service."
+    exit 1
 fi

Comment on lines +89 to +110
```bash
#!/bin/bash

<<Info
Author : Amitabh Soni
date : 30/11/24
description : This script is used to automate the stopping of Docker and Jenkins services.
Info

# Stopping Docker service
sudo systemctl stop docker

# Stopping Jenkins service
sudo systemctl stop jenkins

# Checking if the previous commands ran successfully
if [[ $? -eq 0 ]]; then
echo "Docker and Jenkins services have been stopped successfully."
else
echo "An error occurred while stopping Docker and Jenkins services."
fi
```
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve error handling in stop_services.sh

The current error handling only checks the status of the last command. Each service operation should be checked independently.

Consider this improved implementation:

 #!/bin/bash

 <<Info
 Author : Amitabh Soni
 date : 30/11/24
 description : This script is used to automate the stopping of Docker and Jenkins services.
 Info

 # Stopping Docker service
 sudo systemctl stop docker
-
+if [[ $? -eq 0 ]]; then
+    echo "Docker service stopped successfully."
+else
+    echo "Failed to stop Docker service."
+    exit 1
+fi
+
 # Stopping Jenkins service
 sudo systemctl stop jenkins
-
-# Checking if the previous commands ran successfully
 if [[ $? -eq 0 ]]; then
-    echo "Docker and Jenkins services have been stopped successfully."
+    echo "Jenkins service stopped successfully."
 else
-    echo "An error occurred while stopping Docker and Jenkins services."
+    echo "Failed to stop Jenkins service."
+    exit 1
 fi

Comment on lines +175 to +201
#!/bin/bash

<<Info
Author : Amitabh Soni
Date : 28/11/24
Description : Creating a script that backs up the current permissions of files in a directory to a file.
Info

# Prompt user for the directory path
read -p "Enter the directory path to backup permissions: " dir_path

# Check if the directory exists
if [[ ! -d "$dir_path" ]]; then
echo "Error: The directory '$dir_path' does not exist."
exit 1
fi

# Get and backup the ACL permissions recursively
getfacl -R "$dir_path" > permission_backup.txt

# Check if the command was successful
if [[ $? -eq 0 ]]; then
echo "Permissions backed up successfully to permission_backup.txt"
else
echo "Failed to backup permissions."
fi
```
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve backup file handling and add timestamp

The script uses a hardcoded backup filename which could lead to accidental overwrites.

Consider these improvements:

 #!/bin/bash

 <<Info
 Author      : Amitabh Soni
 Date        : 28/11/24
 Description : Creating a script that backs up the current permissions of files in a directory to a file.
 Info

+# Create backup directory if it doesn't exist
+backup_dir="permission_backups"
+mkdir -p "$backup_dir"
+
+# Generate timestamp for unique backup file
+timestamp=$(date +%Y%m%d_%H%M%S)
+backup_file="$backup_dir/permission_backup_${timestamp}.txt"
+
 # Prompt user for the directory path
 read -p "Enter the directory path to backup permissions: " dir_path

 # Check if the directory exists
 if [[ ! -d "$dir_path" ]]; then
    echo "Error: The directory '$dir_path' does not exist."
    exit 1
 fi

 # Get and backup the ACL permissions recursively
-getfacl -R "$dir_path" > permission_backup.txt
+getfacl -R "$dir_path" > "$backup_file"

 # Check if the command was successful
 if [[ $? -eq 0 ]]; then
-    echo "Permissions backed up successfully to permission_backup.txt"
+    echo "Permissions backed up successfully to $backup_file"
 else
    echo "Failed to backup permissions."
+   rm -f "$backup_file"  # Clean up failed backup file
    exit 1
 fi
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#!/bin/bash
<<Info
Author : Amitabh Soni
Date : 28/11/24
Description : Creating a script that backs up the current permissions of files in a directory to a file.
Info
# Prompt user for the directory path
read -p "Enter the directory path to backup permissions: " dir_path
# Check if the directory exists
if [[ ! -d "$dir_path" ]]; then
echo "Error: The directory '$dir_path' does not exist."
exit 1
fi
# Get and backup the ACL permissions recursively
getfacl -R "$dir_path" > permission_backup.txt
# Check if the command was successful
if [[ $? -eq 0 ]]; then
echo "Permissions backed up successfully to permission_backup.txt"
else
echo "Failed to backup permissions."
fi
```
#!/bin/bash
<<Info
Author : Amitabh Soni
Date : 28/11/24
Description : Creating a script that backs up the current permissions of files in a directory to a file.
Info
# Create backup directory if it doesn't exist
backup_dir="permission_backups"
mkdir -p "$backup_dir"
# Generate timestamp for unique backup file
timestamp=$(date +%Y%m%d_%H%M%S)
backup_file="$backup_dir/permission_backup_${timestamp}.txt"
# Prompt user for the directory path
read -p "Enter the directory path to backup permissions: " dir_path
# Check if the directory exists
if [[ ! -d "$dir_path" ]]; then
echo "Error: The directory '$dir_path' does not exist."
exit 1
fi
# Get and backup the ACL permissions recursively
getfacl -R "$dir_path" > "$backup_file"
# Check if the command was successful
if [[ $? -eq 0 ]]; then
echo "Permissions backed up successfully to $backup_file"
else
echo "Failed to backup permissions."
rm -f "$backup_file" # Clean up failed backup file
exit 1
fi

Comment on lines +213 to +239
#!/bin/bash

<<Info
Author : Amitabh Soni
Date : 28/11/24
Description : Script to restore file permissions from a backup file.
Info

# Prompt for the backup file path
read -p "Enter the backup file path: " backup_file

# Check if the backup file exists
if [[ ! -f "$backup_file" ]]; then
echo "Error: The backup file '$backup_file' does not exist."
exit 1
fi

# Restore the file permissions using the backup file
setfacl --restore="$backup_file"

# Check if the command was successful
if [[ $? -eq 0 ]]; then
echo "Permissions restored successfully from $backup_file"
else
echo "Failed to restore permissions."
fi
```
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add backup file format validation and target directory check

The script should validate the backup file format and ensure the target directory exists.

Consider these improvements:

 #!/bin/bash

 <<Info
 Author      : Amitabh Soni
 Date        : 28/11/24
 Description : Script to restore file permissions from a backup file.
 Info

+# Check if script is run with sudo privileges
+if [[ $EUID -ne 0 ]]; then
+   echo "This script must be run with sudo privileges"
+   exit 1
+fi
+
 # Prompt for the backup file path
 read -p "Enter the backup file path: " backup_file

 # Check if the backup file exists
 if [[ ! -f "$backup_file" ]]; then
    echo "Error: The backup file '$backup_file' does not exist."
    exit 1
 fi

+# Validate backup file format (should contain getfacl output)
+if ! grep -q "^# file:" "$backup_file"; then
+    echo "Error: Invalid backup file format. File should contain getfacl output."
+    exit 1
+fi
+
+# Extract target directory from backup file
+target_dir=$(grep -m1 "^# file:" "$backup_file" | cut -d: -f2 | xargs dirname)
+
+# Check if target directory exists
+if [[ ! -d "$target_dir" ]]; then
+    echo "Error: Target directory '$target_dir' does not exist."
+    exit 1
+fi
+
 # Restore the file permissions using the backup file
 setfacl --restore="$backup_file"

 # Check if the command was successful
 if [[ $? -eq 0 ]]; then
    echo "Permissions restored successfully from $backup_file"
 else
    echo "Failed to restore permissions."
+   exit 1
 fi
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#!/bin/bash
<<Info
Author : Amitabh Soni
Date : 28/11/24
Description : Script to restore file permissions from a backup file.
Info
# Prompt for the backup file path
read -p "Enter the backup file path: " backup_file
# Check if the backup file exists
if [[ ! -f "$backup_file" ]]; then
echo "Error: The backup file '$backup_file' does not exist."
exit 1
fi
# Restore the file permissions using the backup file
setfacl --restore="$backup_file"
# Check if the command was successful
if [[ $? -eq 0 ]]; then
echo "Permissions restored successfully from $backup_file"
else
echo "Failed to restore permissions."
fi
```
#!/bin/bash
<<Info
Author : Amitabh Soni
Date : 28/11/24
Description : Script to restore file permissions from a backup file.
Info
# Check if script is run with sudo privileges
if [[ $EUID -ne 0 ]]; then
echo "This script must be run with sudo privileges"
exit 1
fi
# Prompt for the backup file path
read -p "Enter the backup file path: " backup_file
# Check if the backup file exists
if [[ ! -f "$backup_file" ]]; then
echo "Error: The backup file '$backup_file' does not exist."
exit 1
fi
# Validate backup file format (should contain getfacl output)
if ! grep -q "^# file:" "$backup_file"; then
echo "Error: Invalid backup file format. File should contain getfacl output."
exit 1
fi
# Extract target directory from backup file
target_dir=$(grep -m1 "^# file:" "$backup_file" | cut -d: -f2 | xargs dirname)
# Check if target directory exists
if [[ ! -d "$target_dir" ]]; then
echo "Error: Target directory '$target_dir' does not exist."
exit 1
fi
# Restore the file permissions using the backup file
setfacl --restore="$backup_file"
# Check if the command was successful
if [[ $? -eq 0 ]]; then
echo "Permissions restored successfully from $backup_file"
else
echo "Failed to restore permissions."
exit 1
fi

Comment on lines +106 to +138
#!/bin/bash

<<Info
Author : Amitabh Soni
Date : 28/11/24
Description : This script sets ACL permissions for a user on a given file, based on user input.
Info

# Getting username
read -p "Enter username: " user

# Getting file path
read -p "Enter a file path: " file_path

# Getting permission for the user
read -p "Enter the permission for user (e.g., rwx): " per

# Validate if the file exists
if [[ ! -e "$file_path" ]]; then
echo "Error: File '$file_path' does not exist."
exit 1
fi

# Setting ACL permission
setfacl -m u:$user:$per "$file_path"

# Checking the exit status of setfacl
if [[ $? -eq 0 ]]; then
echo "ACL permissions set successfully for user '$user' on file '$file_path'."
else
echo "Failed to set ACL permissions for user '$user' on file '$file_path'."
fi
```
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add user existence check and permission format validation

The script should validate if the specified user exists and if the permission format is correct.

Consider these improvements:

 #!/bin/bash

 <<Info
 Author      : Amitabh Soni
 Date        : 28/11/24
 Description : This script sets ACL permissions for a user on a given file, based on user input.
 Info

+# Check if script is run with sudo privileges
+if [[ $EUID -ne 0 ]]; then
+   echo "This script must be run with sudo privileges"
+   exit 1
+fi
+
 # Getting username
 read -p "Enter username: " user

+# Validate if user exists
+if ! id "$user" >/dev/null 2>&1; then
+    echo "Error: User '$user' does not exist"
+    exit 1
+fi
+
 # Getting file path
 read -p "Enter a file path: " file_path

 # Getting permission for the user
 read -p "Enter the permission for user (e.g., rwx): " per

+# Validate permission format
+if ! [[ "$per" =~ ^[rwx-]{3}$ ]]; then
+    echo "Error: Invalid permission format. Please use combinations of r, w, x (e.g., rwx, r-x)"
+    exit 1
+fi
+
 # Validate if the file exists
 if [[ ! -e "$file_path" ]]; then
    echo "Error: File '$file_path' does not exist."
    exit 1
 fi

 # Setting ACL permission
-setfacl -m u:$user:$per "$file_path"
+setfacl -m u:"$user":"$per" "$file_path"

 # Checking the exit status of setfacl
 if [[ $? -eq 0 ]]; then
    echo "ACL permissions set successfully for user '$user' on file '$file_path'."
 else
    echo "Failed to set ACL permissions for user '$user' on file '$file_path'."
+   exit 1
 fi
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#!/bin/bash
<<Info
Author : Amitabh Soni
Date : 28/11/24
Description : This script sets ACL permissions for a user on a given file, based on user input.
Info
# Getting username
read -p "Enter username: " user
# Getting file path
read -p "Enter a file path: " file_path
# Getting permission for the user
read -p "Enter the permission for user (e.g., rwx): " per
# Validate if the file exists
if [[ ! -e "$file_path" ]]; then
echo "Error: File '$file_path' does not exist."
exit 1
fi
# Setting ACL permission
setfacl -m u:$user:$per "$file_path"
# Checking the exit status of setfacl
if [[ $? -eq 0 ]]; then
echo "ACL permissions set successfully for user '$user' on file '$file_path'."
else
echo "Failed to set ACL permissions for user '$user' on file '$file_path'."
fi
```
#!/bin/bash
<<Info
Author : Amitabh Soni
Date : 28/11/24
Description : This script sets ACL permissions for a user on a given file, based on user input.
Info
# Check if script is run with sudo privileges
if [[ $EUID -ne 0 ]]; then
echo "This script must be run with sudo privileges"
exit 1
fi
# Getting username
read -p "Enter username: " user
# Validate if user exists
if ! id "$user" >/dev/null 2>&1; then
echo "Error: User '$user' does not exist"
exit 1
fi
# Getting file path
read -p "Enter a file path: " file_path
# Getting permission for the user
read -p "Enter the permission for user (e.g., rwx): " per
# Validate permission format
if ! [[ "$per" =~ ^[rwx-]{3}$ ]]; then
echo "Error: Invalid permission format. Please use combinations of r, w, x (e.g., rwx, r-x)"
exit 1
fi
# Validate if the file exists
if [[ ! -e "$file_path" ]]; then
echo "Error: File '$file_path' does not exist."
exit 1
fi
# Setting ACL permission
setfacl -m u:"$user":"$per" "$file_path"
# Checking the exit status of setfacl
if [[ $? -eq 0 ]]; then
echo "ACL permissions set successfully for user '$user' on file '$file_path'."
else
echo "Failed to set ACL permissions for user '$user' on file '$file_path'."
exit 1
fi

Comment on lines +54 to +88
#!/bin/bash

<<Info
Author : Amitabh Soni
Date : 28/11/24
Description : This script changes the permissions of all .txt files in a directory based on user input.
Info

# Prompting user for input
read -p "Enter the directory name: " dir_name
read -p "Enter permission value (numeric, e.g., 755): " num_value_per

# Validate if the directory exists
if [[ ! -d "$dir_name" ]]; then
echo "Error: Directory '$dir_name' does not exist."
exit 1
fi

# Check if .txt files exist in the directory
txt_files=("$dir_name"/*.txt)
if [[ ! -e "${txt_files[0]}" ]]; then
echo "Error: No .txt files found in directory '$dir_name'."
exit 1
fi

# Attempt to change file permissions for all .txt files
sudo chmod "$num_value_per" "$dir_name"/*.txt

# Checking the exit status
if [[ $? -eq 0 ]]; then
echo "Permissions changed successfully for all .txt files in '$dir_name'."
else
echo "Failed to change permissions for .txt files in '$dir_name'."
fi
```
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add sudo privilege check and improve error handling

The script uses sudo without verifying if the user has the necessary privileges.

Consider these improvements:

 #!/bin/bash

 <<Info
 Author      : Amitabh Soni
 Date        : 28/11/24
 Description : This script changes the permissions of all .txt files in a directory based on user input.
 Info

+# Check if script is run with sudo privileges
+if [[ $EUID -ne 0 ]]; then
+   echo "This script must be run with sudo privileges"
+   exit 1
+fi
+
 # Prompting user for input
 read -p "Enter the directory name: " dir_name
 read -p "Enter permission value (numeric, e.g., 755): " num_value_per

+# Validate permission value format
+if ! [[ "$num_value_per" =~ ^[0-7]{3}$ ]]; then
+    echo "Error: Invalid permission format. Please use octal notation (e.g., 755)"
+    exit 1
+fi
+
 # Validate if the directory exists
 if [[ ! -d "$dir_name" ]]; then
   echo "Error: Directory '$dir_name' does not exist."
   exit 1
 fi

 # Check if .txt files exist in the directory
 txt_files=("$dir_name"/*.txt)
 if [[ ! -e "${txt_files[0]}" ]]; then
   echo "Error: No .txt files found in directory '$dir_name'."
   exit 1
 fi

 # Attempt to change file permissions for all .txt files
-sudo chmod "$num_value_per" "$dir_name"/*.txt
+chmod "$num_value_per" "$dir_name"/*.txt

 # Checking the exit status
 if [[ $? -eq 0 ]]; then
   echo "Permissions changed successfully for all .txt files in '$dir_name'."
 else
   echo "Failed to change permissions for .txt files in '$dir_name'."
+  exit 1
 fi
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#!/bin/bash
<<Info
Author : Amitabh Soni
Date : 28/11/24
Description : This script changes the permissions of all .txt files in a directory based on user input.
Info
# Prompting user for input
read -p "Enter the directory name: " dir_name
read -p "Enter permission value (numeric, e.g., 755): " num_value_per
# Validate if the directory exists
if [[ ! -d "$dir_name" ]]; then
echo "Error: Directory '$dir_name' does not exist."
exit 1
fi
# Check if .txt files exist in the directory
txt_files=("$dir_name"/*.txt)
if [[ ! -e "${txt_files[0]}" ]]; then
echo "Error: No .txt files found in directory '$dir_name'."
exit 1
fi
# Attempt to change file permissions for all .txt files
sudo chmod "$num_value_per" "$dir_name"/*.txt
# Checking the exit status
if [[ $? -eq 0 ]]; then
echo "Permissions changed successfully for all .txt files in '$dir_name'."
else
echo "Failed to change permissions for .txt files in '$dir_name'."
fi
```
#!/bin/bash
<<Info
Author : Amitabh Soni
Date : 28/11/24
Description : This script changes the permissions of all .txt files in a directory based on user input.
Info
# Check if script is run with sudo privileges
if [[ $EUID -ne 0 ]]; then
echo "This script must be run with sudo privileges"
exit 1
fi
# Prompting user for input
read -p "Enter the directory name: " dir_name
read -p "Enter permission value (numeric, e.g., 755): " num_value_per
# Validate permission value format
if ! [[ "$num_value_per" =~ ^[0-7]{3}$ ]]; then
echo "Error: Invalid permission format. Please use octal notation (e.g., 755)"
exit 1
fi
# Validate if the directory exists
if [[ ! -d "$dir_name" ]]; then
echo "Error: Directory '$dir_name' does not exist."
exit 1
fi
# Check if .txt files exist in the directory
txt_files=("$dir_name"/*.txt)
if [[ ! -e "${txt_files[0]}" ]]; then
echo "Error: No .txt files found in directory '$dir_name'."
exit 1
fi
# Attempt to change file permissions for all .txt files
chmod "$num_value_per" "$dir_name"/*.txt
# Checking the exit status
if [[ $? -eq 0 ]]; then
echo "Permissions changed successfully for all .txt files in '$dir_name'."
else
echo "Failed to change permissions for .txt files in '$dir_name'."
exit 1
fi

Copy link

github-actions bot commented Feb 1, 2025

Stale pull request message

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant