Shell Scripting : Menambahkan Repositori Debian: Difference between revisions
m (Protected "Shell Scripting : Menambahkan Repositori Debian" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite)) [cascading]) |
No edit summary |
||
(3 intermediate revisions 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 dengan validasi setelah hapus | ||
echo "=== SMART REPOSITORY REPLACEMENT ===" | |||
# Debian 12 | # 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 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 20: | Line 42: | ||
EOF | EOF | ||
apt update && apt upgrade -y | # Validasi penulisan | ||
echo " | 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 | |||
</syntaxhighlight>Simpan script tersebut. | </syntaxhighlight>Simpan script tersebut. | ||
Latest revision as of 23:31, 7 October 2025

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