Project Backup & Sync Script
This guide provides information about the project backup and synchronization script to backup your entire projects folder to another location, how to use it, and its full content for reference.
Script Overview & Usage
Understand what the script does and how to run it.
Purpose
The script synchronizes all files and directories from a specified source directory to a target directory. It creates a mirror of the source, overwriting newer files and deleting files from the target if they are no longer in the source. This is a great way to backup your entire projects folder to another location as an additional backup.
How it Works
It uses the rsync command with options for archiving (preserving file attributes), verbose output, itemizing changes, and deleting extraneous files from the target. A temporary log file is used to report on deleted files and is removed after the script completes. No permanent logs are kept by the script itself.
How to Use
1. Download the script:
Download project_backup.sh2. Important: Open the downloaded project_backup.sh file in your code editor (Cursor, VS Code, etc.) or a text editor and update the following lines to match your desired source and target directories:
TARGET_DIR="/YOUR_TARGET_DESTINATION_HERE/"
SOURCE_DIR="/YOUR_SOURCE_DIRECTORY_HERE/"3. Make it executable (run this in your terminal in the directory where you saved the script):
chmod +x project_backup.sh4. Run the script (from the same directory):
./project_backup.shLog File Information
A temporary log file is created by mktemp during the script's execution to store the output of the rsync command. This file is used to report on deleted items and 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 for debugging before it's deleted.
Automating the Script (Optional)
Set up the script to run on a schedule.
macOS (using cron)
You can use cron, a time-based job scheduler in Unix-like operating systems, to run the script automatically. First, ensure the script is executable (chmod +x /path/to/your/project_backup.sh) and that the paths within the script are absolute or correctly resolvable by the cron environment.
Edit your crontab:
crontab -eThen, add a line to schedule your script. For example, to run it daily at 2:00 AM:
0 2 * * * /path/to/your/project_backup.sh(Replace /path/to/your/project_backup.sh with the actual absolute path to where you saved the script). You might want to redirect the script's output to a log file as well (e.g., >> /path/to/your/backup.log 2>&1 added after the script path).
Windows (using Task Scheduler)
Windows users can use Task Scheduler to automate the script. Since this is a Bash script, you'll typically run it via the Windows Subsystem for Linux (WSL) or Git Bash.
- Open Task Scheduler (search for it in the Start Menu).
- Click "Create Basic Task..." or "Create Task...".
- Give your task a name (e.g., "Project Backup Sync") and description.
- Set up a trigger (e.g., Daily, Weekly, at a specific time).
- For the "Action", select "Start a program".
- In "Program/script":
- If using WSL:
wslor the path to yourwsl.exe. Arguments would be the path to the script within WSL (e.g.,/mnt/c/path/to/your/project_backup.sh). - If using Git Bash: The path to your
bash.exe(usually in the Git installation directory, e.g.,C:\Program Files\Git\bin\bash.exe). Arguments would be the path to your script (e.g.,C:/path/to/your/project_backup.sh).
- If using WSL:
- In "Add arguments (optional)": If not included with the program/script path, provide the full path to your
project_backup.shscript. Ensure paths are correctly formatted for the chosen shell. - In "Start in (optional)": You might need to set this to the directory containing your script, or ensure all paths within the script are absolute.
- Review and finish the task setup. You may need to adjust permissions or ensure the script can run non-interactively.
Testing the task manually is recommended to ensure it runs as expected.
Alternative Backup Approaches
Other ways to keep your projects safe.
Git-Based Backups
Using Git and GitHub (or GitLab, Bitbucket) is one of the best ways to back up your code. Every commit is a snapshot you can return to, and pushing to a remote repository gives you an off-site copy automatically. Make frequent commits with descriptive messages and push regularly.
git add . && git commit -m 'backup: pre-refactor snapshot' && git push origin mainCloud Backup Tips
For an extra layer of protection, consider syncing your projects folder to a cloud service like iCloud, Google Drive, OneDrive, or Dropbox. Many of these services offer free tiers with enough storage for code projects. You can also use an external SSD or NAS for local redundancy. The key principle is the 3-2-1 rule: 3 copies of your data, on 2 different media, with 1 off-site.
Script Content
The full content of project_backup.sh for reference.