Created
November 1, 2021 15:50
-
-
Save chtzvt/01af7fd5525071d9f4b4e1b6371a058f to your computer and use it in GitHub Desktop.
Rate-limited backup to BackBlaze B2 with IPTables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# You can adjust the maximum throuhput limits (in mbits) here | |
BW_MAX_RATE=500mbit | |
BW_BURST_RATE=560mbit | |
IFACE=ens3 | |
# Local/remote paths | |
B2_SOURCE_PATH="/local/path/to/backupdir" | |
B2_DEST_BUCKET="b2://your-bucket-name" | |
B2_DEST_BUCKET_PATH="/your-bucket-directory/" | |
# Configurable retention policies | |
B2_THREADS=4 | |
B2_KEEPDAYS=30 | |
B2_PID=`pidof -x b2` | |
# TODO: Control traffic from b2 proccess by putting it into a cgroup | |
if [ -z $B2_PID ] | |
then | |
echo "[`date`]: Configuring network interface: iface($IFACE)..." | |
echo "[`date`]: Removing any preexisting traffic shaping rules..." | |
/sbin/tc qdisc del dev $IFACE root | |
echo "[`date`]: Removing any preexisting mangle rules..." | |
/usr/sbin/iptables -D OUTPUT -t mangle -p tcp --sport 443 -j MARK --set-mark 10 | |
echo " + [`date`]: Enabling queuing..." | |
/sbin/tc qdisc add dev $IFACE root handle 1:0 htb default 10 | |
echo " + [`date`]: Applying traffic limits: iface($IFACE) net_max_rate($BW_MAX_RATE) net_burst_rate($BW_BURST_RATE)" | |
/sbin/tc class add dev $IFACE parent 1:0 classid 1:10 htb rate $BW_MAX_RATE ceil $BW_BURST_RATE prio 0 | |
echo "[`date`]: Assigning queuing rule to qdisc..." | |
/sbin/tc filter add dev $IFACE parent 1:0 prio 0 protocol ip handle 10 fw flowid 1:10 | |
echo " + [`date`]: Installing mangle rule..." | |
/usr/sbin/iptables -A OUTPUT -t mangle -p tcp --sport 443 -j MARK --set-mark 10 | |
echo "[`date`]: Starting b2 sync: threads($B2_THREADS) keepdays($B2_KEEPDAYS)..." | |
cd $B2_SOURCE_PATH | |
/usr/local/bin/b2 sync --threads $B2_THREADS --keepDays $B2_KEEPDAYS --replaceNewer $B2_SOURCE_PATH $B2_DEST_BUCKET$B2_DEST_BUCKET_PATH | |
B2_RETURN_STATUS=$? | |
echo "[`date`]: Sync complete, b2 client exiting with status b2_return_status($B2_RETURN_STATUS)" | |
echo "[`date`]: Deconfiguring network interface: iface($IFACE)..." | |
echo "[`date`]: Removing traffic shaping rules..." | |
/sbin/tc qdisc del dev $IFACE root | |
echo "[`date`]: Removing mangle rules..." | |
/usr/sbin/iptables -D OUTPUT -t mangle -p tcp --sport 443 -j MARK --set-mark 10 | |
exit $B2_RETURN_STATUS | |
fi | |
# Terminate early- last b2 sync job is still running | |
echo "[`date`]: DYING - A b2 sync job is already running!" | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment