Skip to content

Instantly share code, notes, and snippets.

@fritzlaszlo
Created March 13, 2025 14:10
Show Gist options
  • Save fritzlaszlo/cd3b9188c670d6236e59b7f82e5759b1 to your computer and use it in GitHub Desktop.
Save fritzlaszlo/cd3b9188c670d6236e59b7f82e5759b1 to your computer and use it in GitHub Desktop.
A simple bash script to SSH into your server/webspace, zip Kirby's content folder, download to your machine and unzip it.
#!/bin/bash
# Colors for terminal output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color (reset)
# Configuration variables
SERVER_USER="your_username"
SERVER_HOST="your_server_ip"
SERVER_PORT="22"
SERVER_CONTENT_PATH="/path/to/remote/content"
SERVER_TEMP_PATH="/path/to/remote/temp/directory" # Online neccessary if zip file is not web-accessible
LOCAL_DOWNLOAD_PATH="/path/to/local/download/directory"
LOCAL_CONTENT_PATH="/path/to/local/content/directory"
DOWNLOAD_URL="your.domain.com/"
# Get current date for backup naming
CURRENT_DATE=$(date +"%Y-%m-%d")
TIMESTAMP=$(date +"%Y-%m-%d_%H%M%S")
TIMESTAMPED_ZIP="content_backup_${TIMESTAMP}.zip"
echo -e "${GREEN}Starting content folder download process...${NC}"
# Step 1: SSH into the server, create zip file, and set up for download
ssh -p $SERVER_PORT $SERVER_USER@$SERVER_HOST << EOF
echo -e "${BLUE}Connected to server. Creating zip file...${NC}"
cd $SERVER_CONTENT_PATH/..
zip -r $SERVER_TEMP_PATH/$TIMESTAMPED_ZIP content
# Optional: If you need to move the zip to a web-accessible location for wget
# cp $SERVER_TEMP_PATH/$TIMESTAMPED_ZIP /var/www/html/downloads/
echo -e "${BLUE}Zip file created at $SERVER_TEMP_PATH/$TIMESTAMPED_ZIP${NC}"
EOF
echo -e "${GREEN}SSH session completed. Downloading zip file...${NC}"
# Step 2: Download the zip file using wget
# Note: This assumes the file is accessible via HTTP. You may need to adjust
# the URL or use scp instead if the file isn't web-accessible
wget -O "$LOCAL_DOWNLOAD_PATH/$TIMESTAMPED_ZIP" "$DOWNLOAD_URL/$TIMESTAMPED_ZIP"
# Alternative: Use SCP instead of wget if the file isn't web-accessible
# scp -P $SERVER_PORT $SERVER_USER@$SERVER_HOST:$SERVER_TEMP_PATH/$TIMESTAMPED_ZIP $LOCAL_DOWNLOAD_PATH/
echo -e "🌐 ${GREEN}Download completed. Renaming the current content folder...${NC}"
# Step 3: Rename the local content folder with date suffix (if it exists)
if [ -d "$LOCAL_CONTENT_PATH" ]; then
mv "$LOCAL_CONTENT_PATH" "${LOCAL_CONTENT_PATH}_backup_before_${TIMESTAMPED_ZIP}"
echo -e "πŸ“¦ ${YELLOW}Current content folder renamed to content_backup_before_${TIMESTAMPED_ZIP}${NC}"
else
echo -e "πŸ“¦ ${YELLOW}No existing content folder found. Creating new one.${NC}"
fi
# Step 4: Unzip the downloaded file
echo -e "${GREEN}Unzipping downloaded content...${NC}"
unzip -q "$LOCAL_DOWNLOAD_PATH/$TIMESTAMPED_ZIP" -d "$LOCAL_DOWNLOAD_PATH"
# Clean up the downloaded zip file
rm "$LOCAL_DOWNLOAD_PATH/$TIMESTAMPED_ZIP"
echo -e "🎊${GREEN}Process completed successfully!${NC}"
echo -e "βœ…${GREEN}Your content folder has been updated, and the old version backed up with today's timestamp.${NC}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment