WESERVER / KNOWLEDGE BASE

How to back up a VDS (automatic backups with rsync, restic and cron)

To back up a VDS, copy database dumps, site files and /etc settings to a place outside the server: use rsync for a single copy and restic for encrypted, versioned backups, run the job from cron and test restores regularly.

Short answer

To back up a VDS, first decide what needs saving (database dumps, site files, settings under /etc), then copy that data somewhere outside the server. rsync is enough for a single up-to-date copy; restic gives you encrypted backups you can roll back to an earlier day. Put the steps in a script and run it from cron every night. Do not rely on a backup you have never restored.

The commands are for Ubuntu and Debian; differences on AlmaLinux and Rocky Linux are noted in the relevant step. In the examples the server being backed up is 203.0.113.10 and the backup server is 203.0.113.20. Commands run as root.

What should you back up on a VDS?

You do not need to copy the whole server. The operating system and packages can be reinstalled; what cannot be recovered once lost is your data:

  • Databases: MySQL, MariaDB or PostgreSQL data. Copying the /var/lib/mysql directory of a running database as plain files gives an inconsistent backup, so take a dump first.
  • Site and application files: /var/www, project directories under /home and files uploaded by users.
  • Settings: /etc (Nginx, PHP, cron and SSH configuration), /root and the applications’ .env files. These are what you look for first when rebuilding a server to match the old one.
  • If you use Docker, the compose files and volumes. Volumes live under /var/lib/docker/volumes; if one holds a database, dump it separately.
  • What you can leave out: /proc, /sys, /dev, /run, /tmp, the package cache, folders that can be downloaded again such as node_modules and vendor, and application caches.

Where should backups be stored? Is a snapshot a backup?

A backup has to live somewhere independent of the server it protects. A backup on the same disk disappears together with the data when the disk fails, when the server is deleted or when an intruder encrypts the files. The common rule is 3-2-1: three copies of the data, on two different media, with one copy in another location.

Typical targets are a second VDS, a NAS in the office or S3-compatible object storage. With a second server, it is safer to have the backup server pull the data from the main server: if the main server is compromised, the attacker does not get a key that can delete the backups. A second server at the same provider protects against disk failure and accidental deletion, but it is no substitute for a copy in another location. WeServer Turkey VDS plans range from 20 GB to 200 GB SSD; give the backup server a disk several times larger than the data you back up.

A provider snapshot is an image of the server’s disk at a point in time. It works well as a rollback point before a major upgrade, but it sits on the same infrastructure and pulling a single file out of it is awkward. Under Article 15 of the WeServer Terms, customer data is not backed up unless a backup service is expressly stated in the order, and the customer is expected to keep at least one current backup in an environment independent of WE1TOWN infrastructure. Ask the support team whether a backup or snapshot option can be added to your plan.

How do you back up a database? (mysqldump and pg_dump)

Turn the database into a dump first. If you write dumps to /root/db, the later rsync or restic step picks them up:

  • Password file: put user=root and password=... under a [client] heading in /root/.my.cnf, then run chmod 600 /root/.my.cnf so only root can read it. The password stays out of the command line and the cron log.
  • MySQL and MariaDB: mkdir -p /root/db && mysqldump --single-transaction --routines --triggers --all-databases | gzip > /root/db/mysql.sql.gz. --single-transaction takes a consistent snapshot of InnoDB tables without locking them, so you can dump while the site is running. If mysqldump is missing on newer MariaDB releases, use mariadb-dump with the same options.
  • PostgreSQL: sudo -u postgres pg_dump -Fc database_name > /root/db/database_name.dump. -Fc is the compressed custom format and is restored with pg_restore. To get roles and all databases in one file, use sudo -u postgres pg_dumpall | gzip > /root/db/pg_all.sql.gz.
  • Check: gunzip -t /root/db/mysql.sql.gz should report no error, and the output of zcat /root/db/mysql.sql.gz | tail -1 should start with “-- Dump completed”. A dump that stopped halfway does not have this line.

