-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
base: master
Are you sure you want to change the base?
Added solution.md for Day-09 : Automate Backup Creation and Rotation with Cron Job #341
Conversation
WalkthroughThe 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
Poem
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this 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 commandThe current error handling could be improved by:
- Checking if the source directory exists
- Validating if we have write permissions to the target directory
- 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 caseThe
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 executionFor production use:
- Consider reducing the frequency to a more reasonable interval
- 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
⛔ 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" |
There was a problem hiding this comment.
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
```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 | ||
``` |
There was a problem hiding this comment.
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
```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 | ||
``` |
There was a problem hiding this comment.
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
#!/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 | ||
``` |
There was a problem hiding this comment.
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.
#!/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 |
#!/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 | ||
``` |
There was a problem hiding this comment.
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.
#!/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 |
#!/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 | ||
``` |
There was a problem hiding this comment.
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.
#!/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 |
#!/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 | ||
``` |
There was a problem hiding this comment.
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.
#!/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 |
Stale pull request message |
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:Implementation Details:
backup_with_rotation.sh
script to handle backup creation and rotation./home/ubuntu/documents
directory.Testing:
This solution ensures efficient backup management and automation, making it suitable for systems requiring frequent data backups.
Summary by CodeRabbit
New Features
Bug Fixes
Documentation