Shell Scripting : Menambahkan Repositori Debian: Difference between revisions

No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
[[File:ShellScripting.png|thumb|'''ShellScripting''']]
Buat file baru dengan nama tambah_repositori.sh<syntaxhighlight lang="linuxconfig">
Buat file baru dengan nama tambah_repositori.sh<syntaxhighlight lang="linuxconfig">
nano tambah_repositori.sh
nano tambah_repositori.sh
</syntaxhighlight>isikan script berikut ini :<syntaxhighlight lang="linuxconfig" line="1">
</syntaxhighlight>isikan script berikut ini :<syntaxhighlight lang="linuxconfig" line="1">
#!/bin/bash
#!/bin/bash
# Script sederhana dengan auto-fix
# Script dengan validasi setelah hapus


# Auto-fix function
echo "=== SMART REPOSITORY REPLACEMENT ==="
auto_fix() {
    # Fix line endings
    sed -i 's/\r$//' "$0" 2>/dev/null
    # Ensure executable
    [ ! -x "$0" ] && chmod +x "$0"
    # Check shebang
    if ! head -1 "$0" | grep -q "^#!/bin/bash"; then
        exec bash "$0"
    fi
}


# Run auto-fix first
# Validasi system
auto_fix
if [ ! -f /etc/apt/sources.list ]; then
    echo "❌ Error: sources.list tidak ditemukan!"
    exit 1
fi


# Main script
# Backup dengan timestamp
echo "=== Auto-Fix Repository Setup ==="
timestamp=$(date +%Y%m%d_%H%M%S)
backup_path="/root/sources.list.backup.$timestamp"
cp /etc/apt/sources.list $backup_path
echo "📁 Backup created: $backup_path"


# Cek root
# Simpan line count sebelum hapus
[ "$EUID" -ne 0 ] && echo "❌ Run with sudo!" && exit 1
old_line_count=$(wc -l < /etc/apt/sources.list)


# Backup
# Hapus isi file
cp /etc/apt/sources.list /etc/apt/sources.list.backup.$(date +%Y%m%d_%H%M%S)
sh -c '> /etc/apt/sources.list'
echo "🗑️  Menghapus $old_line_count lines dari sources.list"


# Hapus cdrom dan tambah repo
# Tambah repository optimized
sed -i '/^deb cdrom/d' /etc/apt/sources.list
echo "🔄 Menambah repository optimized..."
 
tee /etc/apt/sources.list > /dev/null << EOF
cat >> /etc/apt/sources.list << EOF
# Debian 12 Bookworm - Optimized Repository
 
# Last update: $(date)
# Debian 12 Repos
deb http://deb.debian.org/debian/ bookworm main contrib non-free non-free-firmware
deb http://deb.debian.org/debian/ bookworm main contrib non-free non-free-firmware
deb-src http://deb.debian.org/debian/ bookworm main contrib non-free non-free-firmware
deb-src http://deb.debian.org/debian/ bookworm main contrib non-free non-free-firmware
Line 45: Line 42:
EOF
EOF


# Update
# Validasi penulisan
apt update && apt upgrade -y
new_line_count=$(wc -l < /etc/apt/sources.list)
if [ $new_line_count -eq 0 ]; then
    echo "❌ Error: Repository gagal ditulis!"
    echo "🔄 Restoring backup..."
    cp $backup_path /etc/apt/sources.list
    exit 1
fi
 
# Test repository
echo "🔍 Testing repository configuration..."
apt update > /dev/null 2>&1


echo "✅ Done!"
if [ $? -eq 0 ]; then
    echo "✅ Repository berhasil diupdate!"
    echo "📊 New repository lines: $new_line_count"
    echo "📦 Available packages: $(apt list 2>/dev/null | wc -l)"
else
    echo "❌ Error: Repository test failed!"
    echo "🔄 Restoring backup..."
    sudo cp $backup_path /etc/apt/sources.list
    apt update && apt upgrade -y
    echo "Backup restored!"
fi
</syntaxhighlight>Simpan script tersebut.
</syntaxhighlight>Simpan script tersebut.



Latest revision as of 23:31, 7 October 2025

ShellScripting

Buat file baru dengan nama tambah_repositori.sh

nano tambah_repositori.sh

isikan script berikut ini :

#!/bin/bash
# Script dengan validasi setelah hapus

echo "=== SMART REPOSITORY REPLACEMENT ==="

# Validasi system
if [ ! -f /etc/apt/sources.list ]; then
    echo "❌ Error: sources.list tidak ditemukan!"
    exit 1
fi

# Backup dengan timestamp
timestamp=$(date +%Y%m%d_%H%M%S)
backup_path="/root/sources.list.backup.$timestamp"
cp /etc/apt/sources.list $backup_path
echo "📁 Backup created: $backup_path"

# Simpan line count sebelum hapus
old_line_count=$(wc -l < /etc/apt/sources.list)

# Hapus isi file
sh -c '> /etc/apt/sources.list'
echo "🗑️  Menghapus $old_line_count lines dari sources.list"

# Tambah repository optimized
echo "🔄 Menambah repository optimized..."
tee /etc/apt/sources.list > /dev/null << EOF
# Debian 12 Bookworm - Optimized Repository
# Last update: $(date)
deb http://deb.debian.org/debian/ bookworm main contrib non-free non-free-firmware
deb-src http://deb.debian.org/debian/ bookworm main contrib non-free non-free-firmware
deb http://deb.debian.org/debian/ bookworm-updates main contrib non-free non-free-firmware
deb-src http://deb.debian.org/debian/ bookworm-updates main contrib non-free non-free-firmware
deb http://deb.debian.org/debian/ bookworm-backports main contrib non-free non-free-firmware
deb-src http://deb.debian.org/debian/ bookworm-backports main contrib non-free non-free-firmware
deb http://security.debian.org/debian-security/ bookworm-security main contrib non-free non-free-firmware
deb-src http://security.debian.org/debian-security/ bookworm-security main contrib non-free non-free-firmware
EOF

# Validasi penulisan
new_line_count=$(wc -l < /etc/apt/sources.list)
if [ $new_line_count -eq 0 ]; then
    echo "❌ Error: Repository gagal ditulis!"
    echo "🔄 Restoring backup..."
    cp $backup_path /etc/apt/sources.list
    exit 1
fi

# Test repository
echo "🔍 Testing repository configuration..."
apt update > /dev/null 2>&1

if [ $? -eq 0 ]; then
    echo "✅ Repository berhasil diupdate!"
    echo "📊 New repository lines: $new_line_count"
    echo "📦 Available packages: $(apt list 2>/dev/null | wc -l)"
else
    echo "❌ Error: Repository test failed!"
    echo "🔄 Restoring backup..."
    sudo cp $backup_path /etc/apt/sources.list
    apt update && apt upgrade -y
    echo "Backup restored!"
fi

Simpan script tersebut. beri hak akses eksekusi

chmod +x tambah_repositori.sh

jalankan script

./tambah_repositori.sh