We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
bug: Google Drive backup file names malformed
## Description of the issue

The files are uploaded with the full file path as the file name.
This makes extracting and restoring the files difficult.

![Screen Shot 2022-03-01 at 16 49 56](https://user-images.githubusercontent.com/64409021/156212763-43172946-6f0a-41ae-add4-9fb56fc21edd.png)

## Context information (for bug reports)

**Output of `bench version`**
```
ERPNext: v13.19.0
Frappe Framework: v13.19.0
```

## Steps to reproduce the issue

1. Back up to Google Drive

### Observed result
Malformed file names

### Expected result
Normal file names


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `frappe/integrations/doctype/google_drive/google_drive.py`
Content:
```
1 # Copyright (c) 2019, Frappe Technologies and contributors
2 # License: MIT. See LICENSE
3 
4 import os
5 from urllib.parse import quote
6 
7 from apiclient.http import MediaFileUpload
8 from googleapiclient.errors import HttpError
9 
10 import frappe
11 from frappe import _
12 from frappe.integrations.google_oauth import GoogleOAuth
13 from frappe.integrations.offsite_backup_utils import (
14 	get_latest_backup_file,
15 	send_email,
16 	validate_file_size,
17 )
18 from frappe.model.document import Document
19 from frappe.utils import get_backups_path, get_bench_path
20 from frappe.utils.background_jobs import enqueue
21 from frappe.utils.backups import new_backup
22 
23 
24 class GoogleDrive(Document):
25 	def validate(self):
26 		doc_before_save = self.get_doc_before_save()
27 		if doc_before_save and doc_before_save.backup_folder_name != self.backup_folder_name:
28 			self.backup_folder_id = ""
29 
30 	def get_access_token(self):
31 		if not self.refresh_token:
32 			button_label = frappe.bold(_("Allow Google Drive Access"))
33 			raise frappe.ValidationError(_("Click on {0} to generate Refresh Token.").format(button_label))
34 
35 		oauth_obj = GoogleOAuth("drive")
36 		r = oauth_obj.refresh_access_token(
37 			self.get_password(fieldname="refresh_token", raise_exception=False)
38 		)
39 
40 		return r.get("access_token")
41 
42 
43 @frappe.whitelist(methods=["POST"])
44 def authorize_access(reauthorize=False, code=None):
45 	"""
46 	If no Authorization code get it from Google and then request for Refresh Token.
47 	Google Contact Name is set to flags to set_value after Authorization Code is obtained.
48 	"""
49 
50 	oauth_code = (
51 		frappe.db.get_single_value("Google Drive", "authorization_code") if not code else code
52 	)
53 	oauth_obj = GoogleOAuth("drive")
54 
55 	if not oauth_code or reauthorize:
56 		if reauthorize:
57 			frappe.db.set_single_value("Google Drive", "backup_folder_id", "")
58 		return oauth_obj.get_authentication_url(
59 			{
60 				"redirect": f"/app/Form/{quote('Google Drive')}",
61 			},
62 		)
63 
64 	r = oauth_obj.authorize(oauth_code)
65 	frappe.db.set_single_value(
66 		"Google Drive",
67 		{"authorization_code": oauth_code, "refresh_token": r.get("refresh_token")},
68 	)
69 
70 
71 def get_google_drive_object():
72 	"""
73 	Returns an object of Google Drive.
74 	"""
75 	account = frappe.get_doc("Google Drive")
76 	oauth_obj = GoogleOAuth("drive")
77 
78 	google_drive = oauth_obj.get_google_service_object(
79 		account.get_access_token(),
80 		account.get_password(fieldname="indexing_refresh_token", raise_exception=False),
81 	)
82 
83 	return google_drive, account
84 
85 
86 def check_for_folder_in_google_drive():
87 	"""Checks if folder exists in Google Drive else create it."""
88 
89 	def _create_folder_in_google_drive(google_drive, account):
90 		file_metadata = {
91 			"name": account.backup_folder_name,
92 			"mimeType": "application/vnd.google-apps.folder",
93 		}
94 
95 		try:
96 			folder = google_drive.files().create(body=file_metadata, fields="id").execute()
97 			frappe.db.set_single_value("Google Drive", "backup_folder_id", folder.get("id"))
98 			frappe.db.commit()
99 		except HttpError as e:
100 			frappe.throw(
101 				_("Google Drive - Could not create folder in Google Drive - Error Code {0}").format(e)
102 			)
103 
104 	google_drive, account = get_google_drive_object()
105 
106 	if account.backup_folder_id:
107 		return
108 
109 	backup_folder_exists = False
110 
111 	try:
112 		google_drive_folders = (
113 			google_drive.files().list(q="mimeType='application/vnd.google-apps.folder'").execute()
114 		)
115 	except HttpError as e:
116 		frappe.throw(
117 			_("Google Drive - Could not find folder in Google Drive - Error Code {0}").format(e)
118 		)
119 
120 	for f in google_drive_folders.get("files"):
121 		if f.get("name") == account.backup_folder_name:
122 			frappe.db.set_single_value("Google Drive", "backup_folder_id", f.get("id"))
123 			frappe.db.commit()
124 			backup_folder_exists = True
125 			break
126 
127 	if not backup_folder_exists:
128 		_create_folder_in_google_drive(google_drive, account)
129 
130 
131 @frappe.whitelist()
132 def take_backup():
133 	"""Enqueue longjob for taking backup to Google Drive"""
134 	enqueue(
135 		"frappe.integrations.doctype.google_drive.google_drive.upload_system_backup_to_google_drive",
136 		queue="long",
137 		timeout=1500,
138 	)
139 	frappe.msgprint(_("Queued for backup. It may take a few minutes to an hour."))
140 
141 
142 def upload_system_backup_to_google_drive():
143 	"""
144 	Upload system backup to Google Drive
145 	"""
146 	# Get Google Drive Object
147 	google_drive, account = get_google_drive_object()
148 
149 	# Check if folder exists in Google Drive
150 	check_for_folder_in_google_drive()
151 	account.load_from_db()
152 
153 	validate_file_size()
154 
155 	if frappe.flags.create_new_backup:
156 		set_progress(1, "Backing up Data.")
157 		backup = new_backup()
158 		file_urls = []
159 		file_urls.append(backup.backup_path_db)
160 		file_urls.append(backup.backup_path_conf)
161 
162 		if account.file_backup:
163 			file_urls.append(backup.backup_path_files)
164 			file_urls.append(backup.backup_path_private_files)
165 	else:
166 		file_urls = get_latest_backup_file(with_files=account.file_backup)
167 
168 	for fileurl in file_urls:
169 		if not fileurl:
170 			continue
171 
172 		file_metadata = {"name": fileurl, "parents": [account.backup_folder_id]}
173 
174 		try:
175 			media = MediaFileUpload(
176 				get_absolute_path(filename=fileurl), mimetype="application/gzip", resumable=True
177 			)
178 		except OSError as e:
179 			frappe.throw(_("Google Drive - Could not locate - {0}").format(e))
180 
181 		try:
182 			set_progress(2, "Uploading backup to Google Drive.")
183 			google_drive.files().create(body=file_metadata, media_body=media, fields="id").execute()
184 		except HttpError as e:
185 			send_email(False, "Google Drive", "Google Drive", "email", error_status=e)
186 
187 	set_progress(3, "Uploading successful.")
188 	frappe.db.set_single_value("Google Drive", "last_backup_on", frappe.utils.now_datetime())
189 	send_email(True, "Google Drive", "Google Drive", "email")
190 	return _("Google Drive Backup Successful.")
191 
192 
193 def daily_backup():
194 	drive_settings = frappe.db.get_singles_dict("Google Drive", cast=True)
195 	if drive_settings.enable and drive_settings.frequency == "Daily":
196 		upload_system_backup_to_google_drive()
197 
198 
199 def weekly_backup():
200 	drive_settings = frappe.db.get_singles_dict("Google Drive", cast=True)
201 	if drive_settings.enable and drive_settings.frequency == "Weekly":
202 		upload_system_backup_to_google_drive()
203 
204 
205 def get_absolute_path(filename):
206 	file_path = os.path.join(get_backups_path()[2:], os.path.basename(filename))
207 	return f"{get_bench_path()}/sites/{file_path}"
208 
209 
210 def set_progress(progress, message):
211 	frappe.publish_realtime(
212 		"upload_to_google_drive",
213 		dict(progress=progress, total=3, message=message),
214 		user=frappe.session.user,
215 	)
216
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/frappe/integrations/doctype/google_drive/google_drive.py b/frappe/integrations/doctype/google_drive/google_drive.py
--- a/frappe/integrations/doctype/google_drive/google_drive.py
+++ b/frappe/integrations/doctype/google_drive/google_drive.py
@@ -169,7 +169,7 @@
 		if not fileurl:
 			continue
 
-		file_metadata = {"name": fileurl, "parents": [account.backup_folder_id]}
+		file_metadata = {"name": os.path.basename(fileurl), "parents": [account.backup_folder_id]}
 
 		try:
 			media = MediaFileUpload(

{"golden_diff": "diff --git a/frappe/integrations/doctype/google_drive/google_drive.py b/frappe/integrations/doctype/google_drive/google_drive.py\n--- a/frappe/integrations/doctype/google_drive/google_drive.py\n+++ b/frappe/integrations/doctype/google_drive/google_drive.py\n@@ -169,7 +169,7 @@\n \t\tif not fileurl:\n \t\t\tcontinue\n \n-\t\tfile_metadata = {\"name\": fileurl, \"parents\": [account.backup_folder_id]}\n+\t\tfile_metadata = {\"name\": os.path.basename(fileurl), \"parents\": [account.backup_folder_id]}\n \n \t\ttry:\n \t\t\tmedia = MediaFileUpload(\n", "issue": "bug: Google Drive backup file names malformed\n## Description of the issue\r\n\r\nThe files are uploaded with the full file path as the file name.\r\nThis makes extracting and restoring the files difficult.\r\n\r\n![Screen Shot 2022-03-01 at 16 49 56](https://user-images.githubusercontent.com/64409021/156212763-43172946-6f0a-41ae-add4-9fb56fc21edd.png)\r\n\r\n## Context information (for bug reports)\r\n\r\n**Output of `bench version`**\r\n```\r\nERPNext: v13.19.0\r\nFrappe Framework: v13.19.0\r\n```\r\n\r\n## Steps to reproduce the issue\r\n\r\n1. Back up to Google Drive\r\n\r\n### Observed result\r\nMalformed file names\r\n\r\n### Expected result\r\nNormal file names\r\n\n", "before_files": [{"content": "# Copyright (c) 2019, Frappe Technologies and contributors\n# License: MIT. See LICENSE\n\nimport os\nfrom urllib.parse import quote\n\nfrom apiclient.http import MediaFileUpload\nfrom googleapiclient.errors import HttpError\n\nimport frappe\nfrom frappe import _\nfrom frappe.integrations.google_oauth import GoogleOAuth\nfrom frappe.integrations.offsite_backup_utils import (\n\tget_latest_backup_file,\n\tsend_email,\n\tvalidate_file_size,\n)\nfrom frappe.model.document import Document\nfrom frappe.utils import get_backups_path, get_bench_path\nfrom frappe.utils.background_jobs import enqueue\nfrom frappe.utils.backups import new_backup\n\n\nclass GoogleDrive(Document):\n\tdef validate(self):\n\t\tdoc_before_save = self.get_doc_before_save()\n\t\tif doc_before_save and doc_before_save.backup_folder_name != self.backup_folder_name:\n\t\t\tself.backup_folder_id = \"\"\n\n\tdef get_access_token(self):\n\t\tif not self.refresh_token:\n\t\t\tbutton_label = frappe.bold(_(\"Allow Google Drive Access\"))\n\t\t\traise frappe.ValidationError(_(\"Click on {0} to generate Refresh Token.\").format(button_label))\n\n\t\toauth_obj = GoogleOAuth(\"drive\")\n\t\tr = oauth_obj.refresh_access_token(\n\t\t\tself.get_password(fieldname=\"refresh_token\", raise_exception=False)\n\t\t)\n\n\t\treturn r.get(\"access_token\")\n\n\n@frappe.whitelist(methods=[\"POST\"])\ndef authorize_access(reauthorize=False, code=None):\n\t\"\"\"\n\tIf no Authorization code get it from Google and then request for Refresh Token.\n\tGoogle Contact Name is set to flags to set_value after Authorization Code is obtained.\n\t\"\"\"\n\n\toauth_code = (\n\t\tfrappe.db.get_single_value(\"Google Drive\", \"authorization_code\") if not code else code\n\t)\n\toauth_obj = GoogleOAuth(\"drive\")\n\n\tif not oauth_code or reauthorize:\n\t\tif reauthorize:\n\t\t\tfrappe.db.set_single_value(\"Google Drive\", \"backup_folder_id\", \"\")\n\t\treturn oauth_obj.get_authentication_url(\n\t\t\t{\n\t\t\t\t\"redirect\": f\"/app/Form/{quote('Google Drive')}\",\n\t\t\t},\n\t\t)\n\n\tr = oauth_obj.authorize(oauth_code)\n\tfrappe.db.set_single_value(\n\t\t\"Google Drive\",\n\t\t{\"authorization_code\": oauth_code, \"refresh_token\": r.get(\"refresh_token\")},\n\t)\n\n\ndef get_google_drive_object():\n\t\"\"\"\n\tReturns an object of Google Drive.\n\t\"\"\"\n\taccount = frappe.get_doc(\"Google Drive\")\n\toauth_obj = GoogleOAuth(\"drive\")\n\n\tgoogle_drive = oauth_obj.get_google_service_object(\n\t\taccount.get_access_token(),\n\t\taccount.get_password(fieldname=\"indexing_refresh_token\", raise_exception=False),\n\t)\n\n\treturn google_drive, account\n\n\ndef check_for_folder_in_google_drive():\n\t\"\"\"Checks if folder exists in Google Drive else create it.\"\"\"\n\n\tdef _create_folder_in_google_drive(google_drive, account):\n\t\tfile_metadata = {\n\t\t\t\"name\": account.backup_folder_name,\n\t\t\t\"mimeType\": \"application/vnd.google-apps.folder\",\n\t\t}\n\n\t\ttry:\n\t\t\tfolder = google_drive.files().create(body=file_metadata, fields=\"id\").execute()\n\t\t\tfrappe.db.set_single_value(\"Google Drive\", \"backup_folder_id\", folder.get(\"id\"))\n\t\t\tfrappe.db.commit()\n\t\texcept HttpError as e:\n\t\t\tfrappe.throw(\n\t\t\t\t_(\"Google Drive - Could not create folder in Google Drive - Error Code {0}\").format(e)\n\t\t\t)\n\n\tgoogle_drive, account = get_google_drive_object()\n\n\tif account.backup_folder_id:\n\t\treturn\n\n\tbackup_folder_exists = False\n\n\ttry:\n\t\tgoogle_drive_folders = (\n\t\t\tgoogle_drive.files().list(q=\"mimeType='application/vnd.google-apps.folder'\").execute()\n\t\t)\n\texcept HttpError as e:\n\t\tfrappe.throw(\n\t\t\t_(\"Google Drive - Could not find folder in Google Drive - Error Code {0}\").format(e)\n\t\t)\n\n\tfor f in google_drive_folders.get(\"files\"):\n\t\tif f.get(\"name\") == account.backup_folder_name:\n\t\t\tfrappe.db.set_single_value(\"Google Drive\", \"backup_folder_id\", f.get(\"id\"))\n\t\t\tfrappe.db.commit()\n\t\t\tbackup_folder_exists = True\n\t\t\tbreak\n\n\tif not backup_folder_exists:\n\t\t_create_folder_in_google_drive(google_drive, account)\n\n\n@frappe.whitelist()\ndef take_backup():\n\t\"\"\"Enqueue longjob for taking backup to Google Drive\"\"\"\n\tenqueue(\n\t\t\"frappe.integrations.doctype.google_drive.google_drive.upload_system_backup_to_google_drive\",\n\t\tqueue=\"long\",\n\t\ttimeout=1500,\n\t)\n\tfrappe.msgprint(_(\"Queued for backup. It may take a few minutes to an hour.\"))\n\n\ndef upload_system_backup_to_google_drive():\n\t\"\"\"\n\tUpload system backup to Google Drive\n\t\"\"\"\n\t# Get Google Drive Object\n\tgoogle_drive, account = get_google_drive_object()\n\n\t# Check if folder exists in Google Drive\n\tcheck_for_folder_in_google_drive()\n\taccount.load_from_db()\n\n\tvalidate_file_size()\n\n\tif frappe.flags.create_new_backup:\n\t\tset_progress(1, \"Backing up Data.\")\n\t\tbackup = new_backup()\n\t\tfile_urls = []\n\t\tfile_urls.append(backup.backup_path_db)\n\t\tfile_urls.append(backup.backup_path_conf)\n\n\t\tif account.file_backup:\n\t\t\tfile_urls.append(backup.backup_path_files)\n\t\t\tfile_urls.append(backup.backup_path_private_files)\n\telse:\n\t\tfile_urls = get_latest_backup_file(with_files=account.file_backup)\n\n\tfor fileurl in file_urls:\n\t\tif not fileurl:\n\t\t\tcontinue\n\n\t\tfile_metadata = {\"name\": fileurl, \"parents\": [account.backup_folder_id]}\n\n\t\ttry:\n\t\t\tmedia = MediaFileUpload(\n\t\t\t\tget_absolute_path(filename=fileurl), mimetype=\"application/gzip\", resumable=True\n\t\t\t)\n\t\texcept OSError as e:\n\t\t\tfrappe.throw(_(\"Google Drive - Could not locate - {0}\").format(e))\n\n\t\ttry:\n\t\t\tset_progress(2, \"Uploading backup to Google Drive.\")\n\t\t\tgoogle_drive.files().create(body=file_metadata, media_body=media, fields=\"id\").execute()\n\t\texcept HttpError as e:\n\t\t\tsend_email(False, \"Google Drive\", \"Google Drive\", \"email\", error_status=e)\n\n\tset_progress(3, \"Uploading successful.\")\n\tfrappe.db.set_single_value(\"Google Drive\", \"last_backup_on\", frappe.utils.now_datetime())\n\tsend_email(True, \"Google Drive\", \"Google Drive\", \"email\")\n\treturn _(\"Google Drive Backup Successful.\")\n\n\ndef daily_backup():\n\tdrive_settings = frappe.db.get_singles_dict(\"Google Drive\", cast=True)\n\tif drive_settings.enable and drive_settings.frequency == \"Daily\":\n\t\tupload_system_backup_to_google_drive()\n\n\ndef weekly_backup():\n\tdrive_settings = frappe.db.get_singles_dict(\"Google Drive\", cast=True)\n\tif drive_settings.enable and drive_settings.frequency == \"Weekly\":\n\t\tupload_system_backup_to_google_drive()\n\n\ndef get_absolute_path(filename):\n\tfile_path = os.path.join(get_backups_path()[2:], os.path.basename(filename))\n\treturn f\"{get_bench_path()}/sites/{file_path}\"\n\n\ndef set_progress(progress, message):\n\tfrappe.publish_realtime(\n\t\t\"upload_to_google_drive\",\n\t\tdict(progress=progress, total=3, message=message),\n\t\tuser=frappe.session.user,\n\t)\n", "path": "frappe/integrations/doctype/google_drive/google_drive.py"}], "after_files": [{"content": "# Copyright (c) 2019, Frappe Technologies and contributors\n# License: MIT. See LICENSE\n\nimport os\nfrom urllib.parse import quote\n\nfrom apiclient.http import MediaFileUpload\nfrom googleapiclient.errors import HttpError\n\nimport frappe\nfrom frappe import _\nfrom frappe.integrations.google_oauth import GoogleOAuth\nfrom frappe.integrations.offsite_backup_utils import (\n\tget_latest_backup_file,\n\tsend_email,\n\tvalidate_file_size,\n)\nfrom frappe.model.document import Document\nfrom frappe.utils import get_backups_path, get_bench_path\nfrom frappe.utils.background_jobs import enqueue\nfrom frappe.utils.backups import new_backup\n\n\nclass GoogleDrive(Document):\n\tdef validate(self):\n\t\tdoc_before_save = self.get_doc_before_save()\n\t\tif doc_before_save and doc_before_save.backup_folder_name != self.backup_folder_name:\n\t\t\tself.backup_folder_id = \"\"\n\n\tdef get_access_token(self):\n\t\tif not self.refresh_token:\n\t\t\tbutton_label = frappe.bold(_(\"Allow Google Drive Access\"))\n\t\t\traise frappe.ValidationError(_(\"Click on {0} to generate Refresh Token.\").format(button_label))\n\n\t\toauth_obj = GoogleOAuth(\"drive\")\n\t\tr = oauth_obj.refresh_access_token(\n\t\t\tself.get_password(fieldname=\"refresh_token\", raise_exception=False)\n\t\t)\n\n\t\treturn r.get(\"access_token\")\n\n\n@frappe.whitelist(methods=[\"POST\"])\ndef authorize_access(reauthorize=False, code=None):\n\t\"\"\"\n\tIf no Authorization code get it from Google and then request for Refresh Token.\n\tGoogle Contact Name is set to flags to set_value after Authorization Code is obtained.\n\t\"\"\"\n\n\toauth_code = (\n\t\tfrappe.db.get_single_value(\"Google Drive\", \"authorization_code\") if not code else code\n\t)\n\toauth_obj = GoogleOAuth(\"drive\")\n\n\tif not oauth_code or reauthorize:\n\t\tif reauthorize:\n\t\t\tfrappe.db.set_single_value(\"Google Drive\", \"backup_folder_id\", \"\")\n\t\treturn oauth_obj.get_authentication_url(\n\t\t\t{\n\t\t\t\t\"redirect\": f\"/app/Form/{quote('Google Drive')}\",\n\t\t\t},\n\t\t)\n\n\tr = oauth_obj.authorize(oauth_code)\n\tfrappe.db.set_single_value(\n\t\t\"Google Drive\",\n\t\t{\"authorization_code\": oauth_code, \"refresh_token\": r.get(\"refresh_token\")},\n\t)\n\n\ndef get_google_drive_object():\n\t\"\"\"\n\tReturns an object of Google Drive.\n\t\"\"\"\n\taccount = frappe.get_doc(\"Google Drive\")\n\toauth_obj = GoogleOAuth(\"drive\")\n\n\tgoogle_drive = oauth_obj.get_google_service_object(\n\t\taccount.get_access_token(),\n\t\taccount.get_password(fieldname=\"indexing_refresh_token\", raise_exception=False),\n\t)\n\n\treturn google_drive, account\n\n\ndef check_for_folder_in_google_drive():\n\t\"\"\"Checks if folder exists in Google Drive else create it.\"\"\"\n\n\tdef _create_folder_in_google_drive(google_drive, account):\n\t\tfile_metadata = {\n\t\t\t\"name\": account.backup_folder_name,\n\t\t\t\"mimeType\": \"application/vnd.google-apps.folder\",\n\t\t}\n\n\t\ttry:\n\t\t\tfolder = google_drive.files().create(body=file_metadata, fields=\"id\").execute()\n\t\t\tfrappe.db.set_single_value(\"Google Drive\", \"backup_folder_id\", folder.get(\"id\"))\n\t\t\tfrappe.db.commit()\n\t\texcept HttpError as e:\n\t\t\tfrappe.throw(\n\t\t\t\t_(\"Google Drive - Could not create folder in Google Drive - Error Code {0}\").format(e)\n\t\t\t)\n\n\tgoogle_drive, account = get_google_drive_object()\n\n\tif account.backup_folder_id:\n\t\treturn\n\n\tbackup_folder_exists = False\n\n\ttry:\n\t\tgoogle_drive_folders = (\n\t\t\tgoogle_drive.files().list(q=\"mimeType='application/vnd.google-apps.folder'\").execute()\n\t\t)\n\texcept HttpError as e:\n\t\tfrappe.throw(\n\t\t\t_(\"Google Drive - Could not find folder in Google Drive - Error Code {0}\").format(e)\n\t\t)\n\n\tfor f in google_drive_folders.get(\"files\"):\n\t\tif f.get(\"name\") == account.backup_folder_name:\n\t\t\tfrappe.db.set_single_value(\"Google Drive\", \"backup_folder_id\", f.get(\"id\"))\n\t\t\tfrappe.db.commit()\n\t\t\tbackup_folder_exists = True\n\t\t\tbreak\n\n\tif not backup_folder_exists:\n\t\t_create_folder_in_google_drive(google_drive, account)\n\n\n@frappe.whitelist()\ndef take_backup():\n\t\"\"\"Enqueue longjob for taking backup to Google Drive\"\"\"\n\tenqueue(\n\t\t\"frappe.integrations.doctype.google_drive.google_drive.upload_system_backup_to_google_drive\",\n\t\tqueue=\"long\",\n\t\ttimeout=1500,\n\t)\n\tfrappe.msgprint(_(\"Queued for backup. It may take a few minutes to an hour.\"))\n\n\ndef upload_system_backup_to_google_drive():\n\t\"\"\"\n\tUpload system backup to Google Drive\n\t\"\"\"\n\t# Get Google Drive Object\n\tgoogle_drive, account = get_google_drive_object()\n\n\t# Check if folder exists in Google Drive\n\tcheck_for_folder_in_google_drive()\n\taccount.load_from_db()\n\n\tvalidate_file_size()\n\n\tif frappe.flags.create_new_backup:\n\t\tset_progress(1, \"Backing up Data.\")\n\t\tbackup = new_backup()\n\t\tfile_urls = []\n\t\tfile_urls.append(backup.backup_path_db)\n\t\tfile_urls.append(backup.backup_path_conf)\n\n\t\tif account.file_backup:\n\t\t\tfile_urls.append(backup.backup_path_files)\n\t\t\tfile_urls.append(backup.backup_path_private_files)\n\telse:\n\t\tfile_urls = get_latest_backup_file(with_files=account.file_backup)\n\n\tfor fileurl in file_urls:\n\t\tif not fileurl:\n\t\t\tcontinue\n\n\t\tfile_metadata = {\"name\": os.path.basename(fileurl), \"parents\": [account.backup_folder_id]}\n\n\t\ttry:\n\t\t\tmedia = MediaFileUpload(\n\t\t\t\tget_absolute_path(filename=fileurl), mimetype=\"application/gzip\", resumable=True\n\t\t\t)\n\t\texcept OSError as e:\n\t\t\tfrappe.throw(_(\"Google Drive - Could not locate - {0}\").format(e))\n\n\t\ttry:\n\t\t\tset_progress(2, \"Uploading backup to Google Drive.\")\n\t\t\tgoogle_drive.files().create(body=file_metadata, media_body=media, fields=\"id\").execute()\n\t\texcept HttpError as e:\n\t\t\tsend_email(False, \"Google Drive\", \"Google Drive\", \"email\", error_status=e)\n\n\tset_progress(3, \"Uploading successful.\")\n\tfrappe.db.set_single_value(\"Google Drive\", \"last_backup_on\", frappe.utils.now_datetime())\n\tsend_email(True, \"Google Drive\", \"Google Drive\", \"email\")\n\treturn _(\"Google Drive Backup Successful.\")\n\n\ndef daily_backup():\n\tdrive_settings = frappe.db.get_singles_dict(\"Google Drive\", cast=True)\n\tif drive_settings.enable and drive_settings.frequency == \"Daily\":\n\t\tupload_system_backup_to_google_drive()\n\n\ndef weekly_backup():\n\tdrive_settings = frappe.db.get_singles_dict(\"Google Drive\", cast=True)\n\tif drive_settings.enable and drive_settings.frequency == \"Weekly\":\n\t\tupload_system_backup_to_google_drive()\n\n\ndef get_absolute_path(filename):\n\tfile_path = os.path.join(get_backups_path()[2:], os.path.basename(filename))\n\treturn f\"{get_bench_path()}/sites/{file_path}\"\n\n\ndef set_progress(progress, message):\n\tfrappe.publish_realtime(\n\t\t\"upload_to_google_drive\",\n\t\tdict(progress=progress, total=3, message=message),\n\t\tuser=frappe.session.user,\n\t)\n", "path": "frappe/integrations/doctype/google_drive/google_drive.py"}]}