[wiki]AutoWeeklyUpdateHowTo

参与到Ubuntu的翻译中来
回复
头像
oneleaf
论坛管理员
帖子: 10441
注册时间: 2005-03-27 0:06
系统: Ubuntu 12.04

[wiki]AutoWeeklyUpdateHowTo

#1

帖子 oneleaf » 2005-08-02 11:13

Automatic Package Updates Using cron-apt

The package cron-apt (universe repository) is designed to automatically update the package list and download upgraded packages. Therefore it basically calls the commands apt-get update and apt-get dist-upgrade -d. To make cron-apt be executed regularly, create a link in /etc/cron.daily or /etc/cron.weekly with sudo ln -s /usr/sbin/cron-apt /etc/cron.daily/. cron-apt only downloads new packages, to install them you need to execute sudo apt-get dist-upgrade manually.

When using cron-apt you don't have to follow the steps below.
Automatic Weekly Package Updates Using Cron And Apt-Get

WARNING: As with any system changes, the potential for creating new or additional problems may occur.
Please be sure to backup your data and configurations! Use this document at your own risk. FIRST DRAFT Document.

Keeping your Ubuntu (or Debian based) system packages regularly updated not only helps to better secure your system, but also helps keep it running bug free. This HowTo discusses how to setup a very easy and simple Cron job on your system that will update your system packages weekly, and remove any old unused packages no longer installed after updating.

(This document assumes an always or mostly always on system with a broadband, not dial-up, Internet connection.)
Creating the Weekly Cron Job File

First you will need to create the cron job file. You can use a simple text editor to create the file and save it in your home directory. In Ubuntu, open Applications > Accessories > Text Editor. In the Text Editor, type the following lines:

#!/bin/bash
apt-get update
apt-get upgrade -y
apt-get autoclean

Now click Save and name the file something like "autoupdt". The default directory should be your home directory, but verify to be certain. (The steps following this will assume the file has been saved in your home directory.)
Moving the Cron Job File to Cron.Weekly

Now that you've created the cron job file, it needs to be moved into the weekly cron directory so that it will be run automatically on a weekly basis. To do this, we first need to open a command line terminal. In Ubuntu, click Applications > System Tools > Terminal. Now you should see your terminal prompt. At the prompt type "ls" and press Enter (or Return on some keyboards). In the list you should see your newly created file "autoupdt".

user@system:~$cd ~
user@system:~$ls

Now that we know the file is there, we need to move the file to the proper directory. Type the following command at the command line promt to move the file:

user@system:~$sudo mv autoupdt /etc/cron.weekly

(You may be prompted to enter your sudo password, which is YOUR password.) Now we need to move to the Cron directory to verify the file was moved. At the command line prompt type:

user@system:~$cd /etc/cron.weekly
user@system:~$ls

You should see the "autoupdt" file in the list. (If not, try the previous move command again.)
Making the Cron Job File Executable

Now that the file is created and ready to be run weekly by cron, we still need to make the file executable in order for cron to be able to run it. Since you're already in the cron.weekly directory, all you need to do is enter this command at the prompt:

user@system:~$sudo chmod 755 autoupdt

(Again you may be prompted for your sudo password.)
Finished

Now that the file is executable, you are finished. The cron job will run weekly and update your source list (to see if there are any new package updates), update your packages as found and needed, then clean out any old unused no longer installed packages. You can still update using Synaptic Package Manager or apt-get at the command line, but now you can relax a bit knowing your system will be updated automatically on a weekly basis.
Alternatively: Package Updates with Logging using Aptitude

Apt-get doesn't write details of changes it makes to log files. Aptitude logs to /var/log/aptitude, and can mail a report to root, which can be redirected to the sudo user if Postfix is set up to do this. As with the examples above, create this script as /etc/cron.weekly/autoupdt and make it executable:

#!/bin/bash (-)

tmpfile=$(mktemp)

echo "aptitude update" >> ${tmpfile}
aptitude update >> ${tmpfile} 2>&1
echo "" >> ${tmpfile}
echo "aptitude dist-upgrade" >> ${tmpfile}
aptitude -y dist-upgrade >> ${tmpfile} 2>&1
echo "" >> ${tmpfile}
echo "aptitude clean" >> ${tmpfile}
aptitude clean >> ${tmpfile} 2>&1

mail -s "Aptitude cron $(date)" root ${tmpfile}
rm -f ${tmpfile}

Here's a script (based on the above) that is useful if you don't have sendmail or postfix installed on your computer. It collects all the output from aptitude and manually sends an email using a mail server you specify. You must also specify the recipient of the email. As with the examples above, create this script as /etc/cron.weekly/autoupdt and make it executable:

#!/bin/bash
#
# use aptitude to automatically install updates. log and email any
# changes.
#

#
# variables to change
#

# address to send results to
MAILTO=your@email.address
# host name of smtp server
MAIL=mail.yourisp.com


#
# script is below here (do not change)
#

tmpfile=$(mktemp)

#
# smtp setup commands
#

echo "helo $(hostname)" >> ${tmpfile}
echo "mail from: root@$(hostname)" >> ${tmpfile}
echo "rcpt to: $MAILTO" >> ${tmpfile}
echo 'data'>> ${tmpfile}
echo "subject: Aptitude cron $(date)" >> ${tmpfile}

#
# actually run aptitude to do the updates, logging its output
#

echo "aptitude update" >> ${tmpfile}
aptitude update >> ${tmpfile} 2>&1
echo "" >> ${tmpfile}
echo "aptitude dist-upgrade" >> ${tmpfile}
aptitude -y dist-upgrade >> ${tmpfile} 2>&1
echo "" >> ${tmpfile}
echo "aptitude clean" >> ${tmpfile}
aptitude clean >> ${tmpfile} 2>&1

#
# i get a lot of escaped new lines in my output. so the following
# removes them. this could be greatly improved

tmpfile2=$(mktemp)
cat ${tmpfile} | sed 's/\r\r/\n/g'|sed 's/\r//g' > ${tmpfile2}
mv ${tmpfile2} ${tmpfile}

#
# smtp close commands
#

echo >> ${tmpfile}
echo '.' >> ${tmpfile}
echo 'quit' >> ${tmpfile}
echo >> ${tmpfile}

#
# now send the email (and ignore output)
#

telnet $MAIL 25 < ${tmpfile} > /dev/null 2> /dev/null

#
# and remove temp files
#

rm -f ${tmpfile}


https://wiki.ubuntu.com/AutoWeeklyUpdateHowTo
当净其意如虚空,远离妄想及诸取,令心所向皆无碍
lattice
帖子: 36
注册时间: 2005-07-06 18:01

#2

帖子 lattice » 2005-08-03 10:03

认领先
lattice
帖子: 36
注册时间: 2005-07-06 18:01

#3

帖子 lattice » 2005-08-03 11:13

利用cron-apt来进行自动更新软件包

cron-apt包(universe仓库)设计用来自动更新软件包列表和下载更新的软件包。因此它从根本上说是调用apt-get和apt-get dist-upgrade -d命令来完成的。为了让cron-apt能够正常运行,需要在/etc/cron.daily目录或者/etc/cron.weekly目录里用如下命令来创建链接:sudo ln -s /usr/sbin/cron-apt /etc/cron.daily/.cron-apt,这个命令只是用来下载新的软件包,如果你需要安装这些软件包还需要手工执行sudo apt-get dist-upgrade。

使用cron-apt的时候你无需遵从下面这些步骤。


使用cron和apt-get来进行每周自动软件更新

警告:如果有任何系统变动,那么新的或额外的问题可能出现。请确认备份好你的数据和配置!由本文造成的损失自行负责。这是第一版的草稿。

保持你的Ubuntu(或基于Debian)系统的软件包正常更新不仅能够帮助使你的系统更安全,而且能够使它运行时有更少的bug。这篇HowTo讨论了如何在你的系统上创建简单容易的cron任务来进行系统每周自动软件包更新、更新后删除老的没用的软件包。

(这篇文档假设假设你的系统一直或者几乎一直是用宽带网络连接而不是拨号)

1、创建cron周更新任务文件

首先你得创建cron任务文件。你可以使用简单的文本编辑器来创建这个文件然后保存在你的主目录里。在Ubuntu中,打开“应用程序”-〉“附件”-〉“文本编辑器”。在文本编辑器里,键入以下内容:

代码: 全选

#!/bin/bash 
apt-get update 
apt-get upgrade -y 
apt-get autoclean
现在点击保存,命名为如“autoupdt”之类的名称。默认目录为你的主目录,需要你确认好的。(以下步骤假设这个文件保存在了你的主目录里。)

2、把cron任务文件移到cron.weekly

现在你已经建好了cron任务文件,需要把它移到cron周计划目录里面去,这样才能够使它能够每周自动运行。要做到这一点,我们首先得打开一个命令终端。在Ubuntu里,点击“应用程序”-〉“系统工具”-〉“终端”。现在你应该看到命令提示符了。提示符后键入“ls”然后回车。在文件列表里尼应该看见新建的文件“autoupdt”。

代码: 全选

user@system:~$cd ~ 
user@system:~$ls
现在你知道了文件在哪里了,我们需要将此文件移到正确的目录里面去。在命令行提示符后键入以下命令来移动文件:

代码: 全选

user@system:~$sudo mv autoupdt /etc/cron.weekly 
(系统可能提示你输入sudo密码,那就是你的密码。译者注:建立系统的第一个用户的密码)现在我们需要到Cron目录去证实一下这个文件被移过来了。命令提示符下输入:

