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

feat: migrate script #1852

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open

feat: migrate script #1852

wants to merge 2 commits into from

Conversation

steveiliop56
Copy link
Collaborator

@steveiliop56 steveiliop56 commented Dec 17, 2024

It will look something like:

👋 Welcome to the Runtipi migration script. It will automatically update everything to work with version 4.0.0

⚠️  Warning: Make sure you have backed up your data before continuing, if something goes wrong during the migration process, you can risk losing important data! You can press Ctrl+C to cancel now if you need to backup

⏳ Starting in 10 seconds...

📦 Detecting apps...
📦 Found app: app1
📦 Found app: app2
📦 Found app: app3

⚠️  Warning: If an app is missing from the list above, please cancel the script immediately with Ctrl+C and seek help in our Discord or Forums

⏳ Starting in 10 seconds...

🛑 Stopping apps...
🛑 Stopping app: app1
🛑 Stopping app: app2
🛑 Stopping app: app3

🛑 Stopping Runtipi...

⏭️  Moving apps...
⏭️  Moving app: app1
⏭️  Moving app: app2
⏭️  Moving app: app3

🧹 Cleaning repos...

🔄 Updating Runtipi...

[CLI update output goes here]

Tested and working on runtipi install.

Summary by CodeRabbit

  • New Features

    • Introduced a new migration script to assist users in upgrading from version 3.0.0 to 4.0.0 of the Runtipi application.
    • Added color-coded output and a welcome message for improved user experience during migration.
  • Bug Fixes

    • Enhanced error handling to ensure the script exits on errors and uninitialized variables during the migration process.
  • Chores

    • Removed the outdated migration script for version 3.x to 4.0.0.

Copy link
Contributor

coderabbitai bot commented Dec 17, 2024

Walkthrough

The pull request introduces a new migration script update-3.0.0-to-4.0.0.sh for transitioning the Runtipi application from version 3.0.0 to 4.0.0. The script provides a comprehensive migration process that includes error handling, privilege checks, data backup warnings, application detection, service stopping, and directory restructuring. It ensures a systematic approach to upgrading the application by creating new directories, moving application and data files, and preparing the system for the new version.

Changes

File Change Summary
scripts/update-3.0.0-to-4.0.0.sh New migration script added to facilitate version upgrade from 3.0.0 to 4.0.0
scripts/update-3.x-to-4.0.0.sh Previous migration script removed

Sequence Diagram

sequenceDiagram
    participant User
    participant Script
    participant Runtipi Service
    participant Applications
    
    User->>Script: Run migration script
    Script->>Script: Check root privileges
    Script->>Script: Verify migration prerequisites
    Script-->>User: Warn about data backup
    User->>Script: Confirm
    Script->>Applications: Stop all applications
    Script->>Runtipi Service: Stop service
    Script->>Script: Create new directory structure
    Script->>Script: Move applications and data
    Script->>Script: Clean up repositories
    Script->>Runtipi Service: Update to nightly build
    Script-->>User: Migration complete
Loading

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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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 or @coderabbitai title 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
Contributor

@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

🧹 Nitpick comments (2)
scripts/update-3.0.0-to-4.0.0.sh (2)

1-4: Enhance error handling with cleanup trap

While the error handling flags are good, consider adding a cleanup trap to ensure proper cleanup if the script fails midway.

 #!/usr/bin/env bash
 set -o errexit
 set -o nounset
 set -o pipefail
+
+cleanup() {
+    # Start Runtipi if it was stopped during script execution
+    if [[ -f ".migration_in_progress" ]]; then
+        echo "Error occurred. Attempting to restart Runtipi..."
+        ./runtipi-cli start >/dev/null 2>&1 || true
+        rm -f ".migration_in_progress"
+    fi
+}
+
+trap cleanup ERR EXIT

12-13: Enhance user guidance and documentation

The welcome message should provide more context and guidance.

