The BackUp

壓縮備份主要文件


cd/home/

tar czf pi_home.tar.gz pi

This creates a tar archive calledpi_home.tar.gzin/home/. You should copy this file to a USB stick or transfer it to another machine on your network.

MySQL

If you have MySQL databases running on your Raspberry Pi, it would be wise to keep them backed up too. To back up a single database, use themysqldumpcommand:

mysqldump recipes>recipes.sql

This command will back up therecipesdatabase to the filerecipes.sql. Note that in this case no username and password have been supplied to themysqldumpcommand. If you do not have your MySQL credentials in a.my.cnfconfiguration file in your home folder, then supply the username and password with flags:

mysqldump-uroot-ppass recipes>recipes.sql

To restore a MySQL database from a dumpfile, pipe the dumpfile into themysqlcommand; provide credentials if necessary and the database name. Note that the database must exist, so create it first:

mysql-Bse"create database recipes"cat recipes.sql|mysql recipes

Alternatively, you can use thepvcommand (not installed by default, so install withsudo apt-get install pv) to see a progress meter as the dumpfile is processed by MySQL. This is useful for large files:

pv recipes.sql|mysql recipes

SD card image

It may be sensible for you to keep a copy of the entire SD card

image, so you can restore the whole SD card if you lose it or it becomes

corrupt. You can do this using the same method you'd use to write an

image to a new card, but in reverse.

In Linux or Mac, for example:

sudo dd bs=4Mif=/dev/sdb of=raspbian.img

This will create an image file on your PC which you can use to write to another SD card, and keep exactly the same contents and settings. To restore or clone to another card, useddin reverse:

sudo dd bs=4Mif=raspbian.img of=/dev/sdb

These files can be very large, and compress well. To compress, you can pipe the output ofddtogzipas well to get a compressed file that is significantly smaller than the original size:

sudo dd bs=4Mif=/dev/sdb|gzip>rasbian.img.gz

To restore, pipe the output ofgunziptodd:

gunzip--stdout rasbian.img.gz|sudo dd bs=4M of=/dev/sdb

See more aboutinstalling SD card images.

Automation

You could write a Bash script to perform each of these processes automatically, and even have it performed periodically usingcron.





如果您的sd卡不是標準的? 你將發(fā)現(xiàn)dd備份會出現(xiàn)錯誤導(dǎo)致無法啟動,或者說您備份16GB 的sd卡 恢復(fù)到8Gb的SD卡將會無法完全寫入,還會破壞磁盤結(jié)構(gòu)


所以我建議您使用最簡單的方式tar


1. 備份系統(tǒng)


首先成為root用戶:

$ sudo su

然后進入文件系統(tǒng)的根目錄(當然,如果你不想備份整個文件系統(tǒng),你也可以進入你想要備份的目錄,包括遠程目錄或者移動硬盤上的目錄):

# cd /

下面是我用來備份系統(tǒng)的完整命令:

# tar cvpzf backup.tgz –exclude=/proc –exclude=/lost+found–exclude=/backup.tgz –exclude=/mnt –exclude=/sys /

讓我們來簡單看一下這個命令:

“tar”當然就是我們備份系統(tǒng)所使用的程序了。

“cvpfz”是tar的選項,意思是“創(chuàng)建檔案文件”、“保持權(quán)限”(保留所有東西原來的權(quán)限)、“使用gzip來減小文件尺寸”。

“backup.gz”是我們將要得到的檔案文件的文件名。

“/”是我們要備份的目錄,在這里是整個文件系統(tǒng)。

在檔案文件名“backup.gz”和要備份的目錄名“/”之間給出了備份時必須排除在外的目錄。有些目錄是無用的,例如“/proc”、“/lost+found”、“/sys”。當然,“backup.gz”這個檔案文件本身必須排除在外,否則你可能會得到一些超出常理的結(jié)果。如果不把“/mnt”排除在外,那么掛載在“/mnt”上的其它分區(qū)也會被備份。另外需要確認一下“/media”上沒有掛載任何東西(例如光盤、移動硬盤),如果有掛載東西,必須把“/media”也排除在外。

有人可能會建議你把“/dev”目錄排除在外,但是我認為這樣做很不妥,具體原因這里就不討論了。

執(zhí)行備份命令之前請再確認一下你所鍵入的命令是不是你想要的。執(zhí)行備份命令可能需要一段不短的時間。

備份完成后,在文件系統(tǒng)的根目錄將生成一個名為“backup.tgz”的文件,它的尺寸有可能非常大?,F(xiàn)在你可以把它燒錄到DVD上或者放到你認為安全的地方去。

在備份命令結(jié)束時你可能會看到這樣一個提示:’tar: Error exit delayed from previouserrors’,多數(shù)情況下你可以忽略它。

你還可以用Bzip2來壓縮文件,Bzip2比gzip的壓縮率高,但是速度慢一些。如果壓縮率對你來說很重要,那么你應(yīng)該使用Bzip2,用“j”代替命令中的“z”,并且給檔案文件一個正確的擴展名“bz2”。完整的命令如下:

# tar cvpjf backup.tar.bz2 –exclude=/proc –exclude=/lost+found–exclude=/backup.tar.bz2 –exclude=/mnt –exclude=/sys /

2. 恢復(fù)系統(tǒng)

在進行恢復(fù)系統(tǒng)的操作時一定要小心!如果你不清楚自己在做什么,那么你有可能把重要的數(shù)據(jù)弄丟,請務(wù)必小心!

接著上面的例子。切換到root用戶,并把文件“backup.tgz”拷貝到分區(qū)的根目錄下。

在Linux中有一件很美妙的事情,就是你可以在一個運行的系統(tǒng)中恢復(fù)系統(tǒng),而不需要用boot-cd來專門引導(dǎo)。當然,如果你的系統(tǒng)已經(jīng)掛掉不能啟動了,你可以用LiveCD來啟動,效果是一樣的。你還可以用一個命令把Linux系統(tǒng)中的所有文件干掉,當然在這里我不打算給出這個命令!

使用下面的命令來恢復(fù)系統(tǒng):

# tar xvpfz backup.tgz -C /

如果你的檔案文件是使用Bzip2壓縮的,應(yīng)該用:

# tar xvpfj backup.tar.bz2 -C /

注意:上面的命令會用檔案文件中的文件覆蓋分區(qū)上的所有文件。

執(zhí)行恢復(fù)命令之前請再確認一下你所鍵入的命令是不是你想要的,執(zhí)行恢復(fù)命令可能需要一段不短的時間。

恢復(fù)命令結(jié)束時,你的工作還沒完成,別忘了重新創(chuàng)建那些在備份時被排除在外的目錄:

# mkdir proc

# mkdir lost+found

# mkdir mnt

# mkdir sys

等等

當你重啟電腦,你會發(fā)現(xiàn)一切東西恢復(fù)到你創(chuàng)建備份時的樣子了!




可能還會設(shè)計到分區(qū)表的備份還原

現(xiàn)在的分區(qū)如下:

Device Boot ? ? ?Start ? ? ? ? End ? ? ?Blocks ? Id ?System

/dev/sda1 ? * ? ? ? ? ? 1 ? ? ? ?2612 ? ?20980858+ ? 7 ?HPFS/NTFS

/dev/sda2 ? ? ? ? ? ?2613 ? ? ? 19458 ? 135309127+ ? 5 ?Extended

/dev/sda5 ? ? ? ? ? ?2613 ? ? ? ?6529 ? ?31463271 ? ?7 ?HPFS/NTFS

/dev/sda6 ? ? ? ? ? ?6530 ? ? ? ?9141 ? ?20980858+ ?83 ?Linux

