Tag: Linux

  • How to Back Up an Ubuntu Server: rsync, Borg, Restic, and Snapshots

    How to Back Up an Ubuntu Server: rsync, Borg, Restic, and Snapshots

    Ubuntu server backup process copying files and databases to storage

    Why a Proper Ubuntu Backup Strategy Matters

    Hardware failures, botched package upgrades, accidental rm commands, and ransomware can wipe out a production host in seconds. Whether you run Ubuntu 24.04 LTS or Ubuntu 26.04 LTS, a tested Ubuntu backup plan lets you restore configurations, files, and databases cleanly instead of rebuilding from memory.

    This guide covers the 3-2-1-1-0 rule, what to include and exclude, practical commands for rsync, tar, BorgBackup, and Restic, how Timeshift and Déjà Dup fit in, consistent database dumps, systemd timers, ransomware-proof offsite storage, and restore testing.

    Key Facts: Ubuntu Server Backup at a Glance

    • LTS versions covered: Ubuntu 24.04 LTS (Noble Numbat) and Ubuntu 26.04 LTS (Resolute Raccoon, released April 2026 and supported until April 2031).
    • Strategy: 3-2-1-1-0 rule (3 copies, 2 media types, 1 offsite, 1 immutable/offline, 0 unverified restores).
    • Primary tools: BorgBackup (1.2.8 in 24.04 / 1.4.4 in 26.04) or Restic (0.16.4 in 24.04 / 0.18.1 in 26.04) for encrypted deduplication; rsync and tar for mirrors and archives.
    • Desktop tools note: Déjà Dup is for desktop user files; Timeshift creates local OS rollback snapshots (skipping /home by default) and is not an offsite backup.
    • Databases: dump MySQL/MariaDB or PostgreSQL logically before copying files.
    • Automation: use systemd timers for journalctl logs and missed-run catch-up.

    Follow the 3-2-1 (and 3-2-1-1-0) Backup Rule

    CISA’s backup guidance recommends keeping at least 3 copies of data on 2 different storage media, with 1 copy offsite. To defend against ransomware and compromised admin keys, extend this to 3-2-1-1-0: add 1 immutable or offline copy that the live server cannot delete, and maintain 0 errors via automated checks and restore drills.

    Ubuntu backup architecture diagram showing the 3-2-1-1-0 rule across live Ubuntu 24.04 and 26.04 LTS server, local encrypted Borg or Restic repo, immutable offsite storage, and restore verification
    The 3-2-1-1-0 Ubuntu backup architecture balancing fast local recovery and immutable offsite protection.

    What to Back Up on an Ubuntu Server

    Skip virtual kernel filesystems and focus on configuration, application state, and recovery manifests:

    • Configs & certs: /etc/ (Nginx, Apache, SSH, UFW/nftables, systemd units in /etc/systemd/system/, and /etc/letsencrypt/).
    • App data & users: /var/www/, /srv/, container volumes, and /home/.
    • Database dumps: scheduled exports in /var/backups/db/.
    • Package list: run apt-mark showmanual > /var/backups/apt-manual.txt so you can reinstall software on a clean Ubuntu image.

    For full-filesystem mirrors, exclude /dev/*, /proc/*, /sys/*, /tmp/*, /run/*, /mnt/*, /media/*, /lost+found, and /var/cache/*.

    Choosing the Right Ubuntu Backup Tool

    Match the tool to your recovery goal: quick directory mirrors, portable archives, or encrypted deduplicated offsite repositories.

    Ubuntu backup tools comparison matrix for BorgBackup, Restic, rsync, tar, and Timeshift snapshots showing encryption, deduplication, offsite support, and best server use cases
    Comparison of core Ubuntu server backup tools across encryption, deduplication, and offsite support.

    1. Incremental Mirroring with rsync

    rsync mirrors files quickly while preserving permissions, ACLs, and extended attributes. Store exclusions in a file so scripts work identically under cron, systemd, and interactive shells:

    sudo rsync -aAXHv --delete --exclude-from=/etc/backup-exclude.txt / /mnt/backup/ubuntu-mirror/
    sudo rsync -aAXHz --delete /etc /var/www /var/backups/db backup@198.51.100.10:/srv/backups/web01/

    Remember that /etc/ copies directory contents whereas /etc copies the folder itself, and always dry-run (-n) before enabling --delete. For multi-day hardlink snapshots over rsync, see Ubuntu’s rsnapshot guide. Note that rsync encrypts only in transit over SSH, not at rest.

    2. Portable Archives with tar

    tar is ideal for quick snapshots of /etc before config changes. Keep archives outside web roots and encrypt with gpg --symmetric --cipher-algo AES256 before uploading offsite:

    sudo tar --acls --xattrs -czpf /var/backups/etc-$(date +%F).tar.gz /etc
    tar -tzvf /var/backups/etc-$(date +%F).tar.gz | head

    3. Encrypted SSH Backups with BorgBackup

    BorgBackup provides chunk-level deduplication, compression, and client-side encryption. Ubuntu 24.04 ships borgbackup 1.2.8 and Ubuntu 26.04 ships 1.4.4 (26.04 also includes borgbackup2 as a 2.0.0b20 beta, so use stable borgbackup 1.x in production):

    sudo apt update && sudo apt install -y borgbackup
    export BORG_PASSPHRASE="strong-generated-passphrase"
    
    borg init --encryption=repokey-blake2 ssh://borg@backup.example.com:22/srv/borg/web01
    borg key export ssh://borg@backup.example.com:22/srv/borg/web01 /root/borg-key.txt
    
    borg create --stats --compression zstd,6 \
      ssh://borg@backup.example.com:22/srv/borg/web01::"{hostname}-{now:%Y-%m-%d}" \
      /etc /var/www /home /var/backups/db
    
    borg prune --list --keep-daily=7 --keep-weekly=4 --keep-monthly=6 \
      ssh://borg@backup.example.com:22/srv/borg/web01
    borg compact ssh://borg@backup.example.com:22/srv/borg/web01

    4. Encrypted S3 & Cloud Backups with Restic

    Restic (0.16.4 in Ubuntu 24.04; 0.18.1 in Ubuntu 26.04) encrypts and deduplicates data natively to S3-compatible buckets, Backblaze B2, SFTP, or rclone:

    sudo apt update && sudo apt install -y restic
    export RESTIC_REPOSITORY="s3:s3.us-east-1.amazonaws.com/my-ubuntu-backups/web01"
    export RESTIC_PASSWORD_FILE="/root/.restic-pass"
    
    restic init
    restic backup /etc /var/www /home /var/backups/db
    restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune

    5. Timeshift, Déjà Dup, and Cloud/LVM Snapshots

    Déjà Dup targets GNOME desktop folders, not headless servers. Timeshift takes local Rsync or Btrfs system snapshots (also usable via CLI), but excludes /home and /root by default and lacks encrypted offsite storage. Similarly, VPS, LVM, and ZFS snapshots help roll back botched updates quickly, but they live on the same host or cloud account and do not replace independent offsite backups.

    Backing Up MySQL, MariaDB, and PostgreSQL Safely

    Copying /var/lib/mysql or /var/lib/postgresql on a live server risks inconsistent tables. Export logical dumps into a root-only directory right before your file backup runs:

    sudo install -d -m 0700 /var/backups/db
    
    # MySQL (use mariadb-dump on MariaDB)
    mysqldump --single-transaction --routines --triggers --all-databases \
      | gzip > /var/backups/db/mysql-all-$(date +%F).sql.gz
    
    # PostgreSQL
    sudo -u postgres pg_dump -Fc mydb > /var/backups/db/mydb-$(date +%F).dump
    sudo -u postgres pg_dumpall | gzip > /var/backups/db/pg-all-$(date +%F).sql.gz

    Running WordPress on Ubuntu? Pair these database dumps with our WordPress backup guide.

    Automating Backups with systemd Timers (and Cron)

    systemd timers beat bare cron jobs on Ubuntu 24.04 and 26.04 because output goes straight to journalctl and Persistent=true catches missed runs after reboots. Put your backup script at /usr/local/sbin/run-ubuntu-backup.sh (chmod 700) and create a unit pair:

    # /etc/systemd/system/ubuntu-backup.service
    [Unit]
    Description=Daily Ubuntu Server Backup
    After=network-online.target
    Wants=network-online.target
    
    [Service]
    Type=oneshot
    EnvironmentFile=/etc/default/ubuntu-backup
    ExecStart=/usr/local/sbin/run-ubuntu-backup.sh
    
    # /etc/systemd/system/ubuntu-backup.timer
    [Unit]
    Description=Run Ubuntu Server Backup Daily
    
    [Timer]
    OnCalendar=*-*-* 02:30:00
    RandomizedDelaySec=15m
    Persistent=true
    
    [Install]
    WantedBy=timers.target
    sudo systemctl daemon-reload
    sudo systemctl enable --now ubuntu-backup.timer
    journalctl -u ubuntu-backup.service -n 40

    Prefer cron? Call the same script from sudo crontab -e (30 2 * * * /usr/local/sbin/run-ubuntu-backup.sh >/var/log/ubuntu-backup.log 2>&1) rather than putting inline date +%F commands inside crontab.

    Securing Backups Against Ransomware

    CISA’s #StopRansomware Guide warns that intruders actively delete reachable backups. Lock down your backup targets:

    • Make offsite storage append-only or immutable: Restrict Borg SSH keys with command="borg serve --append-only --restrict-to-path /srv/borg/web01",restrict, use Restic’s append-only REST server, or enable S3 Object Lock so a compromised host cannot erase past snapshots.
    • Keep recovery keys off the server: Store Borg key exports and passphrases in a password manager—not only on the host being backed up.
    • Lock down credential files: Keep /etc/default/ubuntu-backup owned by root:root with permissions 0600.
    • Harden the server itself: Follow our Ubuntu server security guide and ransomware prevention guide.

    Testing Your Restores

    Verify repository integrity weekly and test real restores into a temporary directory or staging VM quarterly:

    # Borg integrity check + test extract
    borg check ssh://borg@backup.example.com:22/srv/borg/web01
    mkdir -p /tmp/restore-test && cd /tmp/restore-test
    borg extract ssh://borg@backup.example.com:22/srv/borg/web01::"web01-2026-11-25" etc/nginx
    
    # Restic integrity check + test restore
    restic check --read-data-subset=5%
    restic restore latest --target /tmp/restore-test --include /etc/nginx
    
    # Test database restore
    gunzip < /tmp/restore-test/var/backups/db/mysql-all-2026-11-25.sql.gz | sudo mysql
    sudo -u postgres pg_restore -d mydb_test /tmp/restore-test/var/backups/db/mydb-2026-11-25.dump

    FAQ

    What is the best Ubuntu backup tool for a production server?

    BorgBackup (for SSH servers) and Restic (for S3/cloud object storage) are top choices because both provide client-side encryption, deduplication, and automated pruning.

    Should I use Timeshift or Déjà Dup on Ubuntu Server?

    Déjà Dup is a desktop app. Timeshift works on the CLI for local OS rollbacks, but it excludes /home and /root by default and does not replace encrypted offsite backups.

    Are VPS or cloud snapshots enough?

    No. Snapshots live inside the same cloud account and hypervisor; keep an independent encrypted offsite copy under separate credentials.

    Why use mysqldump or pg_dump instead of copying /var/lib/mysql?

    Copying active database files can capture half-written pages and corrupt tables. Logical dumps produce consistent, restorable snapshots.

    How often should I back up an Ubuntu server?

    Run encrypted incremental backups at least daily, take an extra snapshot before major upgrades, and dump busy databases hourly or ship WAL/binlogs.

    How do I protect Ubuntu backups from ransomware?

    Use append-only SSH keys or S3 Object Lock, keep passphrases off the server, and maintain one offline or air-gapped copy.

    References

    Related Articles

    Last Verified: November 25, 2026. Verified for Ubuntu 24.04 LTS and Ubuntu 26.04 LTS against Ubuntu Server documentation, BorgBackup, Restic, PostgreSQL, systemd, and CISA guidance.

  • How to Secure Your Ubuntu Server: A Beginner-Friendly Guide

    How to Secure Your Ubuntu Server: A Beginner-Friendly Guide

    Ubuntu server secured with firewall and SSH keys

    Setting up an Ubuntu server is an exciting step, whether you’re hosting a website, running applications, or managing a Linux server for your business. But here’s the thing: without proper security measures, your server could be a sitting duck for cyberattacks. In this guide, we’ll walk you through some essential tools and techniques to secure your Ubuntu server, including enabling SSH, configuring firewalls, and using tools like Fail2ban. Let’s dive in!


    Why Securing Your Ubuntu Server Matters

    Imagine this: you’ve just set up your shiny new Linux server hosting environment, and everything is running smoothly. But without security, your server is vulnerable to attacks that could steal sensitive data, disrupt services, or even install malware. Yikes, right? That’s why securing your Ubuntu server setup is non-negotiable.


    1. Fail2ban: Your First Line of Defense

    Fail2ban is like a bouncer for your server. It monitors login attempts and bans IP addresses that fail to authenticate after a certain number of tries. For example, if someone tries to SSH into your server without the correct key or password, Fail2ban steps in and blocks them.

    How to Install Fail2ban on Ubuntu Server

    1. Update your package list:sudo apt update
    2. Install Fail2ban:sudo apt-get install fail2ban

    Configuring Fail2ban

    The default configuration file is located at /etc/fail2ban/jail.conf. To customize it:

    1. Back up the default file:sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
    2. Open the configuration file:sudo nano /etc/fail2ban/jail.local
    3. Adjust these key parameters:
      • ignoreip: Whitelist trusted IPs (e.g., your home IP).
      • bantime: Set how long an IP should be banned (in seconds).
      • findtime: Define the time window for counting failed logins.
      • maxretry: Set the number of failed attempts before banning.

    For example, to whitelist your home IP and ban attackers for 10 minutes:

    ignoreip = 192.168.1.1
    bantime = 600
    maxretry = 5
    

    2. UFW: Simplifying Firewall Management

    UFW (Uncomplicated Firewall) is a user-friendly tool to manage your Ubuntu server’s firewall. It helps you control incoming and outgoing traffic, ensuring only authorized connections get through.

    How to Install and Enable UFW

    1. Check if UFW is already installed:sudo ufw status
    2. If not, install it:sudo apt update sudo apt install ufw

    Configuring UFW for SSH and More

    By default, UFW blocks all incoming traffic. To allow SSH access:

    sudo ufw allow ssh
    

    Want to open another port? For example, if you’re hosting a web server on port 8080:

    sudo ufw allow 8080
    

    Enable UFW to start protecting your server:

    sudo ufw enable
    

    To view your rules:

    sudo ufw status numbered
    

    Pro Tip: Always test your setup after enabling UFW to ensure you haven’t accidentally blocked necessary traffic.


    3. SSH Key Authentication: Ditch Passwords for Good

    Passwords are so last decade. SSH key authentication is a more secure way to log into your Ubuntu server. It uses a pair of cryptographic keys (public and private) to verify your identity.

    How to Enable SSH on Ubuntu Server

    1. Check if SSH is installed:sudo systemctl status ssh
    2. If it’s not running, start it:sudo systemctl start ssh
    3. If SSH isn’t installed, add it:sudo apt-get install openssh-server

    Setting Up SSH Key Authentication

    1. Generate an SSH key pair on your local machine:ssh-keygen -t rsa -b 4096 Press “Enter” to save the key in the default location (~/.ssh/id_rsa) and set a passphrase for extra security.
    2. Copy your public key to the server:ssh-copy-id username@<server_ip>
    3. Disable password authentication for added security:
      Open the SSH configuration file:sudo nano /etc/ssh/sshd_config Find the line PasswordAuthentication and set it to no.
    4. Restart the SSH service:sudo systemctl restart ssh

    Now, try logging in:

    ssh username@<server_ip>
    

    If it works, congrats! You’ve just leveled up your server security.

    4. Don’t Forget Backups: Your Safety Net

    Even with the best security tools in place, things can still go wrong—whether it’s hardware failure, accidental deletions, or a successful cyberattack. That’s why having a solid backup strategy is just as important as setting up firewalls or SSH keys. Backups act as your safety net, ensuring you can quickly restore your system and data if disaster strikes.

    Want to learn how to set up reliable backups for your Ubuntu server? Check out our detailed guide here: How to Back Up Your Ubuntu Server.

    Pro Tip: Automate your backups so you never have to remember to run them manually. Regular, automated backups will save you countless headaches down the road.


    FAQs About Ubuntu Server Security

    Q: Can I use Fail2ban and UFW together?
    Absolutely! They complement each other. Fail2ban handles brute-force attacks, while UFW manages overall traffic rules.

    Q: What happens if I lose my SSH private key?
    If you lose your private key, you won’t be able to log in. Always keep a backup of your private key in a secure location.

    Q: Is SSH key authentication mandatory?
    No, but it’s highly recommended. Passwords are easier to crack, while SSH keys provide a stronger layer of security.


    Wrapping Up

    Securing your Ubuntu server doesn’t have to be overwhelming. By using tools like Fail2ban, UFW, and SSH key authentication, you can protect your Linux server hosting environment from common threats. Whether you’re setting up a personal project or managing a business-critical application, these steps will give you peace of mind.

    Related reading