How do you back up to another server with rsync?

rsync only transfers files that changed, so runs after the first copy are short. It works over SSH, so the target needs no extra service.

  • 1. Create a separate SSH key for backups: ssh-keygen -t ed25519 -f /root/.ssh/backup_ed25519 -N "". The key has no passphrase because cron cannot answer prompts; use it for this job only. General SSH key setup is covered in “How to secure a new VDS: first security steps”.
  • 2. Create a backup user on the target server (adduser backup), then send the key from the main server: ssh-copy-id -i /root/.ssh/backup_ed25519.pub [email protected]. Connect once by hand with ssh -i /root/.ssh/backup_ed25519 [email protected] and accept the host fingerprint, otherwise the cron job will hang on that prompt.
  • 3. Copy: rsync -a --delete -e "ssh -i /root/.ssh/backup_ed25519" /etc /var/www /root [email protected]:/home/backup/server1/. -a keeps permissions, timestamps and symlinks, and --delete removes files on the target that were deleted at the source.
  • 4. Try it first: add --dry-run and rsync lists what it would transfer and delete without changing anything.

What are the limits of an rsync backup?

With --delete, the target becomes a mirror of the source. Files deleted by mistake or encrypted by ransomware on the server are copied into the backup the next night. To go back to yesterday or last week, you need versioned backups.

Because a non-root backup user writes the target, files there are owned by that user. After a restore you have to fix ownership (for example chown -R www-data:www-data /var/www). If owners and permissions must be kept exactly, use restic, which stores them with every snapshot.

How do you make encrypted, versioned backups with restic?

restic records a snapshot on every run, does not write unchanged data twice and encrypts everything in the repository with your password. The repository can sit on an SFTP server, a local disk or S3-compatible storage.

  • Install: apt install restic. On AlmaLinux and Rocky Linux run dnf install epel-release first, then dnf install restic.
  • Password: write the repository password to /root/.restic-password and chmod 600 it. If you lose the password the backups cannot be opened, so keep a copy in your password manager as well.
  • Create the repository: after export RESTIC_REPOSITORY=sftp:[email protected]:/home/backup/restic and export RESTIC_PASSWORD_FILE=/root/.restic-password, run restic init. The SFTP connection uses your SSH settings; point it at the backup key in /root/.ssh/config with Host 203.0.113.20 and IdentityFile /root/.ssh/backup_ed25519.
  • Back up: restic backup /etc /var/www /root --exclude-caches --exclude=node_modules. --exclude-caches skips cache directories that contain a CACHEDIR.TAG file.
  • Stream the database without writing it to disk: mysqldump --single-transaction --routines --triggers --all-databases | restic backup --stdin --stdin-filename mysql.sql. This helps when the disk is tight.
  • Remove old snapshots: restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune keeps one snapshot for each of the last 7 days, 4 weeks and 6 months and deletes the rest.
  • Verify: restic snapshots lists the stored snapshots, and restic check confirms the repository is intact.

How do you automate backups with cron?

Put the steps in a script at /usr/local/bin/backup.sh and let cron run it every night:

  • 1. Start the script with #!/bin/bash and set -euo pipefail. With pipefail, a failing mysqldump no longer looks successful inside a pipeline, and the script stops.
  • 2. Define the environment in the script: export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin, followed by the RESTIC_REPOSITORY and RESTIC_PASSWORD_FILE lines. cron does not see the environment variables of your login session.
  • 3. Write the job in order: database dump, restic backup, restic forget --prune. A line such as echo "$(date) mysql dump" before each step shows in the log where the script stopped.
  • 4. Make the script root-only: chmod 700 /usr/local/bin/backup.sh.
  • 5. Open root’s crontab with crontab -e and add: 30 3 * * * flock -n /run/backup.lock /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1. The job runs at 03:30 every night; flock -n stops a new run from starting before the previous one finishes, and output and errors go to /var/log/backup.log.

How do you know the backup is running?

