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

Remote Server Backup square

Backing up your remote servers is essential for maintaining data security and ensuring effective disaster recovery. When working with limited storage on remote servers and relying solely on SSH without the ability to install additional utilities, a tailored backup approach is necessary. This guide will walk you through the process of creating automated backups from remote Linux servers to your Mac, ensuring that your data remains safe and accessible while managing storage constraints effectively.

By implementing an automated backup solution, you can regularly transfer critical data to your Mac while setting up a system to remove older backups, conserving valuable space on both your remote server and local machine. With clear instructions and practical tips, this guide will empower you to safeguard your data seamlessly and efficiently, providing peace of mind in case of unexpected data loss.

Caveat:


Table of Contents


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:

Leave a Reply

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