/dev/sda7 ? ? ? ? ? ?9142 ? ? ? 11753 ? ?20980858+ ?83 ?Linux

/dev/sda8 ? ? ? ? ? 11754 ? ? ? 19048 ? ?58593280 ? 83 ?Linux

/dev/sda9 ? ? ? ? ? 19048 ? ? ? 19458 ? ? 3289088 ? 82 ?Linux swap / Solaris

備份方法:

1、備份分區(qū)表信息

sudo fdisk -l >hda.txt ?#分區(qū)表信息重定向輸出到文件中

2、備份MBR

linux@linux-desktop:~/ex$ sudo dd if=/dev/sda of=mbr bs=512 count=1

#輸入文件/dev/sda,

輸出文件mbr(自己定義),輸入(出)塊大小512字節(jié),復(fù)制一次,由于mbr是512個字節(jié),所以讀取寫到mbr文件中了

1+0 records in

1+0 records out

512 bytes (512 B) copied,4.0728e-05 秒,12.6 MB/秒

linux@linux-desktop:~/ex$ ls ?#查看備份成功

hda.txt ?mbr

主引導(dǎo)記錄512字節(jié),前446是程序代碼,后64字節(jié)包含分區(qū)表信息,最后2字節(jié)標識是MBR

3、恢復(fù)分區(qū)表

dd if=mbr of=/dev/sda bs=1 skip=446 count=66 ?#輸入文件mbr,輸出 /dev/sda ,塊大小1個字節(jié),輸入跳過446字節(jié),恢復(fù)66個字節(jié),看來恢復(fù)的只有66個字節(jié)

如果邏輯分區(qū)都沒有了,則用fdisk 照著hda.txt的信息重分一下就行了。

順便說一下dd命令的用法

以下來自網(wǎng)絡(luò),我沒有測試

dd是linux下的一個非常有用的命令,作用是用指定大小的塊復(fù)制一個文件,并在復(fù)制的同時進行指定的轉(zhuǎn)換。

if=輸入文件(input file), ?如: if=/dev/sda,缺省為標準輸入

of=輸出文件(output file), 如: of=mbr, 缺省為標準輸出

ibs=Bytes,一次讀入Bytes個字節(jié),即一個塊大小為Bytes個字節(jié)

obs=Bytes, 一次寫B(tài)ytes個字節(jié),即一個塊大小為Bytes個字節(jié)

bs=Bytes ,同時設(shè)置讀寫塊的大小為Bytes字節(jié),可代替ibs,obs

cbs=Bytes,一次轉(zhuǎn)換Bytes個字節(jié),即轉(zhuǎn)換緩沖區(qū)大小

skip=blocks, 從輸入文件開頭跳過blocks個塊后再開始復(fù)制

seek=blocks,從輸出文件開頭路過blocks個塊后再開始復(fù)制(通常只有當輸出文件是磁盤或磁帶時才有效)

count=blocks, 僅復(fù)制blocks個塊,塊大小是ibs指定的字節(jié)數(shù)

conv=conversion[,conversion...]

用指定的參數(shù)轉(zhuǎn)換文件。

轉(zhuǎn)換參數(shù):

ascii 轉(zhuǎn)換 EBCDIC 為 ASCII。

ebcdic 轉(zhuǎn)換 ASCII 為 EBCDIC。

ibm 轉(zhuǎn)換 ASCII 為 alternate EBCDIC.

block 把每一行轉(zhuǎn)換為長度為 cbs 的記錄,不足部分用空格填充。

unblock 使每一行的長度都為 cbs ,不足部分用空格填充。

lcase 把大寫字符轉(zhuǎn)換為小寫字符。

ucase 把小寫字符轉(zhuǎn)換為大寫字符。

swab 交換輸入的每對字節(jié)。

noerror 出錯時不停止。

notrunc 不截短輸出文件。

sync 把每個輸入塊填充到ibs個字節(jié),不足部分用空(NUL)字符補齊。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容