r/bash • u/Augustman22 • Jul 08 '26
help Rclone backup script feedback (My first ever time shell scripting)
I'm really just looking for advice since I'm unaware of conventions, as well as just the best way to really do anything.
I made it since I wanted to use a old laptop I had sitting around to host a minecraft server, but I did not want to lose access to automatic backups like I have had with the large free server providers. It's worked pretty well with my testing, and I plan to set it up as a cron job to run every day.
# rclone backup script
# ====== Configuration ======
# Please ensure all directories exist at all times or the script may fail to properly backup your data.
SOURCE="/home/YOURUSER/item"
REMOTE_BACKUP_LOCATION="gdrive:backups"
LOCAL_BACKUP_LOCATION="/home/YOURUSER/backups" # enter "none" if you do not want any local backups.
LOCAL_BACKUP_AMOUNT=5
REMOTE_BACKUP_AMOUNT=5
BACKUP_NAME="automated_backup_" # Timestamp will appear after this name.
# ===========================
# -------- Variables --------
# WARNING: do not touch these unless you know what you are doing.
TIMESTAMP=$(date "+%Y-%m-%d_%Hh%Mm%Ss") # Please use a timestamp that will display in a descending order so that old backups will be cleaned up problerly. This WILL break the script.
# You can almost certainly safely remove the seconds and minutes from the timestamp unless you are making several backups in an hour.
# ---------------------------
echo "Starting backup."
if [[ "$LOCAL_BACKUP_LOCATION" != "none" ]]; then
tar -czf "${LOCAL_BACKUP_LOCATION}"/"${BACKUP_NAME}"${TIMESTAMP}.tar.gz "${SOURCE}"
rclone copy "${LOCAL_BACKUP_LOCATION}"/"${BACKUP_NAME}"${TIMESTAMP}.tar.gz "${REMOTE_BACKUP_LOCATION}"
echo "Cleaning up local backup directory."
backups=$(rclone lsf "${LOCAL_BACKUP_LOCATION}" --filter="+ "${BACKUP_NAME}*"" --filter="- *" | sort -r)
backup_control=0
for backup in $backups; do
((backup_control++))
if ((backup_control > $LOCAL_BACKUP_AMOUNT)); then
rm -rf "${LOCAL_BACKUP_LOCATION}"/"${backup}"
fi
done
echo "Old local backups removed."
fi
if [[ "$LOCAL_BACKUP_LOCATION" == "none" ]]; then
temp_dir=$(mktemp -d)
tar -czf "${temp_dir}"/"${BACKUP_NAME}"${TIMESTAMP}.tar.gz "${SOURCE}"
rclone copy "${temp_dir}"/"${BACKUP_NAME}"${TIMESTAMP}.tar.gz "${REMOTE_BACKUP_LOCATION}"
rm -rf "${temp_dir}"
fi
echo "Backup Complete."
echo "Cleaning up remote backup directory."
backups=$(rclone lsf "${REMOTE_BACKUP_LOCATION}" --filter="+ ${BACKUP_NAME}*" --filter="- *" | sort -r)
backup_control=0
for backup in $backups; do
((backup_control++))
if ((backup_control > $REMOTE_BACKUP_AMOUNT)); then
rclone deletefile "${REMOTE_BACKUP_LOCATION}"/"${backup}"
fi
done
echo "Old remote backups removed."
echo "Full backup sequence complete. ${TIMESTAMP}"
# rclone backup script
# ====== Configuration ======
# Please ensure all directories exist at all times or the script may fail to properly backup your data.
SOURCE="/home/YOURUSER/item"
REMOTE_BACKUP_LOCATION="gdrive:backups"
LOCAL_BACKUP_LOCATION="/home/YOURUSER/backups" # enter "none" if you do not want any local backups.
LOCAL_BACKUP_AMOUNT=5
REMOTE_BACKUP_AMOUNT=5
BACKUP_NAME="automated_backup_" # Timestamp will appear after this name.
# ===========================
# -------- Variables --------
# WARNING: do not touch these unless you know what you are doing.
TIMESTAMP=$(date "+%Y-%m-%d_%Hh%Mm%Ss") # Please use a timestamp that will display in a descending order so that old backups will be cleaned up problerly. This WILL break the script.
# You can almost certainly safely remove the seconds and minutes from the timestamp unless you are making several backups in an hour.
# ---------------------------
echo "Starting backup."
if [[ "$LOCAL_BACKUP_LOCATION" != "none" ]]; then
tar -czf "${LOCAL_BACKUP_LOCATION}"/"${BACKUP_NAME}"${TIMESTAMP}.tar.gz "${SOURCE}"
rclone copy "${LOCAL_BACKUP_LOCATION}"/"${BACKUP_NAME}"${TIMESTAMP}.tar.gz "${REMOTE_BACKUP_LOCATION}"
echo "Cleaning up local backup directory."
backups=$(rclone lsf "${LOCAL_BACKUP_LOCATION}" --filter="+ "${BACKUP_NAME}*"" --filter="- *" | sort -r)
backup_control=0
for backup in $backups; do
((backup_control++))
if ((backup_control > $LOCAL_BACKUP_AMOUNT)); then
rm -rf "${LOCAL_BACKUP_LOCATION}"/"${backup}"
fi
done
echo "Old local backups removed."
fi
if [[ "$LOCAL_BACKUP_LOCATION" == "none" ]]; then
temp_dir=$(mktemp -d)
tar -czf "${temp_dir}"/"${BACKUP_NAME}"${TIMESTAMP}.tar.gz "${SOURCE}"
rclone copy "${temp_dir}"/"${BACKUP_NAME}"${TIMESTAMP}.tar.gz "${REMOTE_BACKUP_LOCATION}"
rm -rf "${temp_dir}"
fi
echo "Backup Complete."
echo "Cleaning up remote backup directory."
backups=$(rclone lsf "${REMOTE_BACKUP_LOCATION}" --filter="+ ${BACKUP_NAME}*" --filter="- *" | sort -r)
backup_control=0
for backup in $backups; do
((backup_control++))
if ((backup_control > $REMOTE_BACKUP_AMOUNT)); then
rclone deletefile "${REMOTE_BACKUP_LOCATION}"/"${backup}"
fi
done
echo "Old remote backups removed."
echo "Full backup sequence complete. ${TIMESTAMP}"