mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-28 23:20:10 +08:00
Merge remote-tracking branch 'LCTT/master'
This commit is contained in:
commit
1e358ed25f
@ -1,19 +1,16 @@
|
||||
Part-II 树莓派自建 NAS 云盘之数据自动备份
|
||||
树莓派自建 NAS 云盘之——数据自动备份
|
||||
======
|
||||
> 把你的树莓派变成数据的安全之所。
|
||||
|
||||
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/brain_data.png?itok=RH6NA32X)
|
||||
|
||||
在《树莓派自建 NAS 云盘》系列的 [第一篇][1] 文章中,我们讨论了建立 NAS 的一些基本步骤,添加了两块 1TB 的存储硬盘驱动(一个用于数据存储,一个用于数据备份),并且通过 网络文件系统(NFS)将数据存储盘挂载到远程终端上。本文是此系列的第二篇文章,我们将探讨数据自动备份。数据自动备份保证了数据的安全,为硬件损坏后的数据恢复提供便利以及减少了文件误操作带来的不必要的麻烦。
|
||||
|
||||
|
||||
在《树莓派自建 NAS 云盘》系列的 [第一篇][1] 文章中,我们讨论了建立 NAS 的一些基本步骤,添加了两块 1TB 的存储硬盘驱动(一个用于数据存储,一个用于数据备份),并且通过网络文件系统(NFS)将数据存储盘挂载到远程终端上。本文是此系列的第二篇文章,我们将探讨数据自动备份。数据自动备份保证了数据的安全,为硬件损坏后的数据恢复提供便利以及减少了文件误操作带来的不必要的麻烦。
|
||||
|
||||
![](https://opensource.com/sites/default/files/uploads/nas_part2.png)
|
||||
|
||||
|
||||
|
||||
### 备份策略
|
||||
|
||||
我们就从为小型 NAS 构想一个备份策略着手开始吧。我建议每天有时间节点有计划的去备份数据,以防止干扰到我们正常的访问 NAS,比如备份时间点避开正在访问 NAS 并写入文件的时间点。举个例子,你可以每天凌晨 2 点去进行数据备份。
|
||||
我们就从为小型 NAS 构想一个备份策略着手开始吧。我建议每天有时间节点、有计划的去备份数据,以防止干扰到我们正常的访问 NAS,比如备份时间点避开正在访问 NAS 并写入文件的时间点。举个例子,你可以每天凌晨 2 点去进行数据备份。
|
||||
|
||||
另外,你还得决定每天的备份需要被保留的时间长短,因为如果没有时间限制,存储空间很快就会被用完。一般每天的备份保留一周便可以,如果数据出了问题,你便可以很方便的从备份中恢复出来原数据。但是如果需要恢复数据到更久之前怎么办?可以将每周一的备份文件保留一个月、每个月的备份保留更长时间。让我们把每月的备份保留一年时间,每一年的备份保留更长时间、例如五年。
|
||||
|
||||
@ -24,27 +21,24 @@ Part-II 树莓派自建 NAS 云盘之数据自动备份
|
||||
* 每年 12 个月备份
|
||||
* 每五年 5 个年备份
|
||||
|
||||
|
||||
你应该还记得,我们搭建的备份盘和数据盘大小相同(每个 1 TB)。如何将不止 10 个 1TB 数据的备份从数据盘存放到只有 1TB 大小的备份盘呢?如果你创建的是完整备份,这显然不可能。因此,你需要创建增量备份,它是每一份备份都基于上一份备份数据而创建的。增量备份方式不会每隔一天就成倍的去占用存储空间,它每天只会增加一点占用空间。
|
||||
|
||||
以下是我的情况:我的 NAS 自 2016 年 8 月开始运行,备份盘上有 20 个备份。目前,我在数据盘上存储了 406GB 的文件。我的备份盘用了 726GB。当然,备份盘空间使用率在很大程度上取决于数据的更改频率,但正如你所看到的,增量备份不会占用 20 个完整备份所需的空间。然而,随着时间的推移,1TB 空间也可能不足以进行备份。一旦数据增长接近 1TB 限制(或任何备份盘容量),应该选择更大的备份盘空间并将数据移动转移过去。
|
||||
|
||||
### 利用 rsync 进行数据备份
|
||||
|
||||
利用 rsync 命令行工具可以生成完整备份。
|
||||
利用 `rsync` 命令行工具可以生成完整备份。
|
||||
|
||||
```
|
||||
pi@raspberrypi:~ $ rsync -a /nas/data/ /nas/backup/2018-08-01
|
||||
|
||||
```
|
||||
|
||||
这段命令将挂载在 /nas/data/ 目录下的数据盘中的数据进行了完整的复制备份。备份文件保存在 /nas/backup/2018-08-01 目录下。`-a` 参数是以归档模式进行备份,这将会备份所有的元数据,例如文件的修改日期、权限、拥有者以及软连接文件。
|
||||
这段命令将挂载在 `/nas/data/` 目录下的数据盘中的数据进行了完整的复制备份。备份文件保存在 `/nas/backup/2018-08-01` 目录下。`-a` 参数是以归档模式进行备份,这将会备份所有的元数据,例如文件的修改日期、权限、拥有者以及软连接文件。
|
||||
|
||||
现在,你已经在 8 月 1 日创建了完整的初始备份,你将在 8 月 2 日创建第一个增量备份。
|
||||
|
||||
```
|
||||
pi@raspberrypi:~ $ rsync -a --link-dest /nas/backup/2018-08-01/ /nas/data/ /nas/backup/2018-08-02
|
||||
|
||||
```
|
||||
|
||||
上面这行代码又创建了一个关于 `/nas/data` 目录中数据的备份。备份路径是 `/nas/backup/2018-08-02`。这里的参数 `--link-dest` 指定了一个备份文件所在的路径。这样,这次备份会与 `/nas/backup/2018-08-01` 的备份进行比对,只备份已经修改过的文件,未做修改的文件将不会被复制,而是创建一个到上一个备份文件中它们的硬链接。
|
||||
@ -53,142 +47,81 @@ pi@raspberrypi:~ $ rsync -a --link-dest /nas/backup/2018-08-01/ /nas/data/ /nas/
|
||||
|
||||
![](https://opensource.com/sites/default/files/uploads/backup_flow.png)
|
||||
|
||||
左侧框是在进行了第二次备份后的原数据状态。中间的盒子是昨天的备份。昨天的备份中只有图片 `file1.jpg` 并没有 `file2.txt` 。右侧的框反映了今天的增量备份。增量备份命令创建昨天不存在的 `file2.txt`。由于 `file1.jpg` 自昨天以来没有被修改,所以今天创建了一个硬链接,它不会额外占用磁盘上的空间。
|
||||
左侧框是在进行了第二次备份后的原数据状态。中间的方块是昨天的备份。昨天的备份中只有图片 `file1.jpg` 并没有 `file2.txt` 。右侧的框反映了今天的增量备份。增量备份命令创建昨天不存在的 `file2.txt`。由于 `file1.jpg` 自昨天以来没有被修改,所以今天创建了一个硬链接,它不会额外占用磁盘上的空间。
|
||||
|
||||
### 自动化备份
|
||||
|
||||
你肯定也不想每天凌晨去输入命令进行数据备份吧。你可以创建一个任务定时去调用下面的脚本让它自动化备份
|
||||
你肯定也不想每天凌晨去输入命令进行数据备份吧。你可以创建一个任务定时去调用下面的脚本让它自动化备份。
|
||||
|
||||
```
|
||||
#!/bin/bash
|
||||
|
||||
|
||||
|
||||
TODAY=$(date +%Y-%m-%d)
|
||||
|
||||
DATADIR=/nas/data/
|
||||
|
||||
BACKUPDIR=/nas/backup/
|
||||
|
||||
SCRIPTDIR=/nas/data/backup_scripts
|
||||
|
||||
LASTDAYPATH=${BACKUPDIR}/$(ls ${BACKUPDIR} | tail -n 1)
|
||||
|
||||
TODAYPATH=${BACKUPDIR}/${TODAY}
|
||||
|
||||
if [[ ! -e ${TODAYPATH} ]]; then
|
||||
|
||||
mkdir -p ${TODAYPATH}
|
||||
|
||||
mkdir -p ${TODAYPATH}
|
||||
fi
|
||||
|
||||
|
||||
|
||||
rsync -a --link-dest ${LASTDAYPATH} ${DATADIR} ${TODAYPATH} $@
|
||||
|
||||
|
||||
|
||||
${SCRIPTDIR}/deleteOldBackups.sh
|
||||
|
||||
```
|
||||
|
||||
第一段代码指定了数据路径、备份路劲、脚本路径以及昨天和今天的备份路径。第二段代码调用 rsync 命令。最后一段代码执行 `deleteOldBackups.sh` 脚本,它会清除一些过期的没有必要的备份数据。如果不想频繁的调用 `deleteOldBackups.sh`,你也可以手动去执行它。
|
||||
第一段代码指定了数据路径、备份路径、脚本路径以及昨天和今天的备份路径。第二段代码调用 `rsync` 命令。最后一段代码执行 `deleteOldBackups.sh` 脚本,它会清除一些过期的没有必要的备份数据。如果不想频繁的调用 `deleteOldBackups.sh`,你也可以手动去执行它。
|
||||
|
||||
下面是今天讨论的备份策略的一个简单完整的示例脚本。
|
||||
|
||||
```
|
||||
#!/bin/bash
|
||||
|
||||
BACKUPDIR=/nas/backup/
|
||||
|
||||
|
||||
|
||||
function listYearlyBackups() {
|
||||
|
||||
for i in 0 1 2 3 4 5
|
||||
|
||||
do ls ${BACKUPDIR} | egrep "$(date +%Y -d "${i} year ago")-[0-9]{2}-[0-9]{2}" | sort -u | head -n 1
|
||||
|
||||
done
|
||||
|
||||
for i in 0 1 2 3 4 5
|
||||
do ls ${BACKUPDIR} | egrep "$(date +%Y -d "${i} year ago")-[0-9]{2}-[0-9]{2}" | sort -u | head -n 1
|
||||
done
|
||||
}
|
||||
|
||||
|
||||
|
||||
function listMonthlyBackups() {
|
||||
|
||||
for i in 0 1 2 3 4 5 6 7 8 9 10 11 12
|
||||
|
||||
do ls ${BACKUPDIR} | egrep "$(date +%Y-%m -d "${i} month ago")-[0-9]{2}" | sort -u | head -n 1
|
||||
|
||||
done
|
||||
|
||||
for i in 0 1 2 3 4 5 6 7 8 9 10 11 12
|
||||
do ls ${BACKUPDIR} | egrep "$(date +%Y-%m -d "${i} month ago")-[0-9]{2}" | sort -u | head -n 1
|
||||
done
|
||||
}
|
||||
|
||||
|
||||
|
||||
function listWeeklyBackups() {
|
||||
|
||||
for i in 0 1 2 3 4
|
||||
|
||||
do ls ${BACKUPDIR} | grep "$(date +%Y-%m-%d -d "last monday -${i} weeks")"
|
||||
|
||||
done
|
||||
|
||||
for i in 0 1 2 3 4
|
||||
do ls ${BACKUPDIR} | grep "$(date +%Y-%m-%d -d "last monday -${i} weeks")"
|
||||
done
|
||||
}
|
||||
|
||||
|
||||
|
||||
function listDailyBackups() {
|
||||
|
||||
for i in 0 1 2 3 4 5 6
|
||||
|
||||
do ls ${BACKUPDIR} | grep "$(date +%Y-%m-%d -d "-${i} day")"
|
||||
|
||||
done
|
||||
|
||||
for i in 0 1 2 3 4 5 6
|
||||
do ls ${BACKUPDIR} | grep "$(date +%Y-%m-%d -d "-${i} day")"
|
||||
done
|
||||
}
|
||||
|
||||
|
||||
|
||||
function getAllBackups() {
|
||||
|
||||
listYearlyBackups
|
||||
|
||||
listMonthlyBackups
|
||||
|
||||
listWeeklyBackups
|
||||
|
||||
listDailyBackups
|
||||
|
||||
listYearlyBackups
|
||||
listMonthlyBackups
|
||||
listWeeklyBackups
|
||||
listDailyBackups
|
||||
}
|
||||
|
||||
|
||||
|
||||
function listUniqueBackups() {
|
||||
|
||||
getAllBackups | sort -u
|
||||
|
||||
getAllBackups | sort -u
|
||||
}
|
||||
|
||||
|
||||
|
||||
function listBackupsToDelete() {
|
||||
|
||||
ls ${BACKUPDIR} | grep -v -e "$(echo -n $(listUniqueBackups) |sed "s/ /\\\|/g")"
|
||||
|
||||
ls ${BACKUPDIR} | grep -v -e "$(echo -n $(listUniqueBackups) |sed "s/ /\\\|/g")"
|
||||
}
|
||||
|
||||
|
||||
|
||||
cd ${BACKUPDIR}
|
||||
|
||||
listBackupsToDelete | while read file_to_delete; do
|
||||
|
||||
rm -rf ${file_to_delete}
|
||||
|
||||
rm -rf ${file_to_delete}
|
||||
done
|
||||
|
||||
```
|
||||
|
||||
这段脚本会首先根据你的备份策略列出所有需要保存的备份文件,然后它会删除那些再也不需要了的备份目录。
|
||||
@ -197,7 +130,6 @@ done
|
||||
|
||||
```
|
||||
0 2 * * * /nas/data/backup_scripts/daily.sh
|
||||
|
||||
```
|
||||
|
||||
有关创建定时任务请参考 [cron 创建定时任务][2]。
|
||||
@ -218,12 +150,12 @@ via: https://opensource.com/article/18/8/automate-backups-raspberry-pi
|
||||
作者:[Manuel Dewald][a]
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
译者:[jrg](https://github.com/jrglinux)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
校对:[wxy](https://github.com/wxy)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/ntlx
|
||||
[1]: https://opensource.com/article/18/7/network-attached-storage-Raspberry-Pi
|
||||
[1]: https://linux.cn/article-10104-1.html
|
||||
[2]: https://opensource.com/article/17/11/how-use-cron-linux
|
||||
[3]: https://nextcloud.com/
|
||||
|
@ -1,162 +0,0 @@
|
||||
HankChow translating
|
||||
|
||||
How to Replace one Linux Distro With Another in Dual Boot [Guide]
|
||||
======
|
||||
**If you have a Linux distribution installed, you can replace it with another distribution in the dual boot. You can also keep your personal documents while switching the distribution.**
|
||||
|
||||
![How to Replace One Linux Distribution With Another From Dual Boot][1]
|
||||
|
||||
Suppose you managed to [successfully dual boot Ubuntu and Windows][2]. But after reading the [Linux Mint versus Ubuntu discussion][3], you realized that [Linux Mint][4] is more suited for your needs. What would you do now? How would you [remove Ubuntu][5] and [install Mint in dual boot][6]?
|
||||
|
||||
You might think that you need to uninstall [Ubuntu][7] from dual boot first and then repeat the dual booting steps with Linux Mint. Let me tell you something. You don’t need to do all of that.
|
||||
|
||||
If you already have a Linux distribution installed in dual boot, you can easily replace it with another. You don’t have to uninstall the existing Linux distribution. You simply delete its partition and install the new distribution on the disk space vacated by the previous distribution.
|
||||
|
||||
Another good news is that you may be able to keep your Home directory with all your documents and pictures while switching the Linux distributions.
|
||||
|
||||
Let me show you how to switch Linux distributions.
|
||||
|
||||
### Replace one Linux with another from dual boot
|
||||
|
||||
<https://youtu.be/ptF2RUehbKs>
|
||||
|
||||
Let me describe the scenario I am going to use here. I have Linux Mint 19 installed on my system in dual boot mode with Windows 10. I am going to replace it with elementary OS 5. I’ll also keep my personal files (music, pictures, videos, documents from my home directory) while switching distributions.
|
||||
|
||||
Let’s first take a look at the requirements:
|
||||
|
||||
* A system with Linux and Windows dual boot
|
||||
* Live USB of Linux you want to install
|
||||
* Backup of your important files in Windows and in Linux on an external disk (optional yet recommended)
|
||||
|
||||
|
||||
|
||||
#### Things to keep in mind for keeping your home directory while changing Linux distribution
|
||||
|
||||
If you want to keep your files from existing Linux install as it is, you must have a separate root and home directory. You might have noticed that in my [dual boot tutorials][8], I always go for ‘Something Else’ option and then manually create root and home partitions instead of choosing ‘Install alongside Windows’ option. This is where all the troubles in manually creating separate home partition pay off.
|
||||
|
||||
Keeping Home on a separate partition is helpful in situations when you want to replace your existing Linux install with another without losing your files.
|
||||
|
||||
Note: You must remember the exact username and password of your existing Linux install in order to use the same home directory as it is in the new distribution.
|
||||
|
||||
If you don’t have a separate Home partition, you may create it later as well BUT I won’t recommend that. That process is slightly complicated and I don’t want you to mess up your system.
|
||||
|
||||
With that much background information, it’s time to see how to replace a Linux distribution with another.
|
||||
|
||||
#### Step 1: Create a live USB of the new Linux distribution
|
||||
|
||||
Alright! I already mentioned it in the requirements but I still included it in the main steps to avoid confusion.
|
||||
|
||||
You can create a live USB using a start up disk creator like [Etcher][9] in Windows or Linux. The process is simple so I am not going to list the steps here.
|
||||
|
||||
#### Step 2: Boot into live USB and proceed to installing Linux
|
||||
|
||||
Since you have already dual booted before, you probably know the drill. Plugin the live USB, restart your system and at the boot time, press F10 or F12 repeatedly to enter BIOS settings.
|
||||
|
||||
In here, choose to boot from the USB. And then you’ll see the option to try the live environment or installing it immediately.
|
||||
|
||||
You should start the installation procedure. When you reach the ‘Installation type’ screen, choose the ‘Something else’ option.
|
||||
|
||||
![Replacing one Linux with another from dual boot][10]
|
||||
Select ‘Something else’ here
|
||||
|
||||
#### Step 3: Prepare the partition
|
||||
|
||||
You’ll see the partitioning screen now. Look closely and you’ll see your Linux installation with Ext4 file system type.
|
||||
|
||||
![Identifying Linux partition in dual boot][11]
|
||||
Identify where your Linux is installed
|
||||
|
||||
In the above picture, the Ext4 partition labeled as Linux Mint 19 is the root partition. The second Ext4 partition of 82691 MB is the Home partition. I [haven’t used any swap space][12] here.
|
||||
|
||||
Now, if you have just one Ext4 partition, that means that your home directory is on the same partition as root. In this case, you won’t be able to keep your Home directory. I suggest that you copy the important files to an external disk else you’ll lose them forever.
|
||||
|
||||
It’s time to delete the root partition. Select the root partition and click the – sign. This will create some free space.
|
||||
|
||||
![Delete root partition of your existing Linux install][13]
|
||||
Delete root partition
|
||||
|
||||
When you have the free space, click on + sign.
|
||||
|
||||
![Create root partition for the new Linux][14]
|
||||
Create a new root partition
|
||||
|
||||
Now you should create a new partition out of this free space. If you had just one root partition in your previous Linux install, you should create root and home partitions here. You can also create the swap partition if you want to.
|
||||
|
||||
If you had root and home partition separately, just create a root partition from the deleted root partition.
|
||||
|
||||
![Create root partition for the new Linux][15]
|
||||
Creating root partition
|
||||
|
||||
You may ask why did I use delete and add instead of using the ‘change’ option. It’s because a few years ago, using change didn’t work for me. So I prefer to do a – and +. Is it superstition? Maybe.
|
||||
|
||||
One important thing to do here is to mark the newly created partition for format. f you don’t change the size of the partition, it won’t be formatted unless you explicitly ask it to format. And if the partition is not formatted, you’ll have issues.
|
||||
|
||||
![][16]
|
||||
It’s important to format the root partition
|
||||
|
||||
Now if you already had a separate Home partition on your existing Linux install, you should select it and click on change.
|
||||
|
||||
![Recreate home partition][17]
|
||||
Retouch the already existing home partition (if any)
|
||||
|
||||
You just have to specify that you are mounting it as home partition.
|
||||
|
||||
![Specify the home mount point][18]
|
||||
Specify the home mount point
|
||||
|
||||
If you had a swap partition, you can repeat the same steps as the home partition. This time specify that you want to use the space as swap.
|
||||
|
||||
At this stage, you should have a root partition (with format option selected) and a home partition (and a swap if you want to). Hit the install now button to start the installation.
|
||||
|
||||
![Verify partitions while replacing one Linux with another][19]
|
||||
Verify the partitions
|
||||
|
||||
The next few screens would be familiar to you. What matters is the screen where you are asked to create user and password.
|
||||
|
||||
If you had a separate home partition previously and you want to use the same home directory, you MUST use the same username and password that you had before. Computer name doesn’t matter.
|
||||
|
||||
![To keep the home partition intact, use the previous user and password][20]
|
||||
To keep the home partition intact, use the previous user and password
|
||||
|
||||
Your struggle is almost over. You don’t have to do anything else other than waiting for the installation to finish.
|
||||
|
||||
![Wait for installation to finish][21]
|
||||
Wait for installation to finish
|
||||
|
||||
Once the installation is over, restart your system. You’ll have a new Linux distribution or version.
|
||||
|
||||
In my case, I had the entire home directory of Linux Mint 19 as it is in the elementary OS. All the videos, pictures I had remained as it is. Isn’t that nice?
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://itsfoss.com/replace-linux-from-dual-boot/
|
||||
|
||||
作者:[Abhishek Prakash][a]
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://itsfoss.com/author/abhishek/
|
||||
[1]: https://4bds6hergc-flywheel.netdna-ssl.com/wp-content/uploads/2018/09/Replace-Linux-Distro-from-dual-boot.png
|
||||
[2]: https://itsfoss.com/install-ubuntu-1404-dual-boot-mode-windows-8-81-uefi/
|
||||
[3]: https://itsfoss.com/linux-mint-vs-ubuntu/
|
||||
[4]: https://www.linuxmint.com/
|
||||
[5]: https://itsfoss.com/uninstall-ubuntu-linux-windows-dual-boot/
|
||||
[6]: https://itsfoss.com/guide-install-linux-mint-16-dual-boot-windows/
|
||||
[7]: https://www.ubuntu.com/
|
||||
[8]: https://itsfoss.com/guide-install-elementary-os-luna/
|
||||
[9]: https://etcher.io/
|
||||
[10]: https://4bds6hergc-flywheel.netdna-ssl.com/wp-content/uploads/2018/09/replace-linux-with-another-1.jpg
|
||||
[11]: https://4bds6hergc-flywheel.netdna-ssl.com/wp-content/uploads/2018/09/replace-linux-with-another-2.jpg
|
||||
[12]: https://itsfoss.com/swap-size/
|
||||
[13]: https://4bds6hergc-flywheel.netdna-ssl.com/wp-content/uploads/2018/09/replace-linux-with-another-3.jpg
|
||||
[14]: https://4bds6hergc-flywheel.netdna-ssl.com/wp-content/uploads/2018/09/replace-linux-with-another-4.jpg
|
||||
[15]: https://4bds6hergc-flywheel.netdna-ssl.com/wp-content/uploads/2018/09/replace-linux-with-another-5.jpg
|
||||
[16]: https://4bds6hergc-flywheel.netdna-ssl.com/wp-content/uploads/2018/09/replace-linux-with-another-6.jpg
|
||||
[17]: https://4bds6hergc-flywheel.netdna-ssl.com/wp-content/uploads/2018/09/replace-linux-with-another-7.jpg
|
||||
[18]: https://4bds6hergc-flywheel.netdna-ssl.com/wp-content/uploads/2018/09/replace-linux-with-another-8.jpg
|
||||
[19]: https://4bds6hergc-flywheel.netdna-ssl.com/wp-content/uploads/2018/09/replace-linux-with-another-9.jpg
|
||||
[20]: https://4bds6hergc-flywheel.netdna-ssl.com/wp-content/uploads/2018/09/replace-linux-with-another-10.jpg
|
||||
[21]: https://4bds6hergc-flywheel.netdna-ssl.com/wp-content/uploads/2018/09/replace-linux-with-another-11.jpg
|
@ -1,101 +0,0 @@
|
||||
Python at the pump: A script for filling your gas tank
|
||||
======
|
||||
Here's how I used Python to discover a strategy for cost-effective fill-ups.
|
||||
|
||||
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/bulb-light-energy-power-idea.png?itok=zTEEmTZB)
|
||||
|
||||
I recently began driving a car that had traditionally used premium gas (93 octane). According to the maker, though, it requires only 91 octane. The thing is, in the US, you can buy only 87, 89, or 93 octane. Where I live, gas prices jump 30 cents per gallon jump from one grade to the next, so premium costs 60 cents more than regular. So why not try to save some money?
|
||||
|
||||
It’s easy enough to wait until the gas gauge shows that the tank is half full and then fill it with 89 octane, and there you have 91 octane. But it gets tricky to know what to do next—half a tank of 91 octane plus half a tank of 93 ends up being 92, and where do you go from there? You can make continuing calculations, but they get increasingly messy. This is where Python came into the picture.
|
||||
|
||||
I wanted to come up with a simple scheme in which I could fill the tank at some level with 93 octane, then at the same or some other level with 89 octane, with the primary goal to never get below 91 octane with the final mixture. What I needed to do was create some recurring calculation that uses the previous octane value for the preceding fill-up. I suppose there would be some polynomial equation that would solve this, but in Python, this sounds like a loop.
|
||||
|
||||
```
|
||||
#!/usr/bin/env python
|
||||
# octane.py
|
||||
|
||||
o = 93.0
|
||||
newgas = 93.0 # this represents the octane of the last fillup
|
||||
i = 1
|
||||
while i < 21: # 20 iterations (trips to the pump)
|
||||
if newgas == 89.0: # if the last fillup was with 89 octane
|
||||
# switch to 93
|
||||
newgas = 93.0
|
||||
o = newgas/2 + o/2 # fill when gauge is 1/2 full
|
||||
else: # if it wasn't 89 octane, switch to that
|
||||
newgas = 89.0
|
||||
o = newgas/2 + o/2 # fill when gauge says 1/2 full
|
||||
print str(i) + ': '+ str(o)
|
||||
i += 1
|
||||
```
|
||||
|
||||
As you can see, I am initializing the variable o (the current octane mixture in the tank) and the variable newgas (what I last filled the tank with) at the same value of 93. The loop then will repeat 20 times, for 20 fill-ups, switching from 89 octane and 93 octane for every other trip to the station.
|
||||
|
||||
```
|
||||
1: 91.0
|
||||
2: 92.0
|
||||
3: 90.5
|
||||
4: 91.75
|
||||
5: 90.375
|
||||
6: 91.6875
|
||||
7: 90.34375
|
||||
8: 91.671875
|
||||
9: 90.3359375
|
||||
10: 91.66796875
|
||||
11: 90.333984375
|
||||
12: 91.6669921875
|
||||
13: 90.3334960938
|
||||
14: 91.6667480469
|
||||
15: 90.3333740234
|
||||
16: 91.6666870117
|
||||
17: 90.3333435059
|
||||
18: 91.6666717529
|
||||
19: 90.3333358765
|
||||
20: 91.6666679382
|
||||
```
|
||||
|
||||
This shows is that I probably need only 10 or 15 loops to see stabilization. It also shows that soon enough, I undershoot my 91 octane target. It’s also interesting to see this stabilization of the alternating mixture values, and it turns out this happens with any scheme where you choose the same amounts each time. In fact, it is true even if the amount of the fill-up is different for 89 and 93 octane.
|
||||
|
||||
So at this point, I began playing with fractions, reasoning that I would probably need a bigger 93 octane fill-up than the 89 fill-up. I also didn’t want to make frequent trips to the gas station. What I ended up with (which seemed pretty good to me) was to wait until the tank was about 7⁄12 full and fill it with 89 octane, then wait until it was ¼ full and fill it with 93 octane.
|
||||
|
||||
Here is what the changes in the loop look like:
|
||||
|
||||
```
|
||||
if newgas == 89.0:
|
||||
|
||||
newgas = 93.0
|
||||
o = 3*newgas/4 + o/4
|
||||
else:
|
||||
newgas = 89.0
|
||||
o = 5*newgas/12 + 7*o/12
|
||||
```
|
||||
|
||||
Here are the numbers, starting with the tenth fill-up:
|
||||
|
||||
```
|
||||
10: 92.5122272978
|
||||
11: 91.0487992571
|
||||
12: 92.5121998143
|
||||
13: 91.048783225
|
||||
14: 92.5121958062
|
||||
15: 91.048780887
|
||||
```
|
||||
|
||||
As you can see, this keeps the final octane very slightly above 91 all the time. Of course, my gas gauge isn’t marked in twelfths, but 7⁄12 is slightly less than 5⁄8, and I can handle that.
|
||||
|
||||
An alternative simple solution might have been run the tank to empty and fill with 93 octane, then next time only half-fill it for 89—and perhaps this will be my default plan. Personally, I’m not a fan of running the tank all the way down since this isn’t always convenient. On the other hand, it could easily work on a long trip. And sometimes I buy gas because of a sudden drop in prices. So in the end, this scheme is one of a series of options that I can consider.
|
||||
|
||||
The most important thing for Python users: Don’t code while driving!
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/18/10/python-gas-pump
|
||||
|
||||
作者:[Greg Pittman][a]
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/greg-p
|
@ -1,185 +0,0 @@
|
||||
Translating by StdioA
|
||||
|
||||
Design faster web pages, part 1: Image compression
|
||||
======
|
||||
|
||||
![](https://fedoramagazine.org/wp-content/uploads/2018/02/fasterwebsites1-816x345.jpg)
|
||||
|
||||
Lots of web developers want to achieve fast loading web pages. As more page views come from mobile devices, making websites look better on smaller screens using responsive design is just one side of the coin. Browser Calories can make the difference in loading times, which satisfies not just the user but search engines that rank on loading speed. This article series covers how to slim down your web pages with tools Fedora offers.
|
||||
|
||||
### Preparation
|
||||
|
||||
Before you sart to slim down your web pages, you need to identify the core issues. For this, you can use [Browserdiet][1]. It’s a browser add-on available for Firefox, Opera and Chrome and other browsers. It analyzes the performance values of the actual open web page, so you know where to start slimming down.
|
||||
|
||||
Next you’ll need some pages to work on. The example screenshot shows a test of [getfedora.org][2]. At first it looks very simple and responsive.
|
||||
|
||||
![Browser Diet - values of getfedora.org][3]
|
||||
|
||||
However, BrowserDiet’s page analysis shows there are 1.8MB in files downloaded. Therefore, there’s some work to do!
|
||||
|
||||
### Web optimization
|
||||
|
||||
There are over 281 KB of JavaScript files, 203 KB more in CSS files, and 1.2 MB in images. Start with the biggest issue — the images. The tool set you need for this is GIMP, ImageMagick, and optipng. You can easily install them using the following command:
|
||||
|
||||
```
|
||||
sudo dnf install gimp imagemagick optipng
|
||||
|
||||
```
|
||||
|
||||
For example, take the [following file][4] which is 6.4 KB:
|
||||
|
||||
![][4]
|
||||
|
||||
First, use the file command to get some basic information about this image:
|
||||
|
||||
```
|
||||
$ file cinnamon.png
|
||||
cinnamon.png: PNG image data, 60 x 60, 8-bit/color RGBA, non-interlaced
|
||||
|
||||
```
|
||||
|
||||
The image — which is only in grey and white — is saved in 8-bit/color RGBA mode. That’s not as efficient as it could be.
|
||||
|
||||
Start GIMP so you can set a more appropriate color mode. Open cinnamon.png in GIMP. Then go to Image>Mode and set it to greyscale. Export the image as PNG with compression factor 9. All other settings in the export dialog should be the default.
|
||||
|
||||
```
|
||||
$ file cinnamon.png
|
||||
cinnamon.png: PNG image data, 60 x 60, 8-bit gray+alpha, non-interlaced
|
||||
|
||||
```
|
||||
|
||||
The output shows the file’s now in 8bit gray+alpha mode. The file size has shrunk from 6.4 KB to 2.8 KB. That’s already only 43.75% of the original size. But there’s more you can do!
|
||||
|
||||
You can also use the ImageMagick tool identify to provide more information about the image.
|
||||
|
||||
```
|
||||
$ identify cinnamon2.png
|
||||
cinnamon.png PNG 60x60 60x60+0+0 8-bit Grayscale Gray 2831B 0.000u 0:00.000
|
||||
|
||||
```
|
||||
|
||||
This tells you the file is 2831 bytes. Jump back into GIMP, and export the file. In the export dialog disable the storing of the time stamp and the alpha channel color values to reduce this a little more. Now the file output shows:
|
||||
|
||||
```
|
||||
$ identify cinnamon.png
|
||||
cinnamon.png PNG 60x60 60x60+0+0 8-bit Grayscale Gray 2798B 0.000u 0:00.000
|
||||
|
||||
```
|
||||
|
||||
Next, use optipng to losslessly optimize your PNG images. There are other tools that do similar things, including **advdef** (which is part of advancecomp), **pngquant** and **pngcrush.**
|
||||
|
||||
Run optipng on your file. Note that this will replace your original:
|
||||
|
||||
```
|
||||
$ optipng -o7 cinnamon.png
|
||||
** Processing: cinnamon.png
|
||||
60x60 pixels, 2x8 bits/pixel, grayscale+alpha
|
||||
Reducing image to 8 bits/pixel, grayscale
|
||||
Input IDAT size = 2720 bytes
|
||||
Input file size = 2812 bytes
|
||||
|
||||
Trying:
|
||||
zc = 9 zm = 8 zs = 0 f = 0 IDAT size = 1922
|
||||
zc = 9 zm = 8 zs = 1 f = 0 IDAT size = 1920
|
||||
|
||||
Selecting parameters:
|
||||
zc = 9 zm = 8 zs = 1 f = 0 IDAT size = 1920
|
||||
|
||||
Output IDAT size = 1920 bytes (800 bytes decrease)
|
||||
Output file size = 2012 bytes (800 bytes = 28.45% decrease)
|
||||
|
||||
```
|
||||
|
||||
The option -o7 is the slowest to process, but provides the best end results. You’ve knocked 800 more bytes off the file size, which is now 2012 bytes.
|
||||
|
||||
To resize all of the PNGs in a directory, use this command:
|
||||
|
||||
```
|
||||
$ optipng -o7 -dir=<directory> *.png
|
||||
|
||||
```
|
||||
|
||||
The option -dir lets you give a target directory for the output. If this option is not used, optipng would overwrite the original images.
|
||||
|
||||
### Choosing the right file format
|
||||
|
||||
When it comes to pictures for the usage in the internet, you have the choice between:
|
||||
|
||||
|
||||
+ [JPG or JPEG][9]
|
||||
+ [GIF][10]
|
||||
+ [PNG][11]
|
||||
+ [aPNG][12]
|
||||
+ [JPG-LS][13]
|
||||
+ [JPG 2000 or JP2][14]
|
||||
+ [SVG][15]
|
||||
|
||||
|
||||
JPG-LS and JPG 2000 are not widely used. Only a few digital cameras support these formats, so they can be ignored. aPNG is an animated PNG, and not widely used either.
|
||||
|
||||
You could save a few bytes more through changing the compression rate or choosing another file format. The first option you can’t do in GIMP, as it’s already using the highest compression rate. As there are no [alpha channels][5] in the picture, you can choose JPG as file format instead. For now use the default value of 90% quality — you could change it down to 85%, but then alias effects become visible. This saves a few bytes more:
|
||||
|
||||
```
|
||||
$ identify cinnamon.jpg
|
||||
cinnamon.jpg JPEG 60x60 60x60+0+0 8-bit sRGB 2676B 0.000u 0:00.000
|
||||
|
||||
```
|
||||
|
||||
Alone this conversion to the right color space and choosing JPG as file format brought down the file size from 23 KB to 12.3 KB, a reduction of nearly 50%.
|
||||
|
||||
|
||||
#### PNG vs. JPG: quality and compression rate
|
||||
|
||||
So what about the rest of the images? This method would work for all the other pictures, except the Fedora “flavor” logos and the logos for the four foundations. Those are presented on a white background.
|
||||
|
||||
One of the main differences between PNG and JPG is that JPG has no alpha channel. Therefore it can’t handle transparency. If you rework these images by using a JPG on a white background, you can reduce the file size from 40.7 KB to 28.3 KB.
|
||||
|
||||
Now there are four more images you can rework: the backgrounds. For the grey background, set the mode to greyscale again. With this bigger picture, the savings also is bigger. It shrinks from 216.2 KB to 51.0 KB — it’s now barely 25% of its original size. All in all, you’ve shrunk 481.1 KB down to 191.5 KB — only 39.8% of the starting size.
|
||||
|
||||
#### Quality vs. Quantity
|
||||
|
||||
Another difference between PNG and JPG is the quality. PNG is a lossless compressed raster graphics format. But JPG loses size through compression, and thus affects quality. That doesn’t mean you shouldn’t use JPG, though. But you have to find a balance between file size and quality.
|
||||
|
||||
### Achievement
|
||||
|
||||
This is the end of Part 1. After following the techniques described above, here are the results:
|
||||
|
||||
![][6]
|
||||
|
||||
You brought image size down to 488.9 KB versus 1.2MB at the start. That’s only about a third of the size, just through optimizing with optipng. This page can probably be made to load faster still. On the scale from snail to hypersonic, it’s not reached racing car speed yet!
|
||||
|
||||
Finally you can check the results in [Google Insights][7], for example:
|
||||
|
||||
![][8]
|
||||
|
||||
In the Mobile area the page gathered 10 points on scoring, but is still in the Medium sector. It looks totally different for the Desktop, which has gone from 62/100 to 91/100 and went up to Good. As mentioned before, this test isn’t the be all and end all. Consider scores such as these to help you go in the right direction. Keep in mind you’re optimizing for the user experience, and not for a search engine.
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://fedoramagazine.org/design-faster-web-pages-part-1-image-compression/
|
||||
|
||||
作者:[Sirko Kemter][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://fedoramagazine.org/author/gnokii/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://browserdiet.com/calories/
|
||||
[2]: http://getfedora.org
|
||||
[3]: https://fedoramagazine.org/wp-content/uploads/2018/02/ff-addon-diet.jpg
|
||||
[4]: https://getfedora.org/static/images/cinnamon.png
|
||||
[5]: https://www.webopedia.com/TERM/A/alpha_channel.html
|
||||
[6]: https://fedoramagazine.org/wp-content/uploads/2018/02/ff-addon-diet-i.jpg
|
||||
[7]: https://developers.google.com/speed/pagespeed/insights/?url=getfedora.org&tab=mobile
|
||||
[8]: https://fedoramagazine.org/wp-content/uploads/2018/02/PageSpeed_Insights.png
|
||||
[9]: https://en.wikipedia.org/wiki/JPEG
|
||||
[10]: https://en.wikipedia.org/wiki/GIF
|
||||
[11]: https://en.wikipedia.org/wiki/Portable_Network_Graphics
|
||||
[12]: https://en.wikipedia.org/wiki/APNG
|
||||
[13]: https://en.wikipedia.org/wiki/JPEG_2000
|
||||
[14]: https://en.wikipedia.org/wiki/JPEG_2000
|
||||
[15]: https://en.wikipedia.org/wiki/Scalable_Vector_Graphics
|
@ -0,0 +1,155 @@
|
||||
如何在双系统引导下替换 Linux 发行版
|
||||
======
|
||||
在双系统引导的状态下,你可以将已安装的 Linux 发行版替换为另一个发行版,同时还可以保留原本的个人数据。
|
||||
|
||||
![How to Replace One Linux Distribution With Another From Dual Boot][1]
|
||||
|
||||
假设你的电脑上已经[以双系统的形式安装了 Ubuntu 和 Windows][2],但经过[将 Linux Mint 与 Ubuntu 比较][3]之后,你又觉得 [Linux Mint][4] 会更适合自己的时候,你会怎样做?又该如何在[删除 Ubuntu][5] 的同时[在双系统中安装 Mint][6] 呢?
|
||||
|
||||
你或许觉得应该首先从在双系统中卸载 [Ubuntu][7],然后使用 Linux Mint 重新安装成双系统。但实际上并不需要这么麻烦。
|
||||
|
||||
如果你已经在双系统引导中安装了一种 Linux 发行版,就可以轻松替换成另一个发行版了,而且也不必卸载已有的 Linux 发行版,只需要删除其所在的分区,然后在腾出的磁盘空间上安装另一个 Linux 发行版就可以了。
|
||||
|
||||
与此同时,更换 Linux 发行版后,仍然会保留原本 home 目录中包含所有文件。
|
||||
|
||||
下面就来详细介绍一下。
|
||||
|
||||
### 在双系统引导中替换 Linux 发行版
|
||||
|
||||
<https://youtu.be/ptF2RUehbKs>
|
||||
|
||||
这是我的演示范例。我使用双系统引导同时安装了 Windows 10 和 Linux Mint 19,然后我会把 Linux Mint 19 替换成 Elementary OS 5,同时在替换后保留我的个人文件(包括音乐、图片、视频和 home 目录中的文件)。
|
||||
|
||||
你需要做好以下这些准备:
|
||||
|
||||
* 使用 Linux 和 Windows 双系统引导
|
||||
* 需要安装的 Linux 发行版的 USB live 版
|
||||
* 在外部磁盘备份 Windows 和 Linux 中的重要文件(并非必要,但建议备份一下)
|
||||
|
||||
|
||||
|
||||
#### 在替换 Linux 发行版时要记住保留你的 home 目录
|
||||
|
||||
如果想让个人文件在安装新 Linux 系统的过程中不受影响,原有的 Linux 系统必须具有单独的 root 目录和 home 目录。你可能会发现我的[双系统引导教程][8]在安装过程中不选择“与 Windows 一起安装”选项,而选择“其它”选项,然后手动创建 root 和 home 分区。所以,手动创建单独的 home 分区也算是一个磨刀不误砍柴工的操作。因为如果要在不丢失文件的情况下,将现有的 Linux 发行版替换为另一个发行版,需要将 home 目录存放在一个单独的分区上。
|
||||
|
||||
不过,你必须记住现有 Linux 系统的用户名和密码才能使用与新系统中相同的 home 目录。
|
||||
|
||||
如果你没有单独的 home 分区,也可以后续再进行创建。但这并不是推荐做法,因为这个过程会比较复杂,有可能会把你的系统搞乱。
|
||||
|
||||
下面来看看如何替换到另一个 Linux 发行版。
|
||||
|
||||
#### 步骤 1:为新的 Linux 发行版创建一个 USB live 版
|
||||
|
||||
尽管上文中已经提到了它,但我还是要重复一次以免忽略。
|
||||
|
||||
你可以使用 Windows 或 Linux 中的启动盘创建器(例如 [Etcher][9])来创建 USB live 版,这个过程比较简单,这里不再详细叙述。
|
||||
|
||||
#### 步骤 2:启动 USB live 版并安装 Linux
|
||||
|
||||
你应该已经使用过双系统启动,对这个过程不会陌生。使用 USB live 版重新启动系统,在启动时反复按 F10 或 F12 进入 BIOS 设置。选择从 USB 启动,就可以看到进入 live 环境或立即安装的选项。
|
||||
|
||||
在安装过程中,进入“安装类型”界面时,选择“其它”选项。
|
||||
|
||||
![Replacing one Linux with another from dual boot][10]
|
||||
(在这里选择“其它”选项)
|
||||
|
||||
#### 步骤 3:准备分区操作
|
||||
|
||||
下图是分区界面。你会看到使用 Ext4 文件系统类型来安装 Linux。
|
||||
|
||||
![Identifying Linux partition in dual boot][11]
|
||||
(确定 Linux 的安装位置)
|
||||
|
||||
在上图中,标记为 Linux Mint 19 的 Ext4 分区是 root 分区,大小为 82691 MB 的第二个 Ext4 分区是 home 分区。在这里我这里没有使用[交换空间][12]。
|
||||
|
||||
如果你只有一个 Ext4 分区,就意味着你的 home 目录与 root 目录位于同一分区。在这种情况下,你就无法保留 home 目录中的文件了,这个时候我建议将重要文件复制到外部磁盘,否则这些文件将不会保留。
|
||||
|
||||
然后是删除 root 分区。选择 root 分区,然后点击 - 号,这个操作释放了一些磁盘空间。
|
||||
|
||||
![Delete root partition of your existing Linux install][13]
|
||||
(删除 root 分区)
|
||||
|
||||
磁盘空间释放出来后,点击 + 号。
|
||||
|
||||
![Create root partition for the new Linux][14]
|
||||
(创建新的 root 分区)
|
||||
|
||||
现在已经在可用空间中创建一个新分区。如果你之前的 Linux 系统中只有一个 root 分区,就应该在这里创建 root 分区和 home 分区。如果需要,还可以创建交换分区。
|
||||
|
||||
如果你之前已经有 root 分区和 home 分区,那么只需要从已删除的 root 分区创建 root 分区就可以了。
|
||||
|
||||
![Create root partition for the new Linux][15]
|
||||
(创建 root 分区)
|
||||
|
||||
你可能有疑问,为什么要经过“删除”和“添加”两个过程,而不使用“更改”选项。这是因为以前使用“更改”选项好像没有效果,所以我更喜欢用 - 和 +。这是迷信吗?也许是吧。
|
||||
|
||||
这里有一个重要的步骤,对新创建的 root 分区进行格式化。在没有更改分区大小的情况下,默认是不会对分区进行格式化的。如果分区没有被格式化,之后可能会出现问题。
|
||||
|
||||
![][16]
|
||||
(格式化 root 分区很重要)
|
||||
|
||||
如果你在新的 Linux 系统上已经划分了单独的 home 分区,选中它并点击更改。
|
||||
|
||||
![Recreate home partition][17]
|
||||
(修改已有的 home 分区)
|
||||
|
||||
然后指定将其作为 home 分区挂载即可。
|
||||
|
||||
![Specify the home mount point][18]
|
||||
(指定 home 分区的挂载点)
|
||||
|
||||
如果你还有交换分区,可以重复与 home 分区相同的步骤,唯一不同的是要指定将空间用作交换空间。
|
||||
|
||||
现在的状态应该是有一个 root 分区(将被格式化)和一个 home 分区(如果需要,还可以使用交换分区)。点击“立即安装”可以开始安装。
|
||||
|
||||
![Verify partitions while replacing one Linux with another][19]
|
||||
(检查分区情况)
|
||||
|
||||
接下来的几个界面就很熟悉了,要重点注意的是创建用户和密码的步骤。如果你之前有一个单独的 home 分区,并且还想使用相同的 home 目录,那你必须使用和之前相同的用户名和密码,至于设备名称则可以任意指定。
|
||||
|
||||
![To keep the home partition intact, use the previous user and password][20]
|
||||
(要保持 home 分区不变,请使用之前的用户名和密码)
|
||||
|
||||
接下来只要静待安装完成,不需执行任何操作。
|
||||
|
||||
![Wait for installation to finish][21]
|
||||
(等待安装完成)
|
||||
|
||||
安装完成后重新启动系统,你就能使用新的 Linux 发行版。
|
||||
|
||||
在以上的例子中,我可以在新的 Linux Mint 19 中使用原有的 Elementary OS 中的整个 home 目录,并且其中所有视频和图片都原封不动。岂不美哉?
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://itsfoss.com/replace-linux-from-dual-boot/
|
||||
|
||||
作者:[Abhishek Prakash][a]
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
译者:[HankChow](https://github.com/HankChow)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://itsfoss.com/author/abhishek/
|
||||
[1]: https://4bds6hergc-flywheel.netdna-ssl.com/wp-content/uploads/2018/09/Replace-Linux-Distro-from-dual-boot.png
|
||||
[2]: https://itsfoss.com/install-ubuntu-1404-dual-boot-mode-windows-8-81-uefi/
|
||||
[3]: https://itsfoss.com/linux-mint-vs-ubuntu/
|
||||
[4]: https://www.linuxmint.com/
|
||||
[5]: https://itsfoss.com/uninstall-ubuntu-linux-windows-dual-boot/
|
||||
[6]: https://itsfoss.com/guide-install-linux-mint-16-dual-boot-windows/
|
||||
[7]: https://www.ubuntu.com/
|
||||
[8]: https://itsfoss.com/guide-install-elementary-os-luna/
|
||||
[9]: https://etcher.io/
|
||||
[10]: https://4bds6hergc-flywheel.netdna-ssl.com/wp-content/uploads/2018/09/replace-linux-with-another-1.jpg
|
||||
[11]: https://4bds6hergc-flywheel.netdna-ssl.com/wp-content/uploads/2018/09/replace-linux-with-another-2.jpg
|
||||
[12]: https://itsfoss.com/swap-size/
|
||||
[13]: https://4bds6hergc-flywheel.netdna-ssl.com/wp-content/uploads/2018/09/replace-linux-with-another-3.jpg
|
||||
[14]: https://4bds6hergc-flywheel.netdna-ssl.com/wp-content/uploads/2018/09/replace-linux-with-another-4.jpg
|
||||
[15]: https://4bds6hergc-flywheel.netdna-ssl.com/wp-content/uploads/2018/09/replace-linux-with-another-5.jpg
|
||||
[16]: https://4bds6hergc-flywheel.netdna-ssl.com/wp-content/uploads/2018/09/replace-linux-with-another-6.jpg
|
||||
[17]: https://4bds6hergc-flywheel.netdna-ssl.com/wp-content/uploads/2018/09/replace-linux-with-another-7.jpg
|
||||
[18]: https://4bds6hergc-flywheel.netdna-ssl.com/wp-content/uploads/2018/09/replace-linux-with-another-8.jpg
|
||||
[19]: https://4bds6hergc-flywheel.netdna-ssl.com/wp-content/uploads/2018/09/replace-linux-with-another-9.jpg
|
||||
[20]: https://4bds6hergc-flywheel.netdna-ssl.com/wp-content/uploads/2018/09/replace-linux-with-another-10.jpg
|
||||
[21]: https://4bds6hergc-flywheel.netdna-ssl.com/wp-content/uploads/2018/09/replace-linux-with-another-11.jpg
|
||||
|
@ -0,0 +1,101 @@
|
||||
使用 Python 为你的油箱加油
|
||||
======
|
||||
我来介绍一下我是如何使用 Python 来节省成本的。
|
||||
|
||||
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/bulb-light-energy-power-idea.png?itok=zTEEmTZB)
|
||||
|
||||
我最近在开一辆烧 93 号汽油的车子。根据汽车制造商的说法,它只需要加 91 号汽油就可以了。然而,在美国只能买到 87 号、89 号、93 号汽油。而我家附近的汽油的物价水平是每增加一号,每加仑就要多付 30 美分,因此如果加 93 号汽油,每加仑就要多花 60 美分。为什么不能节省一些钱呢?
|
||||
|
||||
一开始很简单,只需要先加满 93 号汽油,然后在油量表显示油箱半满的时候,用 89 号汽油加满,就得到一整箱 91 号汽油了。但接下来就麻烦了,剩下半箱 91 号汽油加上半箱 93 号汽油,只会变成一箱 92 号汽油,再接下来呢?如果继续算下去,只会越来越混乱。这个时候 Python 就派上用场了。
|
||||
|
||||
我的方案是,可以根据汽油的实时状态,不断向油箱中加入 93 号汽油或者 89 号汽油,而最终目标是使油箱内汽油的号数不低于 91。我需要做的是只是通过一些算法来判断新旧汽油混合之后的号数。使用多项式方程或许也可以解决这个问题,但如果使用 Python,好像只需要进行循环就可以了。
|
||||
|
||||
```
|
||||
#!/usr/bin/env python
|
||||
# octane.py
|
||||
|
||||
o = 93.0
|
||||
newgas = 93.0 # 这个变量记录上一次加入的汽油号数
|
||||
i = 1
|
||||
while i < 21: # 20 次迭代 (加油次数)
|
||||
if newgas == 89.0: # 如果上一次加的是 89 号汽油,改加 93 号汽油
|
||||
newgas = 93.0
|
||||
o = newgas/2 + o/2 # 当油箱半满的时候就加油
|
||||
else: # 如果上一次加的是 93 号汽油,则改加 89 号汽油
|
||||
newgas = 89.0
|
||||
o = newgas/2 + o/2 # 当油箱半满的时候就加油
|
||||
print str(i) + ': '+ str(o)
|
||||
i += 1
|
||||
```
|
||||
|
||||
在代码中,我首先将变量 `o`(油箱中的当前混合汽油号数)和变量 `newgas`(上一次加入的汽油号数)的初始值都设为 93,然后循环 20 次,也就是分别加入 89 号汽油和 93 号汽油一共 20 次,以保持混合汽油号数稳定。
|
||||
|
||||
```
|
||||
1: 91.0
|
||||
2: 92.0
|
||||
3: 90.5
|
||||
4: 91.75
|
||||
5: 90.375
|
||||
6: 91.6875
|
||||
7: 90.34375
|
||||
8: 91.671875
|
||||
9: 90.3359375
|
||||
10: 91.66796875
|
||||
11: 90.333984375
|
||||
12: 91.6669921875
|
||||
13: 90.3334960938
|
||||
14: 91.6667480469
|
||||
15: 90.3333740234
|
||||
16: 91.6666870117
|
||||
17: 90.3333435059
|
||||
18: 91.6666717529
|
||||
19: 90.3333358765
|
||||
20: 91.6666679382
|
||||
```
|
||||
|
||||
从以上数据来看,只需要 10 到 15 次循环,汽油号数就比较稳定了,也相当接近 91 号汽油的目标。这种交替混合直到稳定的现象看起来很有趣,每次交替加入同等量的不同号数汽油,都会趋于稳定。实际上,即使加入的 89 号汽油和 93 号汽油的量不同,也会趋于稳定。
|
||||
|
||||
因此,我尝试了不同的比例,我认为加入的 93 号汽油需要比 89 号汽油更多一点。在尽量少补充新汽油的情况下,我最终计算到的结果是 89 号汽油要在油箱大约 7/12 满的时候加进去,而 93 号汽油则要在油箱 1/4 满的时候才加进去。
|
||||
|
||||
我的循环将会更改成这样:
|
||||
|
||||
```
|
||||
if newgas == 89.0:
|
||||
|
||||
newgas = 93.0
|
||||
o = 3*newgas/4 + o/4
|
||||
else:
|
||||
newgas = 89.0
|
||||
o = 5*newgas/12 + 7*o/12
|
||||
```
|
||||
|
||||
以下是从第十次加油开始的混合汽油号数:
|
||||
|
||||
```
|
||||
10: 92.5122272978
|
||||
11: 91.0487992571
|
||||
12: 92.5121998143
|
||||
13: 91.048783225
|
||||
14: 92.5121958062
|
||||
15: 91.048780887
|
||||
```
|
||||
|
||||
如你所见,这个调整会令混合汽油号数始终略高于 91。当然,我的油量表并没有 1/12 的刻度,但是 7/12 略小于 5/8,我可以近似地计算。
|
||||
|
||||
一个更简单地方案是每次都首先加满 93 号汽油,然后在油箱半满时加入 89 号汽油直到耗尽,这可能会是我的常规方案。但就我个人而言,这种方法并不太好,有时甚至会产生一些麻烦。但对于长途旅行来说,这种方案会相对简便一些。有时我也会因为油价突然下跌而购买一些汽油,所以,这个方案是我可以考虑的一系列选项之一。
|
||||
|
||||
当然最重要的是:开车不写码,写码不开车!
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/18/10/python-gas-pump
|
||||
|
||||
作者:[Greg Pittman][a]
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
译者:[HankChow](https://github.com/HankChow)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/greg-p
|
||||
|
@ -0,0 +1,183 @@
|
||||
设计更快的网页——第一部分:图片压缩
|
||||
======
|
||||
|
||||
![](https://fedoramagazine.org/wp-content/uploads/2018/02/fasterwebsites1-816x345.jpg)
|
||||
|
||||
很多 Web 开发者都希望做出加载速度很快的网页。在移动设备浏览占比越来越大的背景下,使用响应式设计使得网站在小屏幕下看起来更漂亮只是其中一个方面。Browser Calories 可以展示网页的加载时间——这不单单关系到用户,还会影响到通过加载速度来进行评级的搜索引擎。这个系列的文章介绍了如何使用 Fedora 提供的工具来给网页“瘦身”。
|
||||
|
||||
### 准备工作
|
||||
|
||||
在你开始缩减网页之前,你需要明确核心问题所在。为此,你可以使用 [Browserdiet][1]. 这是一个浏览器插件,适用于 Firefox, Opera, Chrome 和其它浏览器。它会对打开的网页进行性能分析,这样你就可以知道应该从哪里入手来缩减网页。
|
||||
|
||||
然后,你需要一些用来处理的页面。下面的例子是针对 [getferoda.org][2] 的测试截图。一开始,它看起来非常简单,也符合响应式设计。
|
||||
|
||||
![Browser Diet - getfedora.org 的评分][3]
|
||||
|
||||
然而,BroserDiet 的网页分析表明,这个网页需要加载 1.8MB 的文件。所以,我们现在有活干了!
|
||||
|
||||
### Web 优化
|
||||
|
||||
网页中包含 281 KB 的 JavaScript 文件,203 KB 的 CSS 文件,还有 1.2 MB 的图片。我们先从最严重的问题——图片开始入手。为了解决问题,你需要的工具集有 GIMP, ImageMagick 和 optipng. 你可以使用如下命令轻松安装它们:
|
||||
|
||||
```
|
||||
sudo dnf install gimp imagemagick optipng
|
||||
|
||||
```
|
||||
|
||||
比如,我们先拿到这个 6.4 KB 的[文件][4]:
|
||||
|
||||
![][4]
|
||||
|
||||
首先,使用 file 命令来获取这张图片的一些基本信息:
|
||||
|
||||
```
|
||||
$ file cinnamon.png
|
||||
cinnamon.png: PNG image data, 60 x 60, 8-bit/color RGBA, non-interlaced
|
||||
|
||||
```
|
||||
|
||||
这张只由白色和灰色构成的图片使用 8 位 / RGBA 模式来存储。这种方式并没有那么高效。
|
||||
|
||||
使用 GIMP,你可以为这张图片设置一个更合适的颜色模式。在 GIMP 中打开 cinnamon.png. 然后,在“图片 > 模式”菜单中将其设置为“灰度模式”。将这张图片以 PNG 格式导出。导出时使用压缩因子 9,导出对话框中的其它配置均使用默认选项。
|
||||
|
||||
```
|
||||
$ file cinnamon.png
|
||||
cinnamon.png: PNG image data, 60 x 60, 8-bit gray+alpha, non-interlaced
|
||||
|
||||
```
|
||||
|
||||
输出显示,现在这个文件现在处于 8 位 / 灰阶+aplha 模式。文件大小从 6.4 KB 缩小到了 2.8 KB. 这已经是原来大小的 43.75% 了。但是,我们能做的还有很多!
|
||||
|
||||
你可以使用 ImageMagick 工具来查看这张图片的更多信息。
|
||||
|
||||
```
|
||||
$ identify cinnamon2.png
|
||||
cinnamon.png PNG 60x60 60x60+0+0 8-bit Grayscale Gray 2831B 0.000u 0:00.000
|
||||
|
||||
```
|
||||
|
||||
它告诉你,这个文件的大小为 2831 字节。我们回到 GIMP,重新导出文件。在导出对话框中,取消存储时间戳和 alpha 通道色值,来让文件更小一点。现在文件输出显示:
|
||||
|
||||
```
|
||||
$ identify cinnamon.png
|
||||
cinnamon.png PNG 60x60 60x60+0+0 8-bit Grayscale Gray 2798B 0.000u 0:00.000
|
||||
|
||||
```
|
||||
|
||||
下面,用 optipng 来无损优化你的 PNG 图片。具有相似功能的工具有很多,包括 **advdef**(这是 advancecomp 的一部分),**pngquant** 和 **pngcrush**。
|
||||
|
||||
对你的文件运行 optipng. 注意,这个操作会覆盖你的原文件:
|
||||
|
||||
```
|
||||
$ optipng -o7 cinnamon.png
|
||||
** Processing: cinnamon.png
|
||||
60x60 pixels, 2x8 bits/pixel, grayscale+alpha
|
||||
Reducing image to 8 bits/pixel, grayscale
|
||||
Input IDAT size = 2720 bytes
|
||||
Input file size = 2812 bytes
|
||||
|
||||
Trying:
|
||||
zc = 9 zm = 8 zs = 0 f = 0 IDAT size = 1922
|
||||
zc = 9 zm = 8 zs = 1 f = 0 IDAT size = 1920
|
||||
|
||||
Selecting parameters:
|
||||
zc = 9 zm = 8 zs = 1 f = 0 IDAT size = 1920
|
||||
|
||||
Output IDAT size = 1920 bytes (800 bytes decrease)
|
||||
Output file size = 2012 bytes (800 bytes = 28.45% decrease)
|
||||
|
||||
```
|
||||
|
||||
-o7 选项处理起来最慢,但最终效果最好。于是你又将文件缩小了 800 字节,现在它只有 2012 字节了。
|
||||
|
||||
要压缩文件夹下的所有 PNG,可以使用这个命令:
|
||||
|
||||
```
|
||||
$ optipng -o7 -dir=<directory> *.png
|
||||
|
||||
```
|
||||
|
||||
-dir 选项用来指定输出文件夹。如果不加这个选项,optipng 会覆盖原文件。
|
||||
|
||||
### 选择正确的文件格式
|
||||
|
||||
当涉及到在互联网中使用的图片时,你可以选择:
|
||||
|
||||
|
||||
+ [JPG 或 JPEG][9]
|
||||
+ [GIF][10]
|
||||
+ [PNG][11]
|
||||
+ [aPNG][12]
|
||||
+ [JPG-LS][13]
|
||||
+ [JPG 2000 或 JP2][14]
|
||||
+ [SVG][15]
|
||||
|
||||
|
||||
JPG-LS 和 JPG 2000 没有得到广泛使用。只有一部分数码相机支持这些格式,所以我们可以忽略它们。aPNG 是动态的 PNG 格式,也没有广泛使用。
|
||||
|
||||
可以通过更改压缩率或者使用其它文件格式来节省下更多字节。我们无法在 GIMP 中应用第一种方法,因为现在的图片已经使用了最高的压缩率了。因为我们的图片中不再包含 [aplha 通道][5],你可以使用 JPG 类型来替代 PNG. 现在,使用默认值:90% 质量——你可以将它减小至 85%,但这样会导致可见的叠影。这样又省下一些字节:
|
||||
|
||||
```
|
||||
$ identify cinnamon.jpg
|
||||
cinnamon.jpg JPEG 60x60 60x60+0+0 8-bit sRGB 2676B 0.000u 0:00.000
|
||||
|
||||
```
|
||||
|
||||
只将这张图转成正确的色域,并使用 JPG 作为文件格式,就可以将它从 23 KB 缩小到 12.3 KB,减少了近 50%.
|
||||
|
||||
|
||||
#### PNG vs JPG: 质量和压缩率
|
||||
|
||||
那么,剩下的文件我们要怎么办呢?除了 Fedora “风味”图标和四个特性图标之外,此方法适用于所有其他图片。我们能够处理的图片都有一个白色的背景。
|
||||
|
||||
PNG 和 JPG 的一个主要区别在于,JPG 没有 alpha 通道。所以,它没有透明度选项。如果你使用 JPG 并为它添加白色背景,你可以将文件从 40.7 KB 缩小至 28.3 KB.
|
||||
|
||||
现在又有了四个可以处理的图片:背景图。对于灰色背景,你可以再次使用灰阶模式。对更大的图片,我们就可以节省下更多的空间。它从 216.2 KB 缩小到了 51 KB——基本上只有原图的 25% 了。整体下来,你把这些图片从 481.1 KB 缩小到了 191.5 KB——只有一开始的 39.8%.
|
||||
|
||||
#### 质量 vs 大小
|
||||
|
||||
PNG 和 JPG 的另外一个区别在于质量。PNG 是一种无损压缩光栅图形格式。但是 JPG 虽然使用压缩来缩小体积,可是这会影响到质量。不过,这并不意味着你不应该使用 JPG,只是你需要在文件大小和质量中找到一个平衡。
|
||||
|
||||
### 成就
|
||||
|
||||
这就是第一部分的结尾了。在使用上述技术后,得到的结果如下:
|
||||
|
||||
![][6]
|
||||
|
||||
你将一开始 1.2 MB 的图片体积缩小到了 488.9 KB. 只需通过 optipng 进行优化,就可以达到之前体积的三分之一。这可能使得页面更快地加载。不过,要是使用蜗牛到超音速来对比,这个速度还没到达赛车的速度呢!
|
||||
|
||||
最后,你可以在 [Google Insights][7] 中查看结果,例如:
|
||||
|
||||
![][8]
|
||||
|
||||
在移动端部分,这个页面的得分提升了 10 分,但它依然处于“中等”水平。对于桌面端,结果看起来完全不同,从 62/100 分提升至了 91/100 分,等级也达到了“好”的水平。如我们之前所说的,这个测试并不意味着我们的工作就做完了。通过参考这些分数可以让你朝着正确的方向前进。请记住,你正在为用户体验来进行优化,而不是搜索引擎。
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://fedoramagazine.org/design-faster-web-pages-part-1-image-compression/
|
||||
|
||||
作者:[Sirko Kemter][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[StdioA](https://github.com/StdioA)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://fedoramagazine.org/author/gnokii/
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://browserdiet.com/calories/
|
||||
[2]: http://getfedora.org
|
||||
[3]: https://fedoramagazine.org/wp-content/uploads/2018/02/ff-addon-diet.jpg
|
||||
[4]: https://getfedora.org/static/images/cinnamon.png
|
||||
[5]: https://www.webopedia.com/TERM/A/alpha_channel.html
|
||||
[6]: https://fedoramagazine.org/wp-content/uploads/2018/02/ff-addon-diet-i.jpg
|
||||
[7]: https://developers.google.com/speed/pagespeed/insights/?url=getfedora.org&tab=mobile
|
||||
[8]: https://fedoramagazine.org/wp-content/uploads/2018/02/PageSpeed_Insights.png
|
||||
[9]: https://en.wikipedia.org/wiki/JPEG
|
||||
[10]: https://en.wikipedia.org/wiki/GIF
|
||||
[11]: https://en.wikipedia.org/wiki/Portable_Network_Graphics
|
||||
[12]: https://en.wikipedia.org/wiki/APNG
|
||||
[13]: https://en.wikipedia.org/wiki/JPEG_2000
|
||||
[14]: https://en.wikipedia.org/wiki/JPEG_2000
|
||||
[15]: https://en.wikipedia.org/wiki/Scalable_Vector_Graphics
|
Loading…
Reference in New Issue
Block a user