A simple bash script that takes a full backup of a Linux server and uploads it to Google Drive using rclone, keeping only the latest backup on the remote (each new upload replaces the previous one).
- Creates a
tar.gzarchive of the whole filesystem (excluding volatile/pseudo paths like/proc,/sys,/dev,/tmp,/run,/mnt,/media,/var/cache, and the local backup working directory). - Uploads the archive to a Google Drive folder via an rclone remote.
- After a successful upload, deletes any older backup files in that remote folder, keeping only the newest one.
- Removes the local archive copy so it doesn't fill up server disk space.
- Logs every run to
/var/log/weekly-backup.log.
tarrcloneinstalled and configured with a remote namedgdrivepointing at your Google Drive (rclone config— see rclone's Google Drive docs).
# Place the script
mkdir -p /root/scripts
cp weekly_backup.sh /root/scripts/weekly_backup.sh
chmod +x /root/scripts/weekly_backup.sh
# Test it manually
/root/scripts/weekly_backup.sh
# Schedule it weekly, e.g. every Sunday at 03:00
(crontab -l 2>/dev/null; echo "0 3 * * 0 /root/scripts/weekly_backup.sh >/dev/null 2>&1") | crontab -Edit the variables at the top of weekly_backup.sh to fit your setup:
| Variable | Default | Description |
|---|---|---|
BACKUP_DIR |
/root/backups |
Local staging directory for the archive |
REMOTE |
gdrive:server-backups |
rclone remote path where backups are uploaded |
LOG_FILE |
/var/log/weekly-backup.log |
Log file path |
- The script only ever touches files inside the
REMOTEfolder on Google Drive — nothing else in your Drive is read, modified, or deleted. tarexit code1(files changed while reading, common on a live system) is treated as non-fatal; any other non-zero exit aborts the backup.