Skip to content

Commit 320fab9

Browse files
committed
Improve space check logic
- Modified `enough_free_space` to check both target and current directories - Estimates the storage size with du - Fixed showing the estimated size if there's enough space
1 parent a764705 commit 320fab9

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

functions/backup_func.sh

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,20 @@ function backup_func() {
5252
done
5353

5454
# Ensure that there's enough space in the directory according to what is backed up
55-
local space=$(enough_free_space "$archive_path")
56-
if [[ -n "$space" && "$space" -ne 0 ]]; then
57-
local bkp_size_mb=$(echo "scale=2; $space/1024" | bc)
58-
echo -e "\033[31mThere isn't enough space in the selected directory for the backup. Estimated backup size: $bkp_size_mb MB\033[0m"
55+
cecho "Estimating backup size, please wait..."
56+
57+
local estimated_size
58+
enough_free_space "$archive_path" estimated_size
59+
local fs_status=$?
60+
local bkp_size_mb=$(echo "scale=2; $estimated_size/1024" | bc)
61+
62+
if [ $fs_status -ne 0 ]; then
63+
echo -e "\033[31mThere isn't enough space for the backup. Estimated backup size: ${bkp_size_mb} MB\033[0m"
64+
echo -e "\033[31mFree up space on both the target and backup script locations (to handle temporary files). Double the space is needed if backing up to the drive the backup script is located on.\033[0m"
5965
cecho "Exiting..."
6066
exit 1
6167
else
62-
cecho "Enough space in the current directory. Estimated backup size: $bkp_size_mb MB"
68+
cecho "Enough space in the current directory. Estimated backup size: ${bkp_size_mb} MB"
6369
fi
6470

6571
# The companion app is needed only for contact backups.
@@ -210,4 +216,4 @@ backup_contacts: $backup_contacts
210216
cecho "Backed up successfully."
211217
cecho "Note: SMS messages and call logs cannot be restored by Open Android Backup at the moment. They are included in the backup archive for your own purposes."
212218
cecho "You can find them by opening the backup archive using 7-Zip."
213-
}
219+
}

functions/helper.sh

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function estimate_backup_size() {
1818
local contacts_count=$(adb shell content query --uri content://contacts/people | wc -l)
1919
local sms_count=$(adb shell content query --uri content://sms/ | wc -l)
2020
local call_log_count=$(adb shell content query --uri content://call_log/calls | wc -l)
21-
21+
2222
# Here we estimate that a contact is 4 KB, an SMS is 1 KB and a call log is 0,5 KB
2323
local contacts_size=$(echo "$contacts_count * 4" | bc)
2424
local sms_size=$(echo "$sms_count * 1" | bc)
@@ -27,7 +27,8 @@ function estimate_backup_size() {
2727
fi
2828

2929
if [ "$backup_storage" = "yes" ]; then
30-
local storage_size=$(adb shell df -k /storage/emulated/0 | tail -n 1 | awk '{print $3}')
30+
# Use du to get the actual used space in KB
31+
local storage_size=$(adb shell du -sk /storage/emulated/0 | awk '{print $1}')
3132
backup_size=$(echo "$backup_size + $storage_size" | bc)
3233
fi
3334

@@ -41,17 +42,38 @@ function estimate_backup_size() {
4142
}
4243

4344
# Checks if the user has enough free space to backup the device in the current directory
44-
# Usage: enough_free_space <directory>
45-
# Returns 0 (enough space) or 1 (not enough space) and echoes the estimated size of the backup
45+
# Usage: enough_free_space <directory> <estimated_size_var>
46+
# Returns 0 (enough space) or 1 (not enough space)
47+
# Stores the estimated size in the variable named by the second parameter
4648
function enough_free_space() {
4749
local directory="$1"
50+
local -n estimated_size_ref="$2" # Use nameref for indirect assignment
51+
4852
local backup_size=$(estimate_backup_size)
49-
# Get the free space in the directory in kilobytes
50-
local free_space=$(df -k "$directory" | tail -n 1 | awk '{print $4}')
51-
if [ "$free_space" -lt "$backup_size" ]; then
52-
echo "$backup_size"
53+
estimated_size_ref=$backup_size
54+
local required_size=$backup_size
55+
56+
# The script first gathers all data to ./backup-tmp, compresses it into an archive and finally deletes the temporary directory.
57+
# Therefore, we need to bear both cases in mind.
58+
59+
# Get device IDs to check if directories are on the same drive
60+
local current_dir_device=$(stat -c %m .)
61+
local target_dir_device=$(stat -c %m "$directory")
62+
63+
# Double required space if that's the case
64+
if [ "$current_dir_device" = "$target_dir_device" ]; then
65+
required_size=$((backup_size * 2))
66+
fi
67+
68+
# Check free space in both the target directory and current working directory
69+
local target_free_space=$(df -k "$directory" | tail -n 1 | awk '{print $4}')
70+
local current_free_space=$(df -k . | tail -n 1 | awk '{print $4}')
71+
72+
# Check if either directory has insufficient space
73+
if [ "$target_free_space" -lt "$required_size" ] || [ "$current_free_space" -lt "$required_size" ]; then
5374
return 1
5475
fi
76+
5577
return 0
5678
}
5779

0 commit comments

Comments
 (0)