-# Welcome message
-echo -e "👋 Welcome to the Runtipi migration script. It will automatically update everything to work with version ${Green}4.0.0${ColorOff}\n"
+# Welcome message and information
+cat << EOF
+👋 Welcome to the Runtipi migration script v4.0.0
+
+This script will:
+1. Verify system requirements and current installation
+2. Create a complete backup of your current installation
+3. Stop all running applications
+4. Migrate applications to the new directory structure
+5. Update Runtipi to version 4.0.0
+
+Requirements:
+- Current version must be 3.0.x
+- At least $(df -h . | awk 'NR==2 {print $4}') of free disk space
+- Network connectivity to GitHub
+- Root privileges
+
+A backup will be created before any changes are made.
+In case of failure, you can restore from the backup directory.
+
+EOF
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fd41aef and 1166fdf.

📒 Files selected for processing (2)
  • scripts/update-3.0.0-to-4.0.0.sh (1 hunks)
  • scripts/update-3.x-to-4.0.0.sh (0 hunks)
💤 Files with no reviewable changes (1)
  • scripts/update-3.x-to-4.0.0.sh
🧰 Additional context used
🪛 Shellcheck (0.10.0)
scripts/update-3.0.0-to-4.0.0.sh

[warning] 16-16: Use semicolon or linefeed before 'then' (or quote to make it literal).

(SC1010)


[warning] 22-22: Use semicolon or linefeed before 'then' (or quote to make it literal).

(SC1010)


[warning] 28-28: Use semicolon or linefeed before 'then' (or quote to make it literal).

(SC1010)


[error] 42-42: Iterating over ls output is fragile. Use globs.

(SC2045)


[error] 54-54: Iterating over ls output is fragile. Use globs.

(SC2045)


[error] 72-72: Iterating over ls output is fragile. Use globs.

(SC2045)


[warning] 73-73: Use semicolon or linefeed before 'then' (or quote to make it literal).

(SC1010)

Comment on lines +1 to +95
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail

# Colors
Red='\e[31m'
Green='\e[32m'
Yellow='\e[33m'
ColorOff='\e[0m'

# Welcome message
echo -e "👋 Welcome to the Runtipi migration script. It will automatically update everything to work with version ${Green}4.0.0${ColorOff}\n"

# Check if running as root
if [[ "$EUID" -ne 0 ]] then
echo -e "❌ ${Red}Root is required for this script!${ColorOff}"
exit 1
fi

# Verify app data
if [[ -d "app-data/app-data" ]] then
echo -e "❌ ${Red}You have an additional app-data folder, the script cannot continue with this folder, please seek help in our Discord or Forums for a guide on how to fix the issue.${ColorOff}"
exit 1
fi

# Check if already migrated
if [[ -d "apps/1" ]] then
echo -e "❌ ${Red}You have already migrated your apps, if you haven't updated yet, run ${Green}./runtipi-cli update latest${ColorOff}"
exit 1
fi

# Backups warning
echo -e "⚠️ ${Yellow}Warning:${ColorOff} Make sure you have backed up your data before continuing, if something goes wrong during the migration process, you can risk losing important data! You can press Ctrl+C to cancel now if you need to backup\n"
echo -e "⏳ Starting in 10 seconds...\n"

sleep 10s

# Get all apps
echo -e "📦 Detecting apps..."

for app in $(ls apps); do
echo -e "📦 Found app: ${Green}$app${ColorOff}"
done

echo -e "\n⚠️ ${Yellow}Warning:${ColorOff} If an app is missing from the list above, please cancel the script immediately with Ctrl+C and seek help in our Discord or Forums\n"
echo -e "⏳ Starting in 10 seconds...\n"

sleep 10s

# Stop apps
echo -e "🛑 Stopping apps..."

for app in $(ls apps); do
echo -e "🛑 Stopping app: ${Green}$app${ColorOff}"
./runtipi-cli app stop $app >/dev/null 2>&1
done

# Stop runtipi
echo -e "\n🛑 Stopping Runtipi...\n"

./runtipi-cli stop >/dev/null 2>&1

# Moving apps
echo -e "⏭️ Moving apps..."

