Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Automate Backup to Google Drive with Python

#1
Automate Backup to Google Drive with Python

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;994355&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;2&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;4.5&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;4.5\/5 - (2 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;128&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 128px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 4.5/5 – (2 votes) </div>
</p></div>
<h2>Project Description</h2>
<p>Every day I find myself in a position where I have to take daily backup of a certain folder named Reports and store it in my Google drive. This is an essential folder for me since I store all the necessary documents (especially reports). Hence, keeping a backup of this folder in the cloud gives me a cushion in case of any fault in my local system. </p>
<p>However, this involves a long and repetitive manual process wherein I have to sign in to my google account, navigate to the folder where I store my backup files and then copy and paste new files to this folder. This consumes some time, and I was thinking of automating the entire process. That is when I came up with a wonderful automation script that not only saved me all the hassle of manually backing up my files but also ensured that I could do so without the help of any fancy third-party software application. </p>
<p>So, this is what my script does –</p>
<ul>
<li>It connects me to Google Drive automatically.</li>
<li>Uploads a fresh copy of my backup folder every time by replacing the old files.</li>
<li>The entire backup script is triggered every day at 12:00 AM, thereby ensuring that I do not have to run it manually on a daily basis.</li>
</ul>
<p>Thus, in this article, I will guide you through implementing the same automated backup technique to store files from your local system to Google drive on a daily basis using Python. So, without further delay, let’s discover the power of Python automation through our project.</p>
<h2><strong>Step 1: Setting up Google Drive API</strong></h2>
<p>Before you start writing your script, it is essential to ensure that you have set up your Google Drive API properly. Simply put, the Drive API allows you to interact with Google Drive storage and supports several popular programming languages, such as Java, JavaScript, and Python. </p>
<ul>
<li>Go ahead and log in to your <strong><a href="https://accounts.google.com/v3/signin/identifier?dsh=S386870122%3A1671739095030866&amp;continue=https%3A%2F%2Fconsole.cloud.google.com%2F&amp;followup=https%3A%2F%2Fconsole.cloud.google.com%2F&amp;osid=1&amp;passive=1209600&amp;service=cloudconsole&amp;flowName=GlifWebSignIn&amp;flowEntry=ServiceLogin&amp;ifkv=AeAAQh5y03unLoXyhK3MgF2H2Tup6K6bJiFRyYHfrOT9I2z3m8Few5-jV4EDuyV5w9MGPkPz9OPP">GCP console</a></strong> using your gmail ID.</li>
<li>After logging in, create a new Project. Let’s name it FileBackup.</li>
</ul>
<figure class="wp-block-image size-full is-style-default"><img decoding="async" loading="lazy" width="600" height="337" src="https://blog.finxter.com/wp-content/uploads/2022/12/create-project-in-GCP.gif" alt="" class="wp-image-995235" /></figure>
<ul>
<li> Now, you will need to configure and download the client configuration secrets <a href="https://blog.finxter.com/parse-json-data-in-python/" target="_blank" rel="noreferrer noopener">JSON</a> file. So, head over to <strong>APIs and Services</strong> ⮕ Select <strong>OAuth consent screen</strong>.<strong> </strong></li>
</ul>
<figure class="wp-block-image size-large is-style-default"><img decoding="async" loading="lazy" width="1024" height="487" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-279-1024x487.png" alt="" class="wp-image-995244" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-279-1024x487.png 1024w, https://blog.finxter.com/wp-content/uplo...00x143.png 300w, https://blog.finxter.com/wp-content/uplo...68x365.png 768w, https://blog.finxter.com/wp-content/uplo...ge-279.png 1366w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
<ul>
<li>Select User Type as <strong>External</strong> and click <strong>Create</strong>.</li>
</ul>
<figure class="wp-block-image size-full is-style-default"><img decoding="async" loading="lazy" width="633" height="460" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-280.png" alt="" class="wp-image-995250" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-280.png 633w, https://blog.finxter.com/wp-content/uplo...00x218.png 300w" sizes="(max-width: 633px) 100vw, 633px" /></figure>
<ul>
<li>Fill in the necessary <strong>App Information</strong> and click on <strong>Save and Continue</strong>.</li>
</ul>
<figure class="wp-block-image size-full is-style-default"><img decoding="async" loading="lazy" width="600" height="337" src="https://blog.finxter.com/wp-content/uploads/2022/12/App-Information.gif" alt="" class="wp-image-995258" /></figure>
<ul>
<li>No need to fill in <strong>Scopes</strong> for now and proceed to the next step by clicking on <strong>Save and Continue</strong>. </li>
<li>Add a <strong>Test User</strong>. Simply enter your email ID here, or you may choose to add another user. However, to keep things simple, I entered my email ID. </li>
</ul>
<figure class="wp-block-image size-large is-style-default"><img decoding="async" loading="lazy" width="1024" height="487" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-282-1024x487.png" alt="" class="wp-image-995260" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-282-1024x487.png 1024w, https://blog.finxter.com/wp-content/uplo...00x143.png 300w, https://blog.finxter.com/wp-content/uplo...68x365.png 768w, https://blog.finxter.com/wp-content/uplo...ge-282.png 1366w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
<p>You are done with the first step. Now we need the Credentials that will authorise us when we talk to the Google Drive API from our script. </p>
<ul>
<li>Click on <strong>Credentials</strong> <strong>⮕</strong> <strong>CREATE CREDENTIALS</strong> ⮕ Select <strong>OAuth client ID</strong>.</li>
</ul>
<figure class="wp-block-image size-large is-resized is-style-default"><img decoding="async" loading="lazy" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-283-1024x524.png" alt="" class="wp-image-995263" width="700" height="358" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-283-1024x524.png 1024w, https://blog.finxter.com/wp-content/uplo...00x153.png 300w, https://blog.finxter.com/wp-content/uplo...68x393.png 768w, https://blog.finxter.com/wp-content/uplo...ge-283.png 1107w" sizes="(max-width: 700px) 100vw, 700px" /></figure>
<ul>
<li>Fill in the required details and click on Create.</li>
</ul>
<figure class="wp-block-image size-full is-style-default"><img decoding="async" loading="lazy" width="582" height="428" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-284.png" alt="" class="wp-image-995265" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-284.png 582w, https://blog.finxter.com/wp-content/uplo...00x221.png 300w" sizes="(max-width: 582px) 100vw, 582px" /></figure>
<ul>
<li>A screen pops up, and you can see your client ID and secret. Download the JSON and make sure to store it in your Project folder with the name <strong>client_secrets.json</strong>. </li>
</ul>
<figure class="wp-block-image size-full is-style-default"><img decoding="async" loading="lazy" width="877" height="555" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-285.png" alt="" class="wp-image-995268" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-285.png 877w, https://blog.finxter.com/wp-content/uplo...00x190.png 300w, https://blog.finxter.com/wp-content/uplo...68x486.png 768w" sizes="(max-width: 877px) 100vw, 877px" /></figure>
<p>The final step is to enable the Google Drive API so that you can communicate with it using the credentials from your script.</p>
<ul>
<li>Select <strong>Enabled APIs and services </strong>⮕ click <strong>ENABLE APIS AND SERVICES ⮕</strong> Search Google Drive ⮕ Select the Google Drive API ⮕ Click on Enable</li>
</ul>
<figure class="wp-block-image size-full is-style-default"><img decoding="async" loading="lazy" width="600" height="337" src="https://blog.finxter.com/wp-content/uploads/2022/12/Enable-API.gif" alt="" class="wp-image-995281" /></figure>
<p>That’s it! You are now all set to communicate with the Google Drive API using your script to connect and store files in your Google Drive. </p>
<h2><strong>Step 2: Create the Automation Script </strong></h2>
<p>Now that everything is ready, we are now ready to write a program to generate daily backups on the cloud. To contact the API, you will need the help of the <code>PyDrive</code> library. So, go ahead and install it from your terminal as <code>pip3 install PyDrive</code></p>
<p>After installing the <code>PyDrive</code> library, create a new file in your project directory and name it <strong>backup.py</strong>. This is our driver file. </p>
<ul>
<li>First, import the necessary libraries and modules that will help you throughout the course of your script. </li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from pydrive.drive import GoogleDrive
from pydrive.auth import GoogleAuth
import os</pre>
<ul>
<li>Secondly, you need need to set up an authentication with google drive API. As soon as this code is executed, your default browser will open up and ask you for user permissions to access your drive contents.
<ul>
<li><strong>IMPORTANT NOTE: </strong>Ensure that the <strong><code>client_secrets.json</code></strong> file is in the same directory as our Python file. Also, keep the <code>json</code> file a secret and take special care that it is not leaked online.</li>
</ul>
</li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">gauth = GoogleAuth()
gauth.LoadCredentialsFile("mycreds.txt")
if gauth.credentials is None: gauth.LocalWebserverAuth()
elif gauth.access_token_expired: gauth.Refresh()
else: gauth.Authorize()
gauth.SaveCredentialsFile("mycreds.txt")
drive = GoogleDrive(gauth)</pre>
<p>Note that even after you have set up the <code>pydrive</code> and the google API such that my <code>secret_client.json</code> works, it will still require web authentication for G-Drive access every time you run your script. To deal with this issue, you need to make a minor adjustment so that your app doesn’t have to ask the client to authenticate every time you run the app. You just need to use <a href="http://pythonhosted.org/PyDrive/pydrive.html#pydrive.auth.GoogleAuth.LoadCredentialsFile"><code>LoadCredentialsFile</code></a> and <a href="http://pythonhosted.org/PyDrive/pydrive.html#pydrive.auth.GoogleAuth.SaveCredentialsFile"><code>SaveCredentialsFile</code></a> as we did in the code above. This way, you will need to provide access the first time you run your script and never again will Google ask you to authenticate it (unless, off course your access token expires).</p>
<ul>
<li>Next, it is time to access the folder/file you want to backup from the local drive and store it in your Google Drive.</li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def upload_file_to_drive(): for x in os.listdir(path): file_list = drive.ListFile( {'q': "'16jhq7j-SWZmKF_vU0MmKNX' in parents and trashed = False"}).GetList() try: for file1 in file_list: if file1['title'] == os.path.join(path, x): file1.Delete() except: pass</pre>
<p><strong>Explanation: </strong>The key value pair <code>'q': "'16jhq7j-SWZmKF_vU0MmKNX'</code> denotes the folder ID of the folder within your Google drive where you want to save the files. This is how you can get the folder ID of the required folder:</p>
<figure class="wp-block-image size-full is-style-default"><img decoding="async" loading="lazy" width="948" height="325" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-286.png" alt="" class="wp-image-995305" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-286.png 948w, https://blog.finxter.com/wp-content/uplo...00x103.png 300w, https://blog.finxter.com/wp-content/uplo...68x263.png 768w" sizes="(max-width: 948px) 100vw, 948px" /></figure>
<p>Simply copy it and paste it in your script. </p>
<p>A major problem here is whenever you copy the files and store them in your Google drive folder, the pre-existing files also get copied and duplicate files cover up the space unnecessarily. Thus, instead of simply copying the files blindly, you can opt to replace them instead. So, you can follow a two-step process – (i) Delete the pre-existing files (ii) Store a fresh copy of all the files in the folder. That is exactly what has been done in the above script.</p>
<h2><strong>Putting it all Together</strong></h2>
<ul>
<li>All that remains to be done now is to put all the bits and pieces together to compile the entire code. So this is how the complete script looks like –</li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from pydrive.drive import GoogleDrive
from pydrive.auth import GoogleAuth
import os gauth = GoogleAuth()
gauth.LoadCredentialsFile("mycreds.txt")
if gauth.credentials is None: gauth.LocalWebserverAuth()
elif gauth.access_token_expired: gauth.Refresh()
else: gauth.Authorize()
gauth.SaveCredentialsFile("mycreds.txt")
drive = GoogleDrive(gauth)
path = r"C:\reports"
def upload_file_to_drive(): for x in os.listdir(path): file_list = drive.ListFile( {'q': "'16jhq7j-SWZmKF_vUehGalNf6Yr0MmKNX' in parents and trashed = False"}).GetList() try: for file1 in file_list: if file1['title'] == os.path.join(path, x): file1.Delete() except: pass f = drive.CreateFile({'parents': [{'id': '16jhq7j-SWZmKF_vUehGalNf6Yr0MmKNX'}]}) f.SetContentFile(os.path.join(path, x)) f.Upload() f = None upload_file_to_drive()</pre>
<p><strong>Output:</strong></p>
<figure class="wp-block-image size-full is-style-default"><img decoding="async" loading="lazy" width="600" height="337" src="https://blog.finxter.com/wp-content/uploads/2022/12/output.gif" alt="" class="wp-image-995362" /></figure>
<h2>Schedule Regular Backup</h2>
<p>One final task that needs to be taken care of is ensuring that the backup script runs at a particular time every day. There are different ways of doing this. You can use the <code>schedule</code> library in Python to do so within your script itself. However, using <code>schedule</code> has its own downside and it needs the script to keep on running.</p>
<p>Hence, I would suggest using the windows task scheduler. <strong>Windows Task Scheduler</strong> is built-in windows program that facilitates you with the ability to schedule and automate tasks in Windows by running scripts or programs automatically at a given moment.</p>
<ul>
<li>Search for “Task Scheduler”.</li>
</ul>
<figure class="wp-block-image size-full is-style-default"><img decoding="async" loading="lazy" width="333" height="302" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-287.png" alt="" class="wp-image-995373" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-287.png 333w, https://blog.finxter.com/wp-content/uplo...00x272.png 300w" sizes="(max-width: 333px) 100vw, 333px" /></figure>
<ul>
<li>Click Actions ⮕ Create Task</li>
</ul>
<figure class="wp-block-image size-full is-style-default"><img decoding="async" loading="lazy" width="237" height="226" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-288.png" alt="" class="wp-image-995375" /></figure>
<ul>
<li>Give your task a Name</li>
</ul>
<figure class="wp-block-image size-full is-style-default"><img decoding="async" loading="lazy" width="530" height="401" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-289.png" alt="" class="wp-image-995376" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-289.png 530w, https://blog.finxter.com/wp-content/uplo...00x227.png 300w" sizes="(max-width: 530px) 100vw, 530px" /></figure>
<ul>
<li>Then select Actions ⮕ New</li>
</ul>
<figure class="wp-block-image size-full is-style-default"><img decoding="async" loading="lazy" width="530" height="403" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-290.png" alt="" class="wp-image-995378" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-290.png 530w, https://blog.finxter.com/wp-content/uplo...00x228.png 300w" sizes="(max-width: 530px) 100vw, 530px" /></figure>
<ul>
<li>Find the Python Path using <code>where python</code> in the command line and copy the path from the command line.</li>
</ul>
<figure class="wp-block-image size-full is-style-default"><img decoding="async" loading="lazy" width="669" height="122" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-297.png" alt="" class="wp-image-995450" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-297.png 669w, https://blog.finxter.com/wp-content/uplo...300x55.png 300w" sizes="(max-width: 669px) 100vw, 669px" /></figure>
<ul>
<li>Go to the folder where your Python script is actually located. Hold the Shift Key on your keyboard and right-click on the file and select <code>Copy as path</code></li>
</ul>
<figure class="wp-block-image size-full is-style-default"><img decoding="async" loading="lazy" width="839" height="775" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-294.png" alt="" class="wp-image-995397" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-294.png 839w, https://blog.finxter.com/wp-content/uplo...00x277.png 300w, https://blog.finxter.com/wp-content/uplo...68x709.png 768w" sizes="(max-width: 839px) 100vw, 839px" /></figure>
<ul>
<li>In the <code>"Add arguments (optional)</code>” box, add the name of your Python file.</li>
<li>In the <code>"Start in (optional)"</code> box, paste the location of the Python file that you copied previously.</li>
</ul>
<figure class="wp-block-image size-full is-style-default"><img decoding="async" loading="lazy" width="454" height="494" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-298.png" alt="" class="wp-image-995457" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-298.png 454w, https://blog.finxter.com/wp-content/uplo...76x300.png 276w" sizes="(max-width: 454px) 100vw, 454px" /></figure>
<ul>
<li>Click “OK”</li>
<li>Go to “Triggers” ⮕ Select “New”</li>
</ul>
<figure class="wp-block-image size-full is-style-default"><img decoding="async" loading="lazy" width="644" height="563" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-296.png" alt="" class="wp-image-995406" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-296.png 644w, https://blog.finxter.com/wp-content/uplo...00x262.png 300w" sizes="(max-width: 644px) 100vw, 644px" /></figure>
<ul>
<li>Choose the repetition that you want. Here you can schedule Python scripts to run daily, weekly, monthly or just one time.</li>
<li>Click “OK”</li>
</ul>
<p>Once, you have set this up, your trigger is now active and your Python script will run automatically every day.</p>
<h2>Summary</h2>
<p>Hurrah! You have successfully set up your automated backup script to take the necessary backups on a scheduled time every day without the need to do anything manually. I hope the complete walkthrough of the project helped you! <strong><a href="https://blog.finxter.com/subscribe/" target="_blank" rel="noreferrer noopener">Subscribe</a></strong> and stay tuned for more interesting projects in the future.</p>
<p> </p>
</div>


https://www.sickgaming.net/blog/2022/12/...th-python/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016