Skip to content

Commit 013e121

Browse files
committed
Update ClickHouse restore script to use SFTP for listing backups
- Replaced SSH command with SFTP to list available ClickHouse backup files, improving compatibility with Hetzner Storage Box's restricted shell. - Enhanced file size reporting by converting bytes to human-readable format (GB, MB, KB). - Updated the output format for better clarity on backup dates and sizes.
1 parent 6d33522 commit 013e121

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

clickhouse/restore-clickhouse.sh

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,25 @@ list_backups() {
3333
echo "Available backups:"
3434
echo "=================="
3535

36-
ssh "$STORAGE_BOX_HOST" "
37-
cd $BACKUP_BASE_DIR 2>/dev/null || { echo 'No backups found'; exit 1; }
38-
ls -1 clickhouse-backup-*.tar 2>/dev/null | sort -r | while read file; do
39-
size=\$(du -sh \"\$file\" 2>/dev/null | cut -f1)
40-
date=\$(echo \"\$file\" | sed 's/clickhouse-backup-//;s/.tar//')
41-
echo \" \$date (\$size)\"
42-
done
43-
" || error "Failed to list backups"
36+
# Use sftp to list files (works with Hetzner Storage Box restricted shell)
37+
echo "ls ${BACKUP_BASE_DIR}/clickhouse-backup-*.tar" | sftp -b - "$STORAGE_BOX_HOST" 2>/dev/null | grep "clickhouse-backup-" | while read -r line; do
38+
# Extract filename from sftp output
39+
filename=$(echo "$line" | awk '{print $NF}')
40+
if [[ "$filename" == clickhouse-backup-*.tar ]]; then
41+
date=$(echo "$filename" | sed 's/clickhouse-backup-//;s/.tar//')
42+
# Get file size
43+
size=$(echo "$line" | awk '{print $5}')
44+
# Convert bytes to human readable
45+
if [ "$size" -gt 1073741824 ]; then
46+
size_hr="$(awk "BEGIN {printf \"%.1f\", $size/1073741824}")GB"
47+
elif [ "$size" -gt 1048576 ]; then
48+
size_hr="$(awk "BEGIN {printf \"%.1f\", $size/1048576}")MB"
49+
else
50+
size_hr="$(awk "BEGIN {printf \"%.1f\", $size/1024}")KB"
51+
fi
52+
echo " $date ($size_hr)"
53+
fi
54+
done | sort -r
4455

4556
echo ""
4657
echo "Usage: $0 YYYY-MM-DD"

0 commit comments

Comments
 (0)