VPS swap and backups: set up swap, then back up with tar and rsync
Two small habits save many VPS headaches: swap for memory spikes, and backups for everything else. Commands are for Ubuntu and Debian and may vary on other distributions. Tabi Cloud has not stated that backups or snapshots are included in VPS plans, so make your own and keep a copy off the server.
Check for existing swap
- free -h
- swapon --show
If Swap shows 0, you have none.
Create a swap file
- sudo fallocate -l 2G /swapfile
- sudo chmod 600 /swapfile
- sudo mkswap /swapfile
- sudo swapon /swapfile
- echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
If fallocate fails, use sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 instead. Swap is slower than RAM, so treat it as a safety net, not a replacement. You can lower eagerness with sudo sysctl vm.swappiness=10.
Back up with tar
tar bundles and compresses folders.
- tar -czf backup-$(date +%F).tar.gz /var/www /home/deploy/mybot
- ls -lh to check the size
- tar -tzf backup-DATE.tar.gz | head to peek inside
To restore, run tar -xzf backup-DATE.tar.gz -C /restore-folder.
Copy off the server with rsync
Keeping backups only on the same VPS is risky.
- rsync -avz /var/www/ user@otherhost:/backups/www/
- rsync -avz user@yourvps:/home/deploy/ ./local-backup/ to pull to your own computer
Add --delete only when you want the copy to mirror deletions, and test with --dry-run first.
Databases
- MySQL or MariaDB: mysqldump -u user -p dbname > db.sql
- PostgreSQL: pg_dump dbname > db.sql
Copying live database files is not reliable, so dump instead.
Automate with cron
- crontab -e
- Add 0 3 * * * tar -czf /home/deploy/backups/site-$(date +\%F).tar.gz /var/www
Cron needs the percent sign escaped as shown.
Tips
- Follow the 3-2-1 idea: three copies, two kinds of storage, one off-site
- Test a restore at least once
- Delete old archives so the disk does not fill
Need a hand? Reach us by Discord ticket or email.