The time follows the server’s time zone; check it with timedatectl. Instead of opening the log every day, end the script with a curl command that calls a monitoring URL. If the script finishes, the request goes out; if set -e stops it halfway, the request never arrives and the monitoring service alerts you.

/var/log/backup.log grows over time. Add a weekly rotate-and-compress rule in /etc/logrotate.d/backup. If the disk fills up, backups cannot be written either; how to keep an eye on disk usage is covered in “How to fix a full disk on a Linux server (df, du and log cleanup)”.

How do you restore from a backup and test it?

Do not open the backup for the first time on the day the server dies. Test a restore once a month, ideally on a separate test server:

  • restic: restic restore latest --target /tmp/restore unpacks the latest snapshot under /tmp/restore. Add --include /etc/nginx for a single directory; for an older day, use the ID from restic snapshots instead of latest.
  • MySQL dump taken with --stdin: restic dump latest /mysql.sql | mysql. For a gzipped dump: zcat /root/db/mysql.sql.gz | mysql.
  • PostgreSQL: sudo -u postgres pg_restore -d database_name < /root/db/database_name.dump. If the database does not exist yet, create it first with sudo -u postgres createdb database_name.
  • rsync copy: reverse the direction, rsync -a -e "ssh -i /root/.ssh/backup_ed25519" [email protected]:/home/backup/server1/www/ /var/www/, then fix file ownership.
  • Check: does the site load, are the latest orders or posts in the database, are the config files in place? Also note how many minutes the restore took. That figure is how long the site stays down after a failure.

Which backup errors come up, and how do you fix them?

  • The script works by hand but not from cron. Cause: cron has a short PATH and none of your session’s environment variables. Fix: set PATH and the RESTIC_ variables in the script, and look for “command not found” in /var/log/backup.log.
  • Using date +%F in a crontab line breaks the job. Cause: in crontab, % means end of line. Fix: write \% or generate the date inside the script.
  • rsync run from cron says “Host key verification failed”. Cause: the target server’s fingerprint is not in root’s known_hosts file. Fix: connect once by hand as root with the same key and accept the fingerprint.
  • mysqldump says “Access denied ... when using LOCK TABLES”. Cause: the user lacks the LOCK TABLES privilege and the dump runs without --single-transaction. Fix: add --single-transaction for InnoDB tables or grant the user LOCK TABLES.
  • restic says “Is there a repository at the following location?”. Cause: the repository was never created or RESTIC_REPOSITORY is wrong. Fix: check the address and run restic init if the repository does not exist.
  • restic says “repository is already locked”. Cause: an earlier run stopped halfway and left its lock behind. Fix: confirm no other restic process is running with ps aux | grep restic, then run restic unlock.
  • The server’s disk fills up after backups. Cause: dumps and archives pile up on the server. Fix: stream the dump to restic with --stdin, or overwrite the same file every night.

Frequently asked questions

How often should you back up a VDS? It depends on how much data you can afford to lose. A daily backup means losing up to a day of data in the worst case. For a site that takes orders or sign-ups, a common setup is an hourly database dump and a daily file backup.

Does WeServer back up my VDS? Under Article 15 of the Terms, customer data is not backed up unless a backup service is expressly stated in the order. Keep your own backup in an independent environment, and ask the support team what options can be added to your plan.

Is a backup in another folder on the same server enough? It helps you recover a file deleted by mistake. If the disk fails, the server is deleted or someone breaks in, that copy is gone too.

rsync or restic? rsync if a single current copy with direct file access is enough. restic if you want to go back to earlier days, encrypt the backup or send it to S3-compatible storage. You can also use both.

How do you back up a Windows Server VDS? Add the Windows Server Backup feature from Server Manager; wbadmin start backup can back up folders or the system state to another disk or a network share. The Windows build of restic works with the same commands, and Task Scheduler takes the place of cron.

Browse related productsAsk on WhatsApp← Back to category

RELATED

Related articles

ALL IN WE1

Let’s build your infrastructure together.

Share your requirements and we will help you choose the right resources and server type.

Chat on WhatsApp