代码: 全选

user@system:~$cd /etc/cron.weekly 
user@system:~$ls 
你应该在文件列表里看见“autoupdt”文件。(如果看不到,试着将上面的移动命令重做一遍)

3、把Cron任务文件变为可执行

现在文件创建好了,可以给cron来每周执行了,为了能使cron运行它,我们还需要把文件变为可执行文件。因为你已经在cron.weekly目录里了,你所要做的就是在提示符下键入以下命令:

代码: 全选

user@system:~$sudo chmod 755 autoupdt 
(系统可能又提示你要输入sudo密码)

完成了!

现在那个任务文件变成可执行,你的工作就完成了。cron任务将每周运行,更新你的源列表(看看是否有新的软件包更新)、如果找到你需要的软件包那么将进行更新、然后把不再使用的软件包清除掉。你仍然可以使用新立得软件包管理器或者在命令行上使用apt-get来更新,但是现在你知道你的系统将每周自动进行更新,你就可以轻松一下下啦。


另一种方法:使用有日志记录的Aptitude进行软件包更新

Apt-get不在日志文件中记录改变的细节。Aptitude则将日志记录在/var/log/aptitude,也可以向root发送一份报告,如果Postfix设置好了的话还会重定向给sudo用户。对于上面的这个例子,建立一个/etc/cron.weekly/autoupdt脚本,并且将其变为可执行文件:

代码: 全选

#!/bin/bash (-) 

tmpfile=$(mktemp) 

echo "aptitude update" >> ${tmpfile} 
aptitude update >> ${tmpfile} 2>&1 
echo "" >> ${tmpfile} 
echo "aptitude dist-upgrade" >> ${tmpfile} 
aptitude -y dist-upgrade >> ${tmpfile} 2>&1 
echo "" >> ${tmpfile} 
echo "aptitude clean" >> ${tmpfile} 
aptitude clean >> ${tmpfile} 2>&1 

mail -s "Aptitude cron $(date)" root ${tmpfile} 
rm -f ${tmpfile} 
如果你的计算机上没有装sendmail或者postfix服务,这里我提供的脚本(基于上面的例子)就很有用了。它提取了aptitude的输出然后利用你设置的邮件服务器手动地发送出一封邮件。你必须设置好邮件的接收者。对于上面的例子来说,创建/etc/cron.weekly/autoupdt脚本并且将其变为可执行文件:

代码: 全选

#!/bin/bash 
# 
# use aptitude to automatically install updates. log and email any 
# changes. 
# 

# 
# variables to change 
# 

# address to send results to 
MAILTO=your@email.address 
# host name of smtp server 
MAIL=mail.yourisp.com 


# 
# script is below here (do not change) 
# 

tmpfile=$(mktemp) 

# 
# smtp setup commands 
# 

echo "helo $(hostname)" >> ${tmpfile} 
echo "mail from: root@$(hostname)" >> ${tmpfile} 
echo "rcpt to: $MAILTO" >> ${tmpfile} 
echo 'data'>> ${tmpfile} 
echo "subject: Aptitude cron $(date)" >> ${tmpfile} 

# 
# actually run aptitude to do the updates, logging its output 
# 

echo "aptitude update" >> ${tmpfile} 
aptitude update >> ${tmpfile} 2>&1 
echo "" >> ${tmpfile} 
echo "aptitude dist-upgrade" >> ${tmpfile} 
aptitude -y dist-upgrade >> ${tmpfile} 2>&1 
echo "" >> ${tmpfile} 
echo "aptitude clean" >> ${tmpfile} 
aptitude clean >> ${tmpfile} 2>&1 

# 
# i get a lot of escaped new lines in my output. so the following 
# removes them. this could be greatly improved 

tmpfile2=$(mktemp) 
cat ${tmpfile} | sed 's/\r\r/\n/g'|sed 's/\r//g' > ${tmpfile2} 
mv ${tmpfile2} ${tmpfile} 

# 
# smtp close commands 
# 

echo >> ${tmpfile} 
echo '.' >> ${tmpfile} 
echo 'quit' >> ${tmpfile} 
echo >> ${tmpfile} 

# 
# now send the email (and ignore output) 
# 

telnet $MAIL 25 < ${tmpfile} > /dev/null 2> /dev/null 

# 
# and remove temp files 
# 

rm -f ${tmpfile} 
头像
oneleaf
论坛管理员
帖子: 10441
注册时间: 2005-03-27 0:06
系统: Ubuntu 12.04

#4

帖子 oneleaf » 2005-08-22 21:46

当净其意如虚空,远离妄想及诸取,令心所向皆无碍
头像
millenniumdark
论坛版主
帖子: 4159
注册时间: 2005-07-02 14:41
系统: Ubuntu 14.04 (Kylin)
联系:

#5

帖子 millenniumdark » 2006-07-30 21:25

回复