yum repo updating using bash script in CentOS 7 | yum server update


This is a bash script for update yum repository. This script will work if new RPM packages found in repository folder. There is no required to manual to update repository for new package. Just create script and set cron.




Create Script :


vi /usr/local/bin/linuxtopic

#!/bin/bash
# Created by Lokesh Carpenter #
# web www.linuxtopic.com      #


if [[ ! $(ls /tmp/linuxtopic.txt) ]];then
   echo "Updating yum repository"
   echo $(date)
   /usr/bin/createrepo  /var/www/html/linuxtopic
   echo "yum repository  created successfully"
   echo "------------------------------------"
   echo "generating package list"
   ls /var/www/html/linuxtopic  > /tmp/linuxtopic.txt


else
   echo "checking new package"
   ls /var/www/html/linuxtopic > /tmp/linuxtopic-tmp.txt
   diff <(sort /tmp/linuxtopic.txt) <(sort /tmp/linuxtopic-tmp.txt)  > /dev/null
   if [[ $? = 0 ]]; then
      echo "repository already up to date, no new package founded!"
   else
      echo "updating yum repository"
      echo "------------------------------------"
      echo $(date)
      /usr/bin/createrepo --update /var/www/html/linuxtopic
      echo "linuxtopic repository updated successfully"
      pkg=`diff <(sort /tmp/linuxtopic.txt) <(sort /tmp/linuxtopic-tmp.txt) | awk '{print $2}'`      
      echo "updating package list"
      ls /var/www/html/linuxtopic  > /tmp/linuxtopic.txt
      echo "following packages updated"
      echo "$pkg"
   fi;
fi;


Explanation :


First if condition will  check package list file  if file not found yum repo will create and generate package list file.


If package list file founded else statement will work and generate new package list
And check Difference between  old package list to new package list.   


After checking difference Second if statement will execute and check condition if condition is true OR no diff found echo “already updated”  otherwise repo will update in false OR diff found and echo “updated packages list”

Video Tutorial - English



Video Tutorial - Hindi





Thanks,

Please Share, Like and Comment for more easy

No comments

Powered by Blogger.