Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ระบบ >> Linux

วิธี Backup ไฟล์ไป Google Drive บน Linux

Google Drive เป็น medium ยอดเยี่ยมสำหรับคนที่ต้องเก็บ online copies ของไฟล์ Linux users สามารถ leverage ฟรี 15 GB plan สำหรับ backup essential configuration files นอกจากนี้ backup large volume of data รวมถึง media files ได้หากมี active Google Workspace (formerly G-Suite) plan คู่มือนี้จะแสดงวิธี auto backup files ไป Google Drive ใน Linux ใช้ open-source package google-drive-ocamlfuse และ CRON

google-drive-ocamlfuse คืออะไร?

google-drive-ocamlfuse เป็น FUSE filesystem ให้ users mount Google Drive storage บน local machine เขียนด้วย OCaml และ freely available via GitHub คุณสมบัติหลักรวมถึง full read/write access, multiple account support, Unix permissions, และ Team Drive support

วิธีใช้สำหรับ Automatic Backups?

ขั้นแรกคือ mount remote Google Drive ไป local filesystem นี่คือที่ google-drive-ocamlfuse เข้ามาเล่น 역할 จากนั้นใช้ automatic scheduler สำหรับ periodically transferring files ไป drive เพื่อ keep things simple ใช้ Linux CRON utility ดูแล scheduling process (สามารถใช้ Zeit schedule cron jobs ได้ด้วย)

ติดตั้งและตั้งค่า google-drive-ocamlfuse

แรก ติดตั้ง google-drive-ocamlfuse บน Linux machine โชคดีที่ straightforward มาก เปิด terminal ใส่คำสั่งต่อไปนี้ที่ prompt

sudo add-apt-repository ppa:alessandro-strada/ppa

จะเพิ่ม required package ไป package manager's repository list ไปข้างหน้าและติดตั้ง package ด้วยคำสั่งต่อไปนี้

# Debian/Ubuntu
sudo apt update && sudo apt install google-drive-ocamlfuse
# Fedora
sudo dnf copr enable sergiomb/google-drive-ocamlfuse
sudo dnf install google-drive-ocamlfuse
# Arch
yay -S google-drive-ocamlfuse

รอจนการติดตั้งเสร็จ เมื่อเสร็จ ต้อง configure สำหรับ mounting ขั้นแรกคือสร้าง mount point สำหรับ Google Drive ใน local filesystem ทำได้โดยพิมพ์คำสั่งด้านล่างใน terminal

mkdir -p ~/mount/google-drive

ต่อไป mount drive storage ด้วยคำสั่ง:

google-drive-ocamlfuse ~/mount/google-drive

google-drive-ocamlfuse จะเปิด default browser และ redirect ไป account authentication page สำหรับ Google Drive เลือก account ที่ต้องการ mount locally

ใส่ password สำหรับ selected account เพื่อ authenticate yourself

ต่อไป Google จะขอ permission ให้ access ให้ google-drive-ocamlfuse คลิก "Allow" และดำเนินต่อไป

จะถูกถามให้ allow access to your user account อีกครั้ง

เมื่อ allow permission แล้ว จะ redirect ไป webpage ของ google-drive-ocamlfuse ยินดีด้วย คุณ mount Google Drive account ไป local filesystem สำเร็จแล้ว

ตอนนี้เป็นเวลาที่ดีที่จะ verify ว่า mounting process สำหรับ Google Drive ไปอย่างราบรื่นหรือไม่ หาก stage นี้ให้ error ไม่สามารถใช้ backup ไฟล์ไป Google Drive ได้ ใช้คำสั่งด้านล่าง verify

ls -l ~/mount/google-drive/

Output ควรแสดง list of files และ directories ใน Google Drive storage หากทุกอย่างไปตามคาด เราสามารถดำเนินต่อไป configure backup scheduler

Backup ไฟล์ไป Google Drive ด้วย CRON

Backup ไม่ใช่อะไรนอกจาก copy operation เนื่องจาก Google Drive mounted แล้ว เราสามารถ copy files ไปมันได้ as part of local filesystem Verify ได้โดยรันคำสั่ง:

touch test-file
cp test-file ~/mount/google-drive/

ควร copy test file ไป drive storage ใช้คำสั่ง ls verify ว่าคัดลอกสำเร็จไป Google Drive

ls ~/mount/google-drive/

ตอนทุกอย่างพร้อม configure cron สำหรับ automate backup process หากไม่รู้ cron คือ scheduler ให้ users รัน certain commands periodically เราสามารถระบุ commands ไหนที่จะรัน และเมื่อจะรัน โดยใช้ crontab เรียนรู้เพิ่มเติมได้ที่นี่

เราใช้ crontab entry ต่อไปนี้ copy contents ของ "/tmp" folder ไป Google Drive ที่ 00:00 ทุกวัน

0 0 * * * cp /tmp/ ~/mount/google-drive/

หากต้องการ backup files once every week ใช้ crontab entry ต่อไปนี้

0 0 * * 0 cp /tmp/ ~/mount/google-drive/

จะ copy contents ของ "/tmp" ไป drive storage ที่ 00:00 ของทุก Sunday แทนที่ "/tmp" ด้วย directory ที่มี personal files ของคุณ แต่ยังมีขั้นตอนสุดท้ายหนึ่ง ไม่มี crontab entries ข้างต้น mount Google Drive automatically หนึ่ง quick solution คือ mount drive storage ด้วย crontab entry อีกตัว แล้วตามด้วย entry สำหรับ backup

55 23 * * 0 google-drive-ocamlfuse ~/mount/google-drive
0 0 * * 0 cp /tmp/ ~/mount/google-drive/

เห็นได้ว่าเรา mount drive 5 นาทีก่อน ให้ cron เวลาพอทำให้ไม่ miss backup process เนื่องจาก initialization issues ใช้คำสั่งด้านล่าง edit crontab และเพิ่มบรรทัดข้างต้น

สรุป

เราเสนอ simple way backup files ไป Google Drive ใช้ google-drive-ocamlfuse และ cron หรือหากใช้ Gnome สามารถ connect Google กับ desktop และ mount Google Drive บนระบบได้ง่ายๆ ดู Cron alternatives อื่นๆ หรือใช้ systemd timers as a cron replacement