How to Backup Directories to Google Drive Automatically on CentOS/Red Hat
This article explains how to backup directories to google drive.
1. Install google drive.
Use the following link ti download google drive
https://drive.google.com/open?id=0B2iJNfzwHersOVYzV0xsa09SZ1U
2. Move this file to "/usr/sbin/" folder.
[root@centos ~]# mv drive /usr/sbin/drive
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiRBdhq3zIhsgjPHwJUS8Kaw2xWHOnJunbmc9xQpRRy0rcsKYODBPx636j2p-yqq2f5ekomfNDRpscPzJL7USwmUBVKpd6tEajF20WtedGhc7ep0StVC401mrwA_FqRLgfOFfxN87Q5oh4/s400/drive.png)
3. Assign right permission to the file.
[root@centos ~]# chmod 755 /usr/sbin/drive
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5kj4T_v9mLGIV43UCf9l2Gcj_aLAD0HIJPC17AY5dvkhF2MKbwBoRwlZaASp4ccfjiRpSbUdseDAT4CNPAx_N6JbBETXPBVeTAW6iFZZYRIVrEEDB4SvqtlH5rDAYxgYIDQ4KgS-CN3I/s400/drive1.png)
4. Now run command "dive". It will ask authentication.
You will get a link like this.
"https://accounts.google.com/o/oauth3/auth?client_id=3453453533-7n0vf5gghru6on6o3fjinrghpdoe88eg.apps.googleusercontent.com&redirectghct_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.goohgheapis.com%2Fauth%3Fdrive&state=state"
Go to the above link on browser, you will get verification code. Type verification code and hit Enter.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiCx71HfEdNwpIfk2RaswfZ22V6gr2xm5yO0h-cH0qdmzx_ONbz_v8Lpwbb5tgfKQ6NzpanlQy0shoOp2TMtdsYgx7m8Xnw5K8mOK5jHhKa6V5kn5ShOqWmALpIRhVCMnVnO2JjcB3oX-U/s640/code.png)
5. Create backup script.
----------------------------------------------------------------------------------
#!/bin/sh
# tar the bkp directory
tar -zcf "dir-backup-$(date '+%Y-%m-%d').tar.gz" /root/AAA/aby/
# encrypt the tar
#openssl aes-256-cbc -a -salt -in "dir-backup-$(date '+%Y-%m-%d').tar.gz" -out "dir-backup-$(date '+%Y-%m-%d').tar.gz.enc" -pass 'pass:123456'
# remove the original tar
#rm -rf "dir-backup-$(date '+%Y-%m-%d').tar.gz"
# upload to google drive
/usr/sbin/drive upload --file "dir-backup-$(date '+%Y-%m-%d').tar.gz" -p 0B2iJNfzwHersN3Yxa0N3U1lqeU0
rm -rf "dir-backup-$(date '+%Y-%m-%d').tar.gz"
----------------------------------------------------------------------------------------------------------------------
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgnF_UZ1NEEcG2PCJONK97ka_D0OjvEr_oWzxabx_uxdXF2y_7rElvegdTnDFWOkk_lRGWZIThe6vuWNfbWmcVVorUTkwNMpEXdjK3ge11FF60DDsTgqLmTULZLyr8R7NtLuTz90Zr7QEw/s640/sh.png)
Note:
"0B2iJNfzwHersN3Yxa0N3U1lqeU0" This is your google drive directory name which can be copied from url.
6. Execute the script.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmh7YCmiRmpUhXOK-NcREisZ92viyYeQiZgf1fWI-UTiTZR5J4dDXoZZ-B511zlcr_WnoiUU16qs_eACfJRSBdcoUS7tFkpp6yTBFV3D1g31Z2kaNX5Y3S-u6VMsf2-zByQOqIqPQHkw8/s400/execute.png)
You can Schedule this script on crontab and schedule this autobackup.
No comments: