1. ###########################################################################
  2. ## ##
  3. ## Ubuntu Mirror Script ##
  4. ## ##
  5. ## Creation: 06.07.2008 ##
  6. ## Last Update: 02.06.2012 ##
  7. ## ##
  8. ## Copyright (c) 2008-2012 by Georg Kainzbauer <[email protected]> ##
  9. ## ##
  10. ## This program is free software; you can redistribute it and/or modify ##
  11. ## it under the terms of the GNU General Public License as published by ##
  12. ## the Free Software Foundation; either version 2 of the License, or ##
  13. ## (at your option) any later version. ##
  14. ## ##
  15. ###########################################################################
  16. #!/bin/sh
  17. appname=`basename $0`
  18. nowdate=`date +%Y%m%d%H%M%S`
  19. echo $$ > ${nowdate}.${appname}.pid
  20. # Ubuntu mirror server and mirror directory
  21. #SOURCE_SRV=cn.archive.ubuntu.com
  22. SOURCE_SRV=archive.ubuntu.com
  23. #SOURCE_SRV=mirrors.163.com
  24. SOURCE_DIR=/ubuntu
  25. # Distribution, section and architecture list
  26. DIST=precise,precise-security,precise-updates,precise-backports,precise-proposed
  27. SECTION=main,main/debian-installer,restricted,restricted/debian-installer,universe,universe/debian-installer,multiverse,multiverse/debian-installer
  28. ARCH=i386,amd64
  29. # Local mirror directory
  30. #MIRRORDIR=/var/ftp/pub/linux/ubuntu/
  31. MIRRORDIR=/home/ubuntu/
  32. # Log file
  33. LOGFILE=/var/log/ubuntu_mirror.log
  34. # Debug file (if you do not want to debug the download process set this option to "/dev/null")
  35. DEBUGFILE=/var/log/ubuntu_mirror.debug
  36. # Who will be informed in case if anything goes wrong (if you do not want to be informed via mail, set this option to "")
  37. #MAILNOTIFY="root@localhost"
  38. # Lock file
  39. LOCK=/var/tmp/ubuntu_mirror.lock
  40. ##################################################################
  41. # NORMALY THERE IS NO NEED TO CHANGE ANYTHING BELOW THIS COMMENT #
  42. ##################################################################
  43. function log()
  44. {
  45. echo `date +%d.%m.%Y%t%H:%M:%S` " LOG:" $1 >>${LOGFILE}
  46. }
  47. function error()
  48. {
  49. echo `date +%d.%m.%Y%t%H:%M:%S` " ERROR:" $1 >>${LOGFILE}
  50. if [ -n "$MAILNOTIFY" ] ; then
  51. echo `date +%d.%m.%Y%t%H:%M:%S` " ERROR:" $1 | mail -s "ERROR while synchronizing Ubuntu" $MAILNOTIFY
  52. fi
  53. echo $1 | grep "Lockfile" >/dev/null
  54. if [ $? = 1 ] ; then
  55. rm -f ${LOCK}
  56. fi
  57. rm ${nowdate}.${appname}.pid
  58. exit 1
  59. }
  60. function status()
  61. {
  62. case "$1" in
  63. 0)
  64. log "Synchronization completed."
  65. ;;
  66. 1)
  67. error "DEBMIRROR: Connection closed"
  68. ;;
  69. 2)
  70. error "DEBMIRROR: Timeout"
  71. ;;
  72. *)
  73. error "DEBMIRROR: Unknown error $1"
  74. ;;
  75. esac
  76. }
  77. if [ -f ${LOCK} ] ; then
  78. error "Lockfile ${LOCK} exists."
  79. fi
  80. touch ${LOCK}
  81. # Create local mirror directory if not exists
  82. if [ ! -d ${MIRRORDIR} ] ; then
  83. log "Creating local mirror directory."
  84. mkdir -p ${MIRRORDIR}
  85. fi
  86. log "Starting Ubuntu download process."
  87. debmirror -v -e http -h ${SOURCE_SRV} -r ${SOURCE_DIR} --ignore-release-gpg --dist=${DIST} --section=${SECTION} --arch=${ARCH} ${MIRRORDIR} >> ${DEBUGFILE} 2>&1
  88. status $?
  89. rm -f ${LOCK}
  90. rm ${nowdate}.${appname}.pid
  91. exit 0