Skip to content

Commit fe1cfe4

Browse files
authored
Merge pull request #151 from VionFrancois/master
Estimated backup size
2 parents dfd2fda + 320fab9 commit fe1cfe4

File tree

3 files changed

+75
-16
lines changed

3 files changed

+75
-16
lines changed

backup.sh

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,6 @@ DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
7070
# Load all functions in ./functions
7171
for f in "$DIR"/functions/*.sh; do source "$f"; done
7272

73-
# Ensure that there's enough space on the device
74-
# TODO: Check this based on the size of the backup (or the device's storage capacity) instead of a hardcoded value of 100GB
75-
if ! enough_free_space "."; then
76-
cecho "Less than 100GB of free space available in the current directory. You may encounter issues if working with large backups."
77-
fi
78-
7973
check_adb_connection
8074

8175
if [ ! -v mode ]; then

functions/backup_func.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,23 @@ function backup_func() {
5151
esac
5252
done
5353

54+
# Ensure that there's enough space in the directory according to what is backed up
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"
65+
cecho "Exiting..."
66+
exit 1
67+
else
68+
cecho "Enough space in the current directory. Estimated backup size: ${bkp_size_mb} MB"
69+
fi
70+
5471
# The companion app is needed only for contact backups.
5572
mkdir -p ./backup-tmp/Contacts # Always created for backwards compatibility
5673
mkdir -p ./backup-tmp/SMS

functions/helper.sh

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,70 @@ function wait_for_enter() {
1010
fi
1111
}
1212

13-
# Checks if the user has at least 100GB of free space in a given directory
14-
# Usage: enough_free_space <directory> (optional size threshold in kilobytes)
13+
# Estimate the backup size based on what is backed up
14+
function estimate_backup_size() {
15+
local backup_size=0
16+
17+
if [ "$backup_contacts" = "yes" ]; then
18+
local contacts_count=$(adb shell content query --uri content://contacts/people | wc -l)
19+
local sms_count=$(adb shell content query --uri content://sms/ | wc -l)
20+
local call_log_count=$(adb shell content query --uri content://call_log/calls | wc -l)
21+
22+
# Here we estimate that a contact is 4 KB, an SMS is 1 KB and a call log is 0,5 KB
23+
local contacts_size=$(echo "$contacts_count * 4" | bc)
24+
local sms_size=$(echo "$sms_count * 1" | bc)
25+
local calls_size=$(echo "$call_log_count * 0.5" | bc)
26+
backup_size=$(echo "$backup_size + $contacts_size + $sms_size + $calls_size" | bc)
27+
fi
28+
29+
if [ "$backup_storage" = "yes" ]; then
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}')
32+
backup_size=$(echo "$backup_size + $storage_size" | bc)
33+
fi
34+
35+
if [ "$backup_apps" = "yes" ]; then
36+
local apks_size=$(adb shell 'for p in $(pm list packages -3 -f | sed -E "s/package://; s/=.*//"); do stat -c%s "$p" 2>/dev/null; done' | awk '{s+=$1} END {print int(s/1024)}')
37+
backup_size=$(echo "$backup_size + $apks_size" | bc)
38+
fi
39+
40+
backup_size=$(echo "$backup_size" | awk '{print int($1)}')
41+
echo "$backup_size"
42+
}
43+
44+
# Checks if the user has enough free space to backup the device in the current directory
45+
# Usage: enough_free_space <directory> <estimated_size_var>
1546
# Returns 0 (enough space) or 1 (not enough space)
47+
# Stores the estimated size in the variable named by the second parameter
1648
function enough_free_space() {
1749
local directory="$1"
18-
local size_threshold="$2"
19-
if [ -z "$size_threshold" ]; then
20-
size_threshold=100000000 # 100GB
50+
local -n estimated_size_ref="$2" # Use nameref for indirect assignment
51+
52+
local backup_size=$(estimate_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))
2166
fi
22-
# Convert the size threshold to a normal number in case it's in scientific notation
23-
size_threshold=$(echo "$size_threshold" | awk '{printf "%.0f\n", $1}')
24-
# Get the free space in the directory in kilobytes
25-
local free_space=$(df -k "$directory" | tail -n 1 | awk '{print $4}')
26-
if [ "$free_space" -lt "$size_threshold" ]; then
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
2774
return 1
2875
fi
76+
2977
return 0
3078
}
3179

0 commit comments

Comments
 (0)