I want to write a short backup script in Bash that lets me choose a directory that I want to save, and then compress it. I've got that done.
Next, I wanted to make it so that I could compare the size of the files that are to be copied. I used du -b /example/directory | cut -f1
. That got me the sizes of folders in that directory, without their names. But I can't really compare it to a value using the if statement, because it isn't an integer statement.
This is my code so far.
#!/bin/bash
#Which folders to backup
backup_files="/home"
# Where to save
dest="/home/student"
# Check size of each folder
file_size=$(du -b /example/directory | cut -f1)
# Size limit
check_size=1000
# Archive name
day=$(date +%A)
hostname=$(hostname -s)
archive_file="$hostname-$day.tar.gz"
# Here's the problem I have
if [ "$file_size" -le "$check_size" ]; then
tar -vczf /$dest/$archive_file $backup_files
fi
echo "Backup finished"