Project Backup & Sync Script

A simple script that mirrors your whole project to a backup folder, so you can snapshot your code before a risky change and copy it back if anything breaks. You don't set it up by hand. Your AI coding tool does it for you.

Let your AI tool set it up

Open your project in Cursor, Claude Code, or whatever you build with, paste this in, and let it do the work. It downloads the script, points it at your project, makes it runnable, and proves it works.

Set up a backup script for this project so I can snapshot my code before I change anything and copy it back if a change breaks something.

1. Download this script into the project root:
   curl -o project_backup.sh https://www.thelazydeveloper.org/scripts/project_backup.sh
2. Read it, then set SOURCE_DIR to this project's folder and TARGET_DIR to a backup folder OUTSIDE this project, somewhere on my computer. Add excludes for node_modules, .git, dist, .next, build and log files so the backups stay small.
3. Make it executable and run it once to prove it works. Show me the output.
4. Tell me, in one or two lines each: the single command I run to make a new backup, and exactly how to copy a backup back if a change goes wrong.

If you can't download the file, say so and I'll paste the script in myself.

That's the whole setup. When it finishes, you'll have one command to run whenever you want a fresh backup. Run it before you start fixing anything.

The script, for reference

You don't need to read this. It's here if you or your AI tool want the full content of project_backup.sh.

Download project_backup.sh
#!/bin/bash

# === project_backup.sh ===
#
# Purpose:
#   Synchronizes all files and directories from a SOURCE_DIR to a TARGET_DIR.
#   This script is designed to create a mirror of the source on the target.
#   It will overwrite files in the target if the source has newer versions,
#   and it will delete files from the target if they are no longer in the source.
#
# How it works:
#   1. Defines Source and Target directories.
#   2. Creates the Target directory if it doesn't already exist.
#   3. Uses \`rsync\` with the following key options:
#      - \`-a\` (archive): Preserves permissions, ownership, timestamps, etc.
#      - \`-v\` (verbose): Provides more output about what is happening.
#      - \`--itemize-changes\`: Outputs a detailed list of changes.
#      - \`--delete\`: Deletes files from the TARGET_DIR that are not in the SOURCE_DIR.
#   4. Captures the \`rsync\` output into a TEMPORARY log file (created by \`mktemp\`).
#      This log file is used to generate a report of deleted files and is then
#      REMOVED at the end of the script. It is NOT stored permanently.
#   5. Reports on:
#      - Files deleted from the target directory during the sync.
#      - Total raw counts of directories and files in both source and target.
#
# Exclusions:
#   Currently, NO files or directories are excluded. Everything in the SOURCE_DIR
#   will be synchronized to the TARGET_DIR.
#
# Log File:
#   A temporary log file is created by \`mktemp\` during the script's execution
#   to store the output of the \`rsync\` command. This file is automatically
#   deleted after the synchronization report is generated. It is not persisted.
#   If \`rsync\` fails, the contents of this temporary log are printed to the console
#   to aid in debugging before it's deleted.
#
#   To enable, run chmod +x project_backup.sh

# Set source and target directories
TARGET_DIR="/YOUR_TARGET_DESTINATION_HERE/"
SOURCE_DIR="/YOUR_SOURCE_DIRECTORY_HERE/"

# Colors for output
GREEN='\\033[0;32m'
RED='\\033[0;31m'
YELLOW='\\033[0;33m'
NC='\\033[0m' # No Color

echo -e "\${GREEN}Starting synchronization of projects at \$(date)\${NC}"
echo -e "\${YELLOW}Source: \${SOURCE_DIR}\${NC}"
echo -e "\${YELLOW}Target: \${TARGET_DIR}\${NC}"

# Create target directory if it doesn't exist
mkdir -p "\$TARGET_DIR"

# Temporary file for rsync itemized output
RSYNC_LOG_FILE=\$(mktemp)

# Synchronize files, itemize changes
echo -e "\n\${YELLOW}Synchronizing files...\${NC}"
if rsync -av --itemize-changes --delete \\
    "\$SOURCE_DIR/" \\
    "\$TARGET_DIR/" > "\$RSYNC_LOG_FILE"; then
    SYNC_SUCCESS=true
    echo -e "\${GREEN}Project synchronization successful\${NC}"
else
    SYNC_SUCCESS=false
    echo -e "\${RED}Project synchronization failed. Check rsync output below:\${NC}"
    cat "\$RSYNC_LOG_FILE"
fi

echo -e "\n\${YELLOW}--- Synchronization Report ---\${NC}"
DELETED_COUNT=0
if [ "\$SYNC_SUCCESS" = true ]; then
    echo -e "\${YELLOW}Files deleted from target (\${TARGET_DIR}):\${NC}"
    grep "^\\*deleting" "\$RSYNC_LOG_FILE" | while IFS= read -r line; do
        filename="\${line:11}"
        echo -e "\${RED}- \${filename}\${NC}"
        DELETED_COUNT=\$((\$DELETED_COUNT + 1))
    done
    if [ "\$DELETED_COUNT" -eq 0 ]; then
        echo "No files were deleted from target."
    fi
else
    echo "Synchronization failed, so deletion report is skipped."
fi

# Clean up rsync log file
rm -f "\$RSYNC_LOG_FILE"

echo -e "\n\${YELLOW}--- Directory and File Counts (Raw) ---\${NC}"
SOURCE_DIR_ACTUAL_COUNT=\$(find "\$SOURCE_DIR" -type d 2>/dev/null | wc -l)
SOURCE_FILE_ACTUAL_COUNT=\$(find "\$SOURCE_DIR" -type f 2>/dev/null | wc -l)
echo -e "\${YELLOW}Source Directory (\${SOURCE_DIR}):\${NC}"
echo -e "  Total Directories: \${SOURCE_DIR_ACTUAL_COUNT}"
echo -e "  Total Files:     \${SOURCE_FILE_ACTUAL_COUNT}"

TARGET_DIR_ACTUAL_COUNT=\$(find "\$TARGET_DIR" -type d 2>/dev/null | wc -l)
TARGET_FILE_ACTUAL_COUNT=\$(find "\$TARGET_DIR" -type f 2>/dev/null | wc -l)
echo -e "\n\${YELLOW}Target Directory (\${TARGET_DIR}):\${NC}"
echo -e "  Total Directories: \${TARGET_DIR_ACTUAL_COUNT}"
echo -e "  Total Files:     \${TARGET_FILE_ACTUAL_COUNT}"

echo -e "\n\${GREEN}Synchronization script finished at \$(date)\${NC}"
if [ "\$SYNC_SUCCESS" = true ]; then
    echo -e "\${YELLOW}Target directory \${TARGET_DIR} has been synchronized with \${SOURCE_DIR}.\${NC}"
else
    echo -e "\${RED}Synchronization incomplete due to errors.\${NC}"
fi