Repository: frappe/frappe

Issue:
S3 automatic backup fails when backup folder is empty
<!--
Welcome to the Frappe Framework issue tracker! Before creating an issue, please heed the following:

1. This tracker should only be used to report bugs and request features / enhancements to Frappe
    - For questions and general support, use https://stackoverflow.com/questions/tagged/frappe
    - For documentation issues, refer to https://frappeframework.com/docs/user/en or the developer cheetsheet https://github.com/frappe/frappe/wiki/Developer-Cheatsheet
2. Use the search function before creating a new issue. Duplicates will be closed and directed to
   the original discussion.
3. When making a bug report, make sure you provide all required information. The easier it is for
   maintainers to reproduce, the faster it'll be fixed.
4. If you think you know what the reason for the bug is, share it with us. Maybe put in a PR 😉
-->

## Description of the issue
`frappe.integrations.doctype.s3_backup_settings.s3_backup_settings.take_backups_s3` fails when the backup folder (site_name/private/backups/) is empty when the function is called. 

This occurs because `frappe.integrations.offsite_backup_utils.get_file_size` does not anticipate a situation where its `file_path` argument is invalid i.e `None`.

This bug affects v13 and v12.

## Context information (for bug reports)

**Output of `bench version`**
```
frappe 12.24.0
```

## Steps to reproduce the issue

1. Delete _all_ files in the backup folder
2. Call `take_backups_s3`
3.

### Observed result
When the scheduled backup task runs, you will receive an email with this kind of stack trace:
```
*Backup Upload Failed!*

Oops, your automated backup to Amazon S3 failed.

Error message: Traceback (most recent call last): File "/home/ubuntu/frappe-bench/apps/frappe/frappe/integrations/doctype/s3_backup_settings/s3_backup_settings.py", line 83, in take_backups_s3 validate_file_size() File "/home/ubuntu/frappe-bench/apps/frappe/frappe/integrations/offsite_backup_utils.py", line 105, in validate_file_size file_size = get_file_size(latest_file, unit="GB") File "/home/ubuntu/frappe-bench/apps/frappe/frappe/integrations/offsite_backup_utils.py", line 75, in get_file_size file_size = os.path.getsize(file_path) File "/usr/lib/python3.7/genericpath.py", line 50, in getsize return os.stat(filename).st_size TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType

Please contact your system manager for more information.
```
If you call it in your console, it will fail silently but you will observe that no backup file is created nor uploaded to S3.

### Expected result


### Stacktrace / full error message

```
Traceback (most recent call last): File "/home/ubuntu/frappe-bench/apps/frappe/frappe/integrations/doctype/s3_backup_settings/s3_backup_settings.py", line 83, in take_backups_s3 validate_file_size() File "/home/ubuntu/frappe-bench/apps/frappe/frappe/integrations/offsite_backup_utils.py", line 105, in validate_file_size file_size = get_file_size(latest_file, unit="GB") File "/home/ubuntu/frappe-bench/apps/frappe/frappe/integrations/offsite_backup_utils.py", line 75, in get_file_size file_size = os.path.getsize(file_path) File "/usr/lib/python3.7/genericpath.py", line 50, in getsize return os.stat(filename).st_size TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType
```

## Additional information

OS version / distribution, `Frappe` install method, etc.



Discussion:

Resolving patch:
diff --git a/frappe/integrations/offsite_backup_utils.py b/frappe/integrations/offsite_backup_utils.py
--- a/frappe/integrations/offsite_backup_utils.py
+++ b/frappe/integrations/offsite_backup_utils.py
@@ -65,10 +65,7 @@ def get_latest_backup_file(with_files=False):
 	return database, config
 
 
-def get_file_size(file_path, unit):
-	if not unit:
-		unit = "MB"
-
+def get_file_size(file_path, unit='MB'):
 	file_size = os.path.getsize(file_path)
 
 	memory_size_unit_mapper = {"KB": 1, "MB": 2, "GB": 3, "TB": 4}
@@ -99,7 +96,7 @@ def get_chunk_site(file_size):
 def validate_file_size():
 	frappe.flags.create_new_backup = True
 	latest_file, site_config = get_latest_backup_file()
-	file_size = get_file_size(latest_file, unit="GB")
+	file_size = get_file_size(latest_file, unit="GB") if latest_file else 0
 
 	if file_size > 1:
 		frappe.flags.create_new_backup = False


Test patch: