Automated File Backup Script
A simple bash script that copies any folder to a backup location and stamps it with the current date and time.
No extra software needed — just bash and cp, which are already on every Linux and macOS system.
The Script
Copy this, paste it into a file called backup.sh, and you're 90% done.
#!/bin/bash # Automated File Backup # Copies a folder to /backup with today's timestamp. # Run manually or schedule with cron — works either way. # # USAGE: ./backup.sh # REQUIRES: bash, cp (pre-installed on all Linux/macOS) SOURCE="/home/user/documents" # ← change this to your folder DEST="/backup" # ← change this to your backup location DATE=$(date +%Y-%m-%d_%H-%M) mkdir -p "$DEST" cp -r "$SOURCE" "$DEST/backup_$DATE" echo "✓ Done. Saved to: $DEST/backup_$DATE"
SOURCE is the folder you want to back up. DEST is where backups are saved. DATE grabs the current timestamp. mkdir -p creates the destination folder if it doesn't exist. cp -r copies everything recursively. The last line confirms it worked.
Step-by-Step Setup
Step 1 — Create the file
Open a terminal and run:
nano backup.sh
Paste the script above, then press Ctrl+X → Y → Enter to save.
Step 2 — Edit your paths
Change SOURCE to the folder you want to back up and DEST to where you want backups saved. Examples:
| Variable | Example value | What it means |
|---|---|---|
| SOURCE | /home/alice/projects | The folder being backed up |
| SOURCE | /var/www/html | A web server's files |
| DEST | /backup | A local backup directory |
| DEST | /mnt/external-drive | An external drive mount point |
Step 3 — Make it executable
chmod +x backup.sh
You only need to do this once. It gives the script permission to run.
Step 4 — Run it
./backup.sh
You should see: ✓ Done. Saved to: /backup/backup_2026-05-02_14-30
Schedule It with Cron
Running this manually defeats the purpose. Here's how to make it run automatically every day.
Open your crontab
crontab -e
Add one of these lines
# Run every day at 2am 0 2 * * * /home/user/backup.sh # Run every Sunday at midnight 0 0 * * 0 /home/user/backup.sh # Run every hour 0 * * * * /home/user/backup.sh
Go to crontab.guru to build and test cron time expressions for free. It explains exactly when your job will run in plain English.
Variations
Faster backups with rsync (recommended for large folders)
Swap cp -r for rsync to only copy files that have changed. Much faster on big directories:
#!/bin/bash SOURCE="/home/user/documents" DEST="/backup/latest" rsync -av --delete "$SOURCE/" "$DEST/" echo "✓ Sync complete: $(date)"
Log every backup to a file
Know exactly when each backup ran and whether it succeeded:
#!/bin/bash SOURCE="/home/user/documents" DEST="/backup" DATE=$(date +%Y-%m-%d_%H-%M) LOG="/var/log/mybackup.log" mkdir -p "$DEST" cp -r "$SOURCE" "$DEST/backup_$DATE" && \ echo "[$DATE] SUCCESS: backup_$DATE" >> "$LOG" || \ echo "[$DATE] FAILED" >> "$LOG"
Keep only the last 7 backups
Prevent your backup folder from filling up your disk:
#!/bin/bash SOURCE="/home/user/documents" DEST="/backup" DATE=$(date +%Y-%m-%d_%H-%M) KEEP=7 mkdir -p "$DEST" cp -r "$SOURCE" "$DEST/backup_$DATE" # Delete oldest backups, keep only the last $KEEP ls -dt "$DEST"/backup_* | tail -n +$(( KEEP + 1 )) | xargs rm -rf echo "✓ Backed up. Keeping last $KEEP snapshots."
Common Mistakes
With rsync, rsync -av /source /dest/ copies the folder itself. rsync -av /source/ /dest/ copies the folder's contents. That trailing slash matters. When in doubt, test with --dry-run first.
Cron doesn't load your shell's PATH. If the script works manually but not via cron, add PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin as the first line of your crontab.
A backup on the same physical disk as your source files won't save you from a drive failure. For real protection, back up to an external drive, a second machine, or a cloud location.
Frequently Asked Questions
How do I automatically back up files in Linux?
Use a bash script with cp -r or rsync to copy your folder to a backup location. Append a date variable like DATE=$(date +%Y-%m-%d_%H-%M) to the destination path so each backup gets a unique name. Then add the script to cron to run it automatically on a schedule.
How do I schedule a backup to run every day?
Open cron with crontab -e and add: 0 2 * * * /home/user/backup.sh — this runs at 2am daily. Use crontab.guru to build any cron expression you need.
What's the difference between cp and rsync for backups?
cp copies everything every time — simple but slow on large folders. rsync only transfers files that have changed, making it significantly faster for repeated backups. For daily backups of large directories, rsync is the right choice. For small folders or one-time copies, cp is fine.
How do I verify my backup script is working?
Add set -x right after #!/bin/bash. This enables debug mode and prints every command as it runs, so you can see exactly what happened. Remove it once you're satisfied the script works correctly.