DEV Community

Arthur
Arthur

Posted on

I Built a Simple File Backup System With Python for My VPS

Hello, I’m Arthur. One thing I learned after working with servers is that having a backup is not enough.

You also need to know where the backup is, when it was created, and whether you can actually restore it.

For a small VPS project, I wanted something simpler than a full backup platform, so I built a small Python script that creates compressed backups automatically.

What the Script Does

The script will:

  • Select a folder to back up
  • Create a .tar.gz archive
  • Add the current date and time to the filename
  • Store the backup in another directory
  • Remove old backups after a certain number of days

Here is the basic version:

import tarfile
from pathlib import Path
from datetime import datetime, timedelta

SOURCE = Path("/var/www/mywebsite")
BACKUP_DIR = Path("/home/backups")
KEEP_DAYS = 7

BACKUP_DIR.mkdir(parents=True, exist_ok=True)

timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M")
backup_file = BACKUP_DIR / f"website_{timestamp}.tar.gz"

with tarfile.open(backup_file, "w:gz") as archive:
    archive.add(SOURCE, arcname=SOURCE.name)

print(f"Backup created: {backup_file}")
Enter fullscreen mode Exit fullscreen mode

Now every backup gets a unique name, such as:

website_2026-10-07_19-30.tar.gz
Enter fullscreen mode Exit fullscreen mode

That makes it much easier to identify when a backup was created.

Automatically Remove Old Backups

Keeping every backup forever can eventually fill your disk.

We can remove files older than seven days:

now = datetime.now()

for file in BACKUP_DIR.glob("*.tar.gz"):
    age = datetime.fromtimestamp(file.stat().st_mtime)

    if now - age > timedelta(days=KEEP_DAYS):
        file.unlink()
        print(f"Deleted old backup: {file}")
Enter fullscreen mode Exit fullscreen mode

Now the backup directory won't continue growing forever.

Don't Keep the Only Backup on the Same VPS

This is probably the most important part.

If your VPS has a disk failure and your backup is stored on that same disk, you may lose both.

For anything important, I would keep another copy somewhere else, such as object storage or another server.

A VPS is useful because you get control over the environment, but that also means you need to think about your own backup strategy.

When comparing VPS providers, I would look at storage, backup options, disk type, network, and server location rather than focusing only on CPU and RAM. For example, you can compare these factors with HelloServer VPS hosting and other providers.

Automating the Backup

Linux has a simple tool called cron that can run the script automatically.

Open the cron configuration:

crontab -e
Enter fullscreen mode Exit fullscreen mode

Then add:

0 3 * * * /usr/bin/python3 /home/scripts/backup.py
Enter fullscreen mode Exit fullscreen mode

This runs the backup every day at 3:00 AM.

You can change the time depending on when your server has the least activity.

Always Test Your Backups

A backup that has never been restored is only a theory.

Every once in a while, I would create a test directory and extract one of the backup files:

mkdir backup-test
tar -xzf website_2026-10-07_19-30.tar.gz -C backup-test
Enter fullscreen mode Exit fullscreen mode

Then check whether the important files are actually there.

This simple step can save a lot of trouble later.

Final Thought

You don't always need a complicated backup system for a small project.

A simple automated script can already give you a good starting point.

But the important rule is:

Backup → Store another copy → Test the restore.

That is much safer than simply assuming your backup will work when you need it.

Top comments (0)