mkdir -p apps/1
mkdir -p app-data/1
mkdir -p user-config/1
mkdir -p backups/1

for app in $(ls apps); do
if [[ $app == 1 ]] then
continue
fi
echo -e "⏭️ Moving app: ${Green}$app${ColorOff}"
mv apps/$app apps/1/$app
mv app-data/$app app-data/1/$app
if [[ -d "user-config/$app" ]]; then
mv user-config/$app user-config/1/$app
fi
if [[ -d "backups/$app" ]]; then
mv backups/$app backups/1/$app
fi
done

# Clean repos
echo -e "\n🧹 Cleaning repos...\n"

rm -rf repos/*

# Update runtipi
echo -e "🔄 Updating Runtipi...\n"

./runtipi-cli update nightly
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add essential pre-flight checks

The script should verify system requirements and current state before proceeding.

Add these checks after the root verification:

# Check current version
current_version=$(./runtipi-cli version)
if [[ ! "$current_version" =~ ^3\.0\.[0-9]+$ ]]; then
  echo -e "${Red}This script is only for migrating from version 3.0.x to 4.0.0${ColorOff}"
  echo -e "Current version: ${current_version}"
  exit 1
fi

# Check available disk space (need at least 2x current size for backup)
current_size=$(du -s . | awk '{print $1}')
available_space=$(df -k . | awk 'NR==2 {print $4}')
if (( available_space < current_size * 2 )); then
  echo -e "${Red}Insufficient disk space for backup and migration${ColorOff}"
  echo -e "Required: $(( current_size * 2 / 1024 ))MB, Available: $(( available_space / 1024 ))MB"
  exit 1
fi

# Check network connectivity
if ! curl -s --connect-timeout 5 https://api.github.com >/dev/null; then
  echo -e "${Red}No network connectivity to GitHub${ColorOff}"
  echo "Network access is required for updating Runtipi"
  exit 1
fi
🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 16-16: Use semicolon or linefeed before 'then' (or quote to make it literal).

(SC1010)


[warning] 22-22: Use semicolon or linefeed before 'then' (or quote to make it literal).

(SC1010)


[warning] 28-28: Use semicolon or linefeed before 'then' (or quote to make it literal).

(SC1010)


[error] 42-42: Iterating over ls output is fragile. Use globs.

(SC2045)


[error] 54-54: Iterating over ls output is fragile. Use globs.

(SC2045)


[error] 72-72: Iterating over ls output is fragile. Use globs.

(SC2045)


[warning] 73-73: Use semicolon or linefeed before 'then' (or quote to make it literal).

(SC1010)

Comment on lines +16 to +19
if [[ "$EUID" -ne 0 ]] then
echo -e "❌ ${Red}Root is required for this script!${ColorOff}"
exit 1
fi
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix syntax error in root check condition

There's a syntax error in the if statement that needs to be fixed.

-if [[ "$EUID" -ne 0 ]] then
+if [[ "$EUID" -ne 0 ]]; then
   echo -e "❌ ${Red}Root is required for this script!${ColorOff}"
   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
if [[ "$EUID" -ne 0 ]] then
echo -e "${Red}Root is required for this script!${ColorOff}"
exit 1
fi
if [[ "$EUID" -ne 0 ]]; then
echo -e "${Red}Root is required for this script!${ColorOff}"
exit 1
fi
🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 16-16: Use semicolon or linefeed before 'then' (or quote to make it literal).

(SC1010)

Comment on lines +40 to +44
echo -e "📦 Detecting apps..."

for app in $(ls apps); do
echo -e "📦 Found app: ${Green}$app${ColorOff}"
done
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Replace unsafe ls usage with glob expansion

Using ls for iteration is fragile and can break with special characters in app names.

 echo -e "📦 Detecting apps..."
 
-for app in $(ls apps); do
+shopt -s nullglob
+for app in apps/*; do
+  app=${app#apps/}  # Remove 'apps/' prefix
+  [[ -d "apps/$app" ]] || continue  # Skip if not a directory
   echo -e "📦 Found app: ${Green}$app${ColorOff}"
 done
📝 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
echo -e "📦 Detecting apps..."
for app in $(ls apps); do
echo -e "📦 Found app: ${Green}$app${ColorOff}"
done
echo -e "📦 Detecting apps..."
shopt -s nullglob
for app in apps/*; do
app=${app#apps/} # Remove 'apps/' prefix
[[ -d "apps/$app" ]] || continue # Skip if not a directory
echo -e "📦 Found app: ${Green}$app${ColorOff}"
done
🧰 Tools
🪛 Shellcheck (0.10.0)

[error] 42-42: Iterating over ls output is fragile. Use globs.

(SC2045)

Comment on lines +21 to +31
# Verify app data
if [[ -d "app-data/app-data" ]] then
echo -e "❌ ${Red}You have an additional app-data folder, the script cannot continue with this folder, please seek help in our Discord or Forums for a guide on how to fix the issue.${ColorOff}"
exit 1
fi

# Check if already migrated
if [[ -d "apps/1" ]] then
echo -e "❌ ${Red}You have already migrated your apps, if you haven't updated yet, run ${Green}./runtipi-cli update latest${ColorOff}"
exit 1
fi
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix syntax errors and enhance pre-migration validation

There are syntax errors in the if statements and the validation could be more comprehensive.

-if [[ -d "app-data/app-data" ]] then
+if [[ -d "app-data/app-data" ]]; then
   echo -e "❌ ${Red}You have an additional app-data folder, the script cannot continue with this folder, please seek help in our Discord or Forums for a guide on how to fix the issue.${ColorOff}"
   exit 1
 fi

-if [[ -d "apps/1" ]] then
+if [[ -d "apps/1" ]]; then
   echo -e "❌ ${Red}You have already migrated your apps, if you haven't updated yet, run ${Green}./runtipi-cli update latest${ColorOff}"
   exit 1
 fi

+# Verify required directories exist
+for dir in "apps" "app-data" "user-config" "backups"; do
+  if [[ ! -d "$dir" ]]; then
+    echo -e "❌ ${Red}Required directory '$dir' not found!${ColorOff}"
+    exit 1
+  fi
+done

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 Shellcheck (0.10.0)

[warning] 22-22: Use semicolon or linefeed before 'then' (or quote to make it literal).

(SC1010)


[warning] 28-28: Use semicolon or linefeed before 'then' (or quote to make it literal).

(SC1010)

Comment on lines +87 to +95
# Clean repos
echo -e "\n🧹 Cleaning repos...\n"

rm -rf repos/*

# Update runtipi
echo -e "🔄 Updating Runtipi...\n"

./runtipi-cli update nightly
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add verification steps to cleanup and update process

The cleanup and update process needs safety checks and verification.

 echo -e "\n🧹 Cleaning repos...\n"
 
+if [[ -d "repos" ]]; then
+  # Backup repos directory
+  mv repos "repos_backup_${BACKUP_TIMESTAMP}"
+fi
 mkdir -p repos
-rm -rf repos/*
 
 echo -e "🔄 Updating Runtipi...\n"
 
-./runtipi-cli update nightly
+if ! ./runtipi-cli update nightly; then
+  echo -e "${Red}Failed to update Runtipi${ColorOff}"
+  echo -e "Please check the logs and try updating manually with: ${Green}./runtipi-cli update latest${ColorOff}"
+  exit 1
+fi
+
+# Clean up migration marker
+rm -f .migration_in_progress
+
+echo -e "\n✅ ${Green}Migration completed successfully!${ColorOff}"
+echo -e "Backup is available at: ${Green}${BACKUP_DIR}${ColorOff}"

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +51 to +62
# Stop apps
echo -e "🛑 Stopping apps..."

for app in $(ls apps); do
echo -e "🛑 Stopping app: ${Green}$app${ColorOff}"
./runtipi-cli app stop $app >/dev/null 2>&1
done

# Stop runtipi
echo -e "\n🛑 Stopping Runtipi...\n"

./runtipi-cli stop >/dev/null 2>&1
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Enhance app stopping logic with error handling

The current implementation suppresses errors and doesn't verify successful stops.

 echo -e "🛑 Stopping apps..."
 
-for app in $(ls apps); do
+failed_stops=()
+shopt -s nullglob
+for app in apps/*; do
+  app=${app#apps/}
+  [[ -d "apps/$app" ]] || continue
   echo -e "🛑 Stopping app: ${Green}$app${ColorOff}"
-  ./runtipi-cli app stop $app >/dev/null 2>&1
+  if ! ./runtipi-cli app stop "$app"; then
+    echo -e "${Red}Failed to stop $app${ColorOff}"
+    failed_stops+=("$app")
+  fi
 done
 
+if [[ ${#failed_stops[@]} -gt 0 ]]; then
+  echo -e "${Red}Failed to stop some apps: ${failed_stops[*]}${ColorOff}"
+  echo "Please stop these apps manually and retry the migration"
+  exit 1
+fi
+
 echo -e "\n🛑 Stopping Runtipi...\n"
 
-./runtipi-cli stop >/dev/null 2>&1
+if ! ./runtipi-cli stop; then
+  echo -e "${Red}Failed to stop Runtipi${ColorOff}"
+  exit 1
+fi
+
+# Mark migration in progress for cleanup trap
+touch .migration_in_progress
📝 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
# Stop apps
echo -e "🛑 Stopping apps..."
for app in $(ls apps); do
echo -e "🛑 Stopping app: ${Green}$app${ColorOff}"
./runtipi-cli app stop $app >/dev/null 2>&1
done
# Stop runtipi
echo -e "\n🛑 Stopping Runtipi...\n"
./runtipi-cli stop >/dev/null 2>&1
# Stop apps
echo -e "🛑 Stopping apps..."
failed_stops=()
shopt -s nullglob
for app in apps/*; do
app=${app#apps/}
[[ -d "apps/$app" ]] || continue
echo -e "🛑 Stopping app: ${Green}$app${ColorOff}"
if ! ./runtipi-cli app stop "$app"; then
echo -e "${Red}Failed to stop $app${ColorOff}"
failed_stops+=("$app")
fi
done
if [[ ${#failed_stops[@]} -gt 0 ]]; then
echo -e "${Red}Failed to stop some apps: ${failed_stops[*]}${ColorOff}"
echo "Please stop these apps manually and retry the migration"
exit 1
fi
echo -e "\n🛑 Stopping Runtipi...\n"
if ! ./runtipi-cli stop; then
echo -e "${Red}Failed to stop Runtipi${ColorOff}"
exit 1
fi
# Mark migration in progress for cleanup trap
touch .migration_in_progress
🧰 Tools
🪛 Shellcheck (0.10.0)

[error] 54-54: Iterating over ls output is fragile. Use globs.

(SC2045)

Comment on lines +64 to +85
# Moving apps
echo -e "⏭️ Moving apps..."

mkdir -p apps/1
mkdir -p app-data/1
mkdir -p user-config/1
mkdir -p backups/1

for app in $(ls apps); do
if [[ $app == 1 ]] then
continue
fi
echo -e "⏭️ Moving app: ${Green}$app${ColorOff}"
mv apps/$app apps/1/$app
mv app-data/$app app-data/1/$app
if [[ -d "user-config/$app" ]]; then
mv user-config/$app user-config/1/$app
fi
if [[ -d "backups/$app" ]]; then
mv backups/$app backups/1/$app
fi
done
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add safety measures and verification to app migration

The current implementation lacks proper backup and verification steps during migration.

 echo -e "⏭️  Moving apps..."
 
+# Create timestamp for backup
+BACKUP_TIMESTAMP=$(date +%Y%m%d_%H%M%S)
+BACKUP_DIR="migration_backup_${BACKUP_TIMESTAMP}"
+
+echo -e "📦 Creating backup in ${Green}${BACKUP_DIR}${ColorOff}"
+mkdir -p "${BACKUP_DIR}"
+cp -r apps app-data user-config backups "${BACKUP_DIR}/"
+
 mkdir -p apps/1
 mkdir -p app-data/1
 mkdir -p user-config/1
 mkdir -p backups/1
 
-for app in $(ls apps); do
-  if [[ $app == 1 ]] then
+failed_migrations=()
+shopt -s nullglob
+for app_path in apps/*; do
+  app=${app_path#apps/}
+  [[ -d "$app_path" ]] || continue
+  if [[ $app == "1" ]]; then
     continue
   fi
   echo -e "⏭️  Moving app: ${Green}$app${ColorOff}"
-  mv apps/$app apps/1/$app
-  mv app-data/$app app-data/1/$app
-  if [[ -d "user-config/$app" ]]; then
-    mv user-config/$app user-config/1/$app
-  fi
-  if [[ -d "backups/$app" ]]; then
-    mv backups/$app backups/1/$app
+  
+  # Move with error checking
+  if ! mv "apps/$app" "apps/1/$app" || \
+     ! mv "app-data/$app" "app-data/1/$app"; then
+    failed_migrations+=("$app")
+    continue
   fi
+  
+  # Move optional directories if they exist
+  [[ -d "user-config/$app" ]] && mv "user-config/$app" "user-config/1/$app"
+  [[ -d "backups/$app" ]] && mv "backups/$app" "backups/1/$app"
 done
+
+if [[ ${#failed_migrations[@]} -gt 0 ]]; then
+  echo -e "${Red}Failed to migrate some apps: ${failed_migrations[*]}${ColorOff}"
+  echo -e "Backup is available at: ${Green}${BACKUP_DIR}${ColorOff}"
+  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
# Moving apps
echo -e "⏭️ Moving apps..."
mkdir -p apps/1
mkdir -p app-data/1
mkdir -p user-config/1
mkdir -p backups/1
for app in $(ls apps); do
if [[ $app == 1 ]] then
continue
fi
echo -e "⏭️ Moving app: ${Green}$app${ColorOff}"
mv apps/$app apps/1/$app
mv app-data/$app app-data/1/$app
if [[ -d "user-config/$app" ]]; then
mv user-config/$app user-config/1/$app
fi
if [[ -d "backups/$app" ]]; then
mv backups/$app backups/1/$app
fi
done
# Moving apps
echo -e "⏭️ Moving apps..."
# Create timestamp for backup
BACKUP_TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR="migration_backup_${BACKUP_TIMESTAMP}"
echo -e "📦 Creating backup in ${Green}${BACKUP_DIR}${ColorOff}"
mkdir -p "${BACKUP_DIR}"
cp -r apps app-data user-config backups "${BACKUP_DIR}/"
mkdir -p apps/1
mkdir -p app-data/1
mkdir -p user-config/1
mkdir -p backups/1
failed_migrations=()
shopt -s nullglob
for app_path in apps/*; do
app=${app_path#apps/}
[[ -d "$app_path" ]] || continue
if [[ $app == "1" ]]; then
continue
fi
echo -e "⏭️ Moving app: ${Green}$app${ColorOff}"
# Move with error checking
if ! mv "apps/$app" "apps/1/$app" || \
! mv "app-data/$app" "app-data/1/$app"; then
failed_migrations+=("$app")
continue
fi
# Move optional directories if they exist
[[ -d "user-config/$app" ]] && mv "user-config/$app" "user-config/1/$app"
[[ -d "backups/$app" ]] && mv "backups/$app" "backups/1/$app"
done
if [[ ${#failed_migrations[@]} -gt 0 ]]; then
echo -e "${Red}Failed to migrate some apps: ${failed_migrations[*]}${ColorOff}"
echo -e "Backup is available at: ${Green}${BACKUP_DIR}${ColorOff}"
exit 1
fi
🧰 Tools
🪛 Shellcheck (0.10.0)

[error] 72-72: Iterating over ls output is fragile. Use globs.

(SC2045)


[warning] 73-73: Use semicolon or linefeed before 'then' (or quote to make it literal).

(SC1010)

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

Successfully merging this pull request may close these issues.

1 participant