├── rc.local ├── LICENSE ├── autoresize.sh ├── 한국어 └── README.md ├── raspberrypi_img_backup.sh └── README.md /rc.local: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | # 3 | # rc.local 4 | # 5 | # This script is executed at the end of each multiuser runlevel. 6 | # Make sure that the script will "exit 0" on success or any other 7 | # value on error. 8 | # 9 | # In order to enable or disable this script just change the execution 10 | # bits. 11 | # 12 | # By default this script does nothing. 13 | 14 | # Print the IP address 15 | _IP=$(hostname -I) || true 16 | if [ "$_IP" ]; then 17 | printf "My IP address is %s\n" "$_IP" 18 | fi 19 | 20 | ##autoresize 21 | if [ -f /home/pi/check ]; then 22 | sudo rm -rf /home/pi/check 23 | sudo rm -rf /home/pi/autoresize.sh 24 | exit 1 25 | fi 26 | 27 | sudo touch /home/pi/check 28 | sudo bash /home/pi/autoresize.sh 29 | sudo mv /etc/rc.local.orig /etc/rc.local 30 | 31 | exit 0 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Yonghoon Jung 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /autoresize.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ## auto resize script 4 | ## reference : https://github.com/RPi-Distro/raspi-config/blob/master/raspi-config 5 | 6 | function errorlog_pi(){ 7 | echo "Error, line : $1" >> /home/pi/resize_errorlog 8 | exit 1 9 | } 10 | 11 | ROOT_PART=$(mount | sed -n 's|^/dev/\(.*\) on / .*|\1|p') 12 | [ "$ROOT_PART" ] || errorlog_pi $LINENO 13 | PART_NUM=${ROOT_PART#mmcblk0p} 14 | [ "$PART_NUM" ] || errorlog_pi $LINENO 15 | PART_START=$(sudo parted /dev/mmcblk0 -ms unit s p | grep "^${PART_NUM}" | cut -f 2 -d: | sed 's/[^0-9]//g') 16 | [ "$PART_START" ] || errorlog_pi $LINENO 17 | 18 | ( 19 | echo d 20 | echo $PART_NUM 21 | echo n 22 | echo p 23 | echo $PART_NUM 24 | echo $PART_START 25 | echo 26 | echo w 27 | ) | sudo fdisk /dev/mmcblk0 28 | 29 | cat < /etc/init.d/resize2fs_once && 30 | #!/bin/sh 31 | ### BEGIN INIT INFO 32 | # Provides: resize2fs_once 33 | # Required-Start: 34 | # Required-Stop: 35 | # Default-Start: 3 36 | # Default-Stop: 37 | # Short-Description: Resize the root filesystem to fill partition 38 | # Description: 39 | ### END INIT INFO 40 | . /lib/lsb/init-functions 41 | case "\$1" in 42 | start) 43 | log_daemon_msg "Starting resize2fs_once" && 44 | resize2fs /dev/$ROOT_PART && 45 | update-rc.d resize2fs_once remove && 46 | rm /etc/init.d/resize2fs_once && 47 | log_end_msg \$? 48 | ;; 49 | *) 50 | echo "Usage: \$0 start" >&2 51 | exit 3 52 | ;; 53 | esac 54 | EOF 55 | chmod +x /etc/init.d/resize2fs_once && 56 | update-rc.d resize2fs_once defaults && 57 | 58 | ## reboot.. 59 | reboot 60 | -------------------------------------------------------------------------------- /한국어/README.md: -------------------------------------------------------------------------------- 1 | # 라즈베리파이 백업 스크립트 2 | 3 | ## 설명 4 | 5 | * 1 6 | 7 | * 2 8 | 9 | * 3 10 | 11 | 라즈베리파이를 사용하면서 한 번쯤은 img 파일로 백업을 시도해보셨을 겁니다. img 백업을 하실 때 gparted 사용하셨을 텐데, gparted를 사용할 경우에 1번 사진과 같은 에러 이미지가 출력이 되는 것을 보셨을 겁니다. 해당과 같은 경우에는 최소 사이즈를 넘어가서 출력되는 오류인데 300M ~ 500M 용량의 여유 공간을 남겨두고 다시 Resize 할 경우에는 정상적으로 Resizing이 됩니다. 하지만 이러한 여유 공간을 남겨두고 resizing 한 후 이미지 파일을 만들면 read와 write 하는 속도가 여유 공간만큼 느려지게 됩니다. 완벽하게 resizing을 하는 방법이 없을까 고민을 하다가 해당 스크립트를 만들게 되었습니다. 기능은 굉장히 심플합니다. 작성자는 전문 프로그래머가 아니기 때문에 코드들이 뒤죽박죽이지만, 도움이 되길 바랍니다. 12 | 13 | 해당 스크립트를 사용하여 라즈베리파이 백업 이미지 파일을 만들면 2, 3번 사진처럼 여유 공간을 1~0.5%로 남기고 이미지 파일을 만들기 때문에 gparted보다 더 효율적인 이미지 파일을 생성할 수 있습니다. 또한 SD카드 부팅 시 auto resizing 기능도 스크립트에 써놓았으니, 부팅 후 번거롭게 raspi-config를 이용하여 파티션 resizing을 해주실 필요가 없습니다. 14 | 15 | ## 환경 16 | * 운영체제 : 우분투 혹은 데비안 계열 17 | * 요구되는 프로그램들 : 없음... 18 | 19 | 해당 스크립트는 우분투에 기본 환경에서 작성되었기 때문에 우분투에서 실행하시는 것을 권장합니다. 혹은 스크립트를 보면서 필요한 프로그램을 직접 설치하는 방법도 있으니 생각해보시길 바랍니다. 20 | 21 | ## 사용방법 22 | 23 | * 주의사항 ! 스크립트를 다운로드 및 실행하기 전에 검증된 방법으로 미리 백업을 해두시길 바랍니다. ! 24 | 25 | git clone으로 스크립트를 가져옵니다. 26 | 27 | ``` 28 | git clone https://github.com/dydgns2017/raspberrypi_img_backup_script.git 29 | cd raspberrypi_img_backup_script 30 | ``` 31 | 32 | 해당 스크립트를 사용하기전에 flash 메모리가 꽂혀있는지 확인하고 다음과 같이 fdisk 명령어를 통해서 메모리 장치 이름을 확인해주시길 바랍니다. 33 | 34 | ``` 35 | sudo fdisk -l 36 | ``` 37 | 38 | ex ) print 39 | 40 | ``` 41 | Disk /dev/sdb: 14.9 GiB, 15931539456 bytes, 31116288 sectors 42 | Units: sectors of 1 * 512 = 512 bytes 43 | Sector size (logical/physical): 512 bytes / 512 bytes 44 | I/O size (minimum/optimal): 512 bytes / 512 bytes 45 | Disklabel type: dos 46 | Disk identifier: 0x86310bac 47 | 48 | Device Boot Start End Sectors Size Id Type 49 | /dev/sdb1 8192 96663 88472 43.2M c W95 FAT32 (LBA) 50 | /dev/sdb2 98304 4138976 4040673 1.9G 83 Linux 51 | ``` 52 | 53 | 출력된 메모리 장치의 이름을 기억한 뒤 장치명 및 이미지 파일 이름을 인자값으로 지정하여 스크립트를 실행시켜줍니다. 54 | 55 | ``` 56 | sudo bash raspberrypi_img_backup.sh /dev/xxx xxx.img 57 | ``` 58 | 59 | ex ) excute script 60 | 61 | ``` 62 | sudo bash raspberrypi_img_backup.sh /dev/sdb Raspberrypi.img 63 | ``` 64 | 65 | 전체적인 스크립트가 종료되면 다음과 같은 명령어로 디스크가 정상적으로 write 되었는지 확인합니다. 66 | 67 | ``` 68 | sudo fdisk -l 69 | ``` 70 | 71 | ex ) print 72 | 73 | ``` 74 | Device Boot Start End Sectors Size Id Type 75 | /dev/sdb1 8192 96663 88472 43.2M c W95 FAT32 (LBA) 76 | /dev/sdb2 98304 4138976 4040673 1.9G 83 Linux 77 | ``` 78 | 79 | 이미지 백업은 기본적으로 $HOME/backupfiles로 설정되어 있으니 해당 디렉토리로 들어가서 이미지도 백업이 잘되었는지도 확인해봅니다. 80 | 81 | ``` 82 | cd $HOME/backupfiles 83 | ls -alh 84 | 85 | ex ) print 86 | -rw-r--r-- 1 root root 2.0G 8월 21 23:04 homes.img 87 | -rw-r--r-- 1 root root 2.7G 8월 21 12:11 RPibackup.img 88 | -rw-r--r-- 1 root root 2.0G 8월 21 13:59 test.img 89 | ``` 90 | 91 | 마지막으로 이미지 파일을 dd 명령어로 다른 SD카드에다가 write하여 잘되는지 테스트해봅니다. 92 | 93 | ## 작성자 94 | * **정용훈** - *University : Sunkyul Univ* - 95 | * **이승원** - *University : Korea Univ* - 96 | 97 | ## 참고 98 | * https://raspberrypi.stackexchange.com/questions/499/how-can-i-resize-my-root-partition 99 | * https://github.com/RPi-Distro/raspi-config/blob/master/raspi-config 100 | * https://www.instructables.com/id/How-to-BackUp-and-Shrink-Your-Raspberry-Pi-Image/ 101 | * https://github.com/billw2/rpi-clone 102 | -------------------------------------------------------------------------------- /raspberrypi_img_backup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | ## Create RPi Backup img file 3 | ## By. Yonghoon Jung 4 | 5 | ## variable list 6 | # backupDir 7 | # imgFilename 8 | # piDev 9 | # used 10 | # endofsector 11 | # temp 12 | 13 | ## backup .img file Directory config 14 | 15 | echo "configuration..." 16 | 17 | gitdir=${PWD} 18 | 19 | if [ ! -d $HOME"/backupfiles" ] 20 | then 21 | mkdir $HOME/backupfiles 22 | backupDir=$HOME"/backupfiles" 23 | fi 24 | 25 | if [ ! -n "$1" ] 26 | then 27 | echo "Error, input the Raspberry Pi SD card Device information ex) sudo bash *.sh /dev/sdb" && exit 1 28 | fi 29 | 30 | if [ -n "$2" ] 31 | then 32 | imgFilename=$2 ## img file name config 33 | else 34 | ## default 35 | imgFilename=RPibackup.img 36 | fi 37 | 38 | piDev=$1 ## Rasberry pi SD card config 39 | 40 | ##umount## 41 | mountCheck=$(mount -l | grep $piDev | awk {'print $2'}) 42 | 43 | if [ "$mountCheck" = "on" ] 44 | then 45 | sudo sh -c "umount $piDev" || sudo umount $piDev'2' 46 | if [ ! $? == 0 ]; then 47 | echo "Error, unmount point is Wrong! You should be unmount your raspberry pi SD card Device directly!!" 48 | exit 1 49 | fi 50 | fi 51 | 52 | ########## 53 | 54 | cd / 55 | 56 | if [ -d /media ] ## mount location config 57 | then 58 | cd /media 59 | else 60 | sudo mkdir /media 61 | cd /media 62 | fi 63 | 64 | if [ ! -d /media/pi ] 65 | then 66 | sudo mkdir pi 67 | fi 68 | 69 | if [ ! -d /media/pim ] 70 | then 71 | sudo mkdir pim 72 | fi 73 | 74 | sudo mount -t ext4 $piDev'2' /media/pi 75 | cd /media/pi 76 | 77 | ## Filemove pi to pim 78 | echo "File moving...(pi to pim)" 79 | nodeValue=$(sudo df -ik | grep /media/pi | awk {'print $3'}) 80 | used=$(sudo df -k | grep media/pi | awk {'print $3'}) 81 | varValue=$(sudo du -k /media/pi/var | tail -n 1 | awk {'print $1'}) 82 | ((used=$used+$inodeValue+$varValue)) 83 | ((used=${used}+16384)) ## 16mb add.. 84 | ((used=${used}*1024/512))##change to sector 85 | #used=$(df -k | grep media | awk {'print $3'}) 86 | #((used=${used}*1024/512)) ## sector 87 | #((used=${used}+34816)) ## 17MB add.. 88 | #temp=$(sudo du -ks ./var | awk {'print $1'}) 89 | #((temp=${temp}*1024/512/2)) 90 | #((used=${used}+${temp})) 91 | 92 | if [ ! -n $used ] 93 | then 94 | echo "Error, user variable not set..!" && exit 1 95 | fi 96 | 97 | sudo mv ./* ../pim/ && echo "success file move" && cd /media 98 | sudo umount /media/pi 99 | 100 | ##resize Partition 101 | ( 102 | echo d 103 | echo 2 104 | echo n 105 | echo p 106 | echo 2 107 | echo 98304 ## .. Things to fix later 108 | echo +$used 109 | echo w 110 | ) | sudo fdisk $piDev 111 | if [ ! $? == 0 ]; then 112 | echo "Error, fdisk.." 113 | exit 1 114 | fi 115 | sudo e2fsck -f -y -v -C 0 $piDev'2' 116 | ( 117 | echo y 118 | ) | sudo mkfs.ext4 $piDev'2' 119 | 120 | ##Filemove pim to pi 121 | sudo mount -t ext4 $piDev'2' /media/pi 122 | cd /media/pi && sudo rm -rf ./* 123 | echo "File moving...(pim to pi)" 124 | cd /media/pim 125 | sudo mv ./* ../pi/ && echo "success file move" && cd /media 126 | 127 | ## SD card autoresize config 128 | #sudo cp $gitdir'/autoresize.sh' ./pi/home/pi/autoresize.sh 129 | #sudo cp ./pi/etc/rc.local ./pi/etc/rc.local.orig 130 | #sudo cp $gitdir'/rc.local' ./pi/etc/rc.local 131 | sudo umount /media/pi 132 | 133 | ##create backup img file 134 | echo "create backup img file.." 135 | endofsector=$(sudo fdisk -l $piDev | grep $piDev'2' | awk {'print $3'}) 136 | 137 | if [ ! -n $endofsector ] 138 | then 139 | echo "Error, endofsector variable not set..!" && exit 1 140 | fi 141 | 142 | cd $HOME/backupfiles/ 143 | sudo dd if=$piDev of=$HOME/backupfiles/$imgFilename bs=512 count=$endofsector 144 | 145 | if [ $? == 0 ]; then 146 | echo "Success!.." 147 | cd $HOME/backupfiles 148 | ls -al | grep $imgFilename 149 | else 150 | echo "Error, Flash read Faild!" && exit 1 151 | fi 152 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![issue badge](https://img.shields.io/github/issues/dydgns2017/raspberrypi_img_backup_script.svg) 2 | ![license](https://img.shields.io/github/license/dydgns2017/raspberrypi_img_backup_script.svg) 3 | ![version](https://img.shields.io/badge/ubuntu-16.04-orange.svg) 4 | 5 | # raspberrypi_img_backup_script 6 | 7 | ## Explanation 8 | 9 | * 1 10 | * 2 11 | * 3 12 | 13 | I hope you should have tried back up img file while using raspberry pi. 14 | When you use ‘gparted’ to back up img file, you might encounter with error image such as picture no.1. 15 | This error occurs when compacting size is over-limited than the least set size. 16 | By remaining 300MB~500MB of storage space, resizing process will work normally. 17 | However, remaining these storage space leads to speed lag of reading and writing. 18 | Concerning about how to resize perfect made me write this script. 19 | Although the function is quite straightforward and a bit mixed up cause I’m not a professional programmer, I hope to be helpful. 20 | 21 | If you use this script to create a Raspberrypi backup image file, you can create an image file that is more efficient than gparted by creating an image file with 1 to 0.5% free space as shown in pictures 2 and 3. In addition, auto resizing function is also written to the script when booting SD card, so you do not need to resize the partition using raspi-config after booting. 22 | 23 | ## Getting Started 24 | * OS : ubuntu or debian .. (etc) 25 | * Prerequisites Program : nothing.. 26 | 27 | We recommend that you run the script in Ubuntu because it was created in the default environment. Or you can look at the script and think about installing the required programs yourself. 28 | 29 | ## HOW TO USE 30 | 31 | Care point! Before downloading and running this script, make sure you back up in advance with a proven method! 32 | 33 | Import the script into the git clone. 34 | 35 | ``` 36 | git clone https://github.com/dydgns2017/raspberrypi_img_backup_script.git 37 | cd raspberrypi_img_backup_script 38 | ``` 39 | 40 | Before using the script, make sure that flash memory is plugged
41 | in and check the memory device name through the fdisk command as follows: 42 | 43 | ``` 44 | sudo fdisk -l 45 | ``` 46 | 47 | ex ) print 48 | 49 | ``` 50 | Disk /dev/sdb: 14.9 GiB, 15931539456 bytes, 31116288 sectors 51 | Units: sectors of 1 * 512 = 512 bytes 52 | Sector size (logical/physical): 512 bytes / 512 bytes 53 | I/O size (minimum/optimal): 512 bytes / 512 bytes 54 | Disklabel type: dos 55 | Disk identifier: 0x86310bac 56 | 57 | Device Boot Start End Sectors Size Id Type 58 | /dev/sdb1 8192 96663 88472 43.2M c W95 FAT32 (LBA) 59 | /dev/sdb2 98304 4138976 4040673 1.9G 83 Linux 60 | ``` 61 | 62 | Remember the name of the printed memory device and run the script by
63 | specifying the device name and image file name as factor values. 64 | 65 | ``` 66 | sudo bash raspberrypi_img_backup.sh /dev/xxx xxx.img 67 | ``` 68 | 69 | ex ) excute script 70 | 71 | ``` 72 | sudo bash raspberrypi_img_backup.sh /dev/sdb Raspberrypi.img 73 | ``` 74 | 75 | After the entire script finishes,
verify that the disk is normally written with the command: 76 | 77 | ``` 78 | sudo fdisk -l 79 | ``` 80 | 81 | ex ) print 82 | 83 | ``` 84 | Device Boot Start End Sectors Size Id Type 85 | /dev/sdb1 8192 96663 88472 43.2M c W95 FAT32 (LBA) 86 | /dev/sdb2 98304 4138976 4040673 1.9G 83 Linux 87 | ``` 88 | 89 | Image backups are set to $HOME/backupfiles by default,
so go into the directory to see if the image backup was successful. 90 | 91 | ``` 92 | cd $HOME/backupfiles 93 | ls -alh 94 | 95 | ex ) print 96 | -rw-r--r-- 1 root root 2.0G 8월 21 23:04 homes.img 97 | -rw-r--r-- 1 root root 2.7G 8월 21 12:11 RPibackup.img 98 | -rw-r--r-- 1 root root 2.0G 8월 21 13:59 test.img 99 | ``` 100 | 101 | Finally, use the dd command to write the image file
to another SD card to test its performance. 102 | 103 | ## Authors 104 | * **Yonghoon Jung** - *University : Sunkyul Univ* - 105 | * **Seungwon Lee** - *University : Korea Univ* - 106 | 107 | ## Reference 108 | * https://raspberrypi.stackexchange.com/questions/499/how-can-i-resize-my-root-partition 109 | * https://github.com/RPi-Distro/raspi-config/blob/master/raspi-config 110 | * https://www.instructables.com/id/How-to-BackUp-and-Shrink-Your-Raspberry-Pi-Image/ 111 | * https://github.com/billw2/rpi-clone 112 | --------------------------------------------------------------------------------