Cheatsheet#backup
Linux Backup (Rsync, Rclone, Restic & DB Dumps) Cheatsheet
A complete reference for data backup strategies on Linux, covering file sync (rsync), cloud storage (rclone), encrypted deduplicated backups (restic), system snapshots (timeshift), and database dumps (MySQL, PostgreSQL, MongoDB).
Rsync (Local & SSH File Synchronization)
rsync transfers modified file chunks, preserving file attributes, symlinks, and permissions.
Essential Flags
-a(--archive) : Enables recursive mode, preserves symlinks, permissions, modification times, group, owner.-v(--verbose) : Displays verbose progress details.-z(--compress) : Compresses data during network transfer.-h(--human-readable) : Output numbers in human-readable format.-P(--partial --progress) : Keeps partially transferred files and shows progress bar.--delete: Mirrors directory by removing files in destination not present in source.
Commands
# Sync local directory to backup directory
rsync -avh /path/to/source/ /path/to/backup/
# Push backup to remote server over SSH (custom port 2222)
rsync -avzP -e 'ssh -p 2222' /var/www/ [email protected]:/backups/www/
# Pull backup from remote server to local machine
rsync -avzP [email protected]:/backups/db/ /local/backups/db/
# Exact mirror backup (Deletes extra files on destination)
rsync -avh --delete /data/user/ /mnt/external_drive/user/
# Exclude specific patterns (e.g. node_modules, cache)
rsync -avh --exclude 'node_modules' --exclude '.git' --exclude '*.log' /src/ /dst/
# Dry run (Preview changes without modifying destination)
rsync -avh --delete --dry-run /src/ /dst/Rclone (Cloud Storage Sync & Mount)
rclone syncs files to over 40 cloud storage providers (S3, Google Drive, Backblaze B2, Dropbox, OneDrive).
# Interactive setup for cloud storage remotes
rclone config
# List files in remote bucket/drive
rclone ls remote_gdrive:backup_folder
# Copy files (only adds/updates, does not delete files in remote)
rclone copy /var/backups remote_gdrive:server_backups
# Mirror sync (WARNING: Deletes files in remote if deleted locally)
rclone sync /var/www remote_s3:mybucket/www --progress
# Mount cloud storage locally as a filesystem
rclone mount remote_gdrive: /mnt/gdrive --vfs-cache-mode full --daemonRestic (Encrypted Deduplicated Backups)
restic creates fast, secure, deduplicated snapshot backups stored locally or in cloud storage.
# Initialize restic repository (Local directory or S3 bucket)
restic -r /srv/restic-repo init
# Create a backup snapshot
restic -r /srv/restic-repo --verbose backup /var/www /etc
# List all snapshots in repository
restic -r /srv/restic-repo snapshots
# Restore specific snapshot to a directory
restic -r /srv/restic-repo restore 0a1b2c3d --target /tmp/restore
# Prune & keep retention policy (keep last 7 daily, 4 weekly, 12 monthly)
restic -r /srv/restic-repo forget --keep-daily 7 --keep-weekly 4 --keep-monthly 12 --pruneDatabase Backup Dumps
MySQL / MariaDB
# Dump a single database to compressed SQL file
mysqldump -u root -p'Password' dbname | gzip > /backups/dbname_$(date +%F).sql.gz
# Dump ALL databases
mysqldump -u root -p'Password' --all-databases | gzip > /backups/all_dbs_$(date +%F).sql.gz
# Restore MySQL database from compressed dump
gunzip < /backups/dbname_2026-08-08.sql.gz | mysql -u root -p'Password' dbnamePostgreSQL
# Dump a single PostgreSQL database (custom compressed binary format)
pg_dump -U postgres -F c -b -v -f /backups/pg_dbname_$(date +%F).dump dbname
# Dump ALL databases
pg_dumpall -U postgres | gzip > /backups/pg_all_$(date +%F).sql.gz
# Restore PostgreSQL database from binary dump
pg_restore -U postgres -d dbname -v /backups/pg_dbname_2026-08-08.dumpMongoDB
# Dump MongoDB database to archive file
mongodump --db=my_database --archive=/backups/mongo_$(date +%F).gz --gzip
# Restore MongoDB database
mongorestore --nsInclude="my_database.*" --archive=/backups/mongo_2026-08-08.gz --gzipSystem Tar Archiving & Automation
Archive with Tar & Compression
# Create gzip compressed archive (.tar.gz)
tar -czvf /backups/etc_$(date +%F).tar.gz /etc
# Create zstd compressed archive (.tar.zst) - fast & high compression
tar --zstd -cvf /backups/data.tar.zst /var/dataCron Automated Backup Script
Add to root crontab (sudo crontab -e):
# Run daily backup script at 2:30 AM
30 2 * * * /usr/local/bin/daily_backup.sh >> /var/log/backup.log 2>&1