How to Perform Remote Server Backup to Mac with Limited Storage Using SSH (Strategy)

penguins fighting over a huge white apple. textured paint. angsty. gritty, grungy esthetic.

Backing up your remote servers is crucial for data security and disaster recovery. When you’re dealing with limited storage on remote servers and can only use SSH without installing additional utilities, you need a tailored approach. This guide walks you through creating automated remote server backup to Mac, ensuring old backups are removed to conserve space.

penguins fighting over a huge white apple. textured paint. angsty. gritty, grungy esthetic.

Caveat:


Table of Content

  1. Introduction
  2. Use Cases
  3. Requirements
  4. Step-by-Step Guide
  5. Conclusion

Introduction

Backing up data from remote servers is essential, especially when running websites, web apps, or databases. However, when your remote servers have limited storage and you can’t install additional utilities, the task becomes more challenging. This guide provides a strategy to perform a remote server backup to Mac, delete the backup from the remote server after successful transfer, and automate the process.

Use Cases

  • Web Developers and System Administrators: Automatically back up website files, configurations, and databases from production servers without affecting server performance.
  • Businesses with Remote Servers: Regularly back up important data from remote servers to ensure business continuity in case of server failure.
  • Data Security: Securely store backups on a local device, ensuring that sensitive information is not exposed to third-party storage solutions.

Requirements

  • SSH access to the remote servers.
  • A Mac to store the backups.
  • Basic knowledge of shell scripting and cron jobs.

Step-by-Step Guide

1. Setting Up SSH Access

To securely transfer files from your remote servers to your Mac, you need to set up SSH keys for password-less authentication.

Generate SSH Keys on Your Mac:

$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Copy the Public Key to Your Remote Servers:

$ ssh-copy-id user@remote-server-ip

This ensures that your Mac can connect to the remote servers without needing a password each time, which is essential for automating the remote server backup to Mac process.

2. Creating the Backup Script

Next, create a shell script on your Mac that will handle the backup process:

#!/bin/bash

# Server 1 Backup
echo "Starting backup for Server 1..."
ssh user@remote1 'tar -czf /tmp/backup_$(date +\%Y\%m\%d_\%H\%M\%S).tar.gz /path/to/data'
scp user@remote1:/tmp/backup_*.tar.gz /backups/remote1/
if [ $? -eq 0 ]; then
  ssh user@remote1 'rm /tmp/backup_*.tar.gz'
  echo "Backup for Server 1 completed and remote archive deleted."
else
  echo "Backup for Server 1 failed."
fi

# Server 2 Backup
echo "Starting backup for Server 2..."
ssh user@remote2 'tar -czf /tmp/backup_$(date +\%Y\%m\%d_\%H\%M\%S).tar.gz /path/to/data'
scp user@remote2:/tmp/backup_*.tar.gz /backups/remote2/
if [ $? -eq 0 ]; then
  ssh user@remote2 'rm /tmp/backup_*.tar.gz'
  echo "Backup for Server 2 completed and remote archive deleted."
else
  echo "Backup for Server 2 failed."
fi

# Optional: Clean up old backups on the Mac
find /backups/remote1/ -type f -mtime +30 -name '*.tar.gz' -exec rm {} \;
find /backups/remote2/ -type f -mtime +30 -name '*.tar.gz' -exec rm {} \;
  • Explanation:
    • The script logs into each remote server, creates a tarball of the necessary data, and transfers it to your Mac using scp.
    • After a successful transfer, the script deletes the tarball from the remote server to conserve space.
    • The script also includes an optional step to delete backups older than 30 days from your Mac.

3. Automating with Cron

To ensure regular backups, you can automate this script using cron.

Add the Script to Cron:

$ crontab -e

Example Cron Job:

Run the backup script daily at 2 AM:

0 2 * * * /path/to/your/backup_script.sh

4. Restoring Backups

To restore from a backup, reverse the process:

Transfer the Backup to the Remote Server:

$ scp /backups/remote1/backup_*.tar.gz user@remote1:/path/to/restore/

Extract the Backup on the Remote Server:

$ ssh user@remote1 'tar -xzf /path/to/restore/backup_*.tar.gz -C /path/to/destination/'

Conclusion

By following this guide, you can efficiently perform a remote server backup to Mac while managing limited storage on the servers. Automating this process ensures your data is regularly backed up without manual intervention, and old backups are cleared from the remote servers to free up space.


External Links:

Internal Links:

This article provides a reliable and automated remote server backup to Mac strategy, essential for maintaining data integrity and security.

Leave a Reply

Your email address will not be published. Required fields are marked *