• Contact
  • About Me
  • Privacy Policy
DefenceDev
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
No Result
View All Result
DefenceDev
No Result
View All Result
ADVERTISEMENT
Home Solutions

Solution: Automate Daily Backup of Remote Ubuntu Server Folder with a Bash Script

neo by neo
May 27, 2025
in Solutions
0
Solution: Automate Daily Backup of Remote Ubuntu Server Folder with a Bash Script

Solution: Automate Daily Backup of Remote Ubuntu Server Folder with a Bash Script

0
SHARES
12
VIEWS
Share on FacebookShare on Twitter
ADVERTISEMENT

Automate Daily Backup (Sync) Ubuntu Server Folder

Automating daily backups is crucial for protecting valuable data on remote servers. Without a proper backup plan, accidental data loss, hardware failures, or unforeseen issues can lead to significant disruptions. In this post, we’ll explore how to automate the process of backing up a specific folder on a remote Ubuntu server.

The rsync command on Ubuntu is a versatile tool for syncing files and directories, which can be used for backups as it efficiently copies and synchronizes data locally or remotely, preserving permissions, timestamps, and optionally deleting files no longer present at the source. In this case we are not deleting files which are no longer present at the source.

To ensure that files not present on the source are removed from the destination, you need to add the --delete option and remove --ignore-existing.

Introduction

In this article, I will present you my way to automate daily backup with a Bash script of the folder on remote Ubuntu Server.

Backing up data is crucial for any system administrator to ensure data integrity and availability. Automating backups can save time and reduce the risk of human error. In this post, we will guide you through setting up a daily backup of a folder from a remote Ubuntu server using a bash script and rsync.

ADVERTISEMENT

Prerequisites

  • Two Ubuntu machines (one acting as the backup server, another as the source).
  • SSH access between the machines. -> Click on the link to check how to configure it
  • Basic knowledge of bash scripting and rsync.

The Bash Script for Automate Daily Folder Backup

Create a script:

nano /opt/scripts/bkp_pi-share.sh
#!/bin/sh
runtime=$(date +%Y%m%d_%H%M)

# Createa a log entry for the executed backup
echo "Starting sync between remote host and folder on local host" >> /opt/scripts/log/bkp_pi-share_$runtime.log

# Write a exact time before start
echo $(date) >> /opt/scripts/log/bkp_pi-share_$runtime.log

# Source system: user@REMOTE_HOST_IP:/remote_folder/
# Destination: folder on the local server 

sudo rsync -avzh --ignore-existing --rsync-path="sudo rsync" ubuntu@192.168.20.116:/srv/share/ /opt/data/bkp_pi-share >> /opt/scripts/log/bkp_pi-share_$runtime.log

Script Breakdown

  • runtime=$(date +%Y%m%d_%H%M): This line sets the runtime variable to the current date and time, formatted as YYYYMMDD_HHMM.
  • echo "Start sync..." >> /opt/scripts/log/bkp_pi-share_$runtime.log: Logs the start of the sync operation.
  • sudo rsync -avzh --ignore-existing --rsync-path="sudo rsync": Uses rsync to synchronize the files from the remote server to the local backup directory. The --ignore-existing flag ensures that only new or modified files are copied, saving time and bandwidth.

Make the script executable:

chmod +x /opt/scripts/bkp_pi-share.sh

Setting Up the Cron Job

To automate the script to run daily, add a cron job:

ADVERTISEMENT
ADVERTISEMENT
  • Open the crontab file:
crontab -e
  • Add the following line to schedule the script to run daily at 2 AM:
0 2 * * * /opt/scripts/bkp_pi-share.sh

About The Author

neo

See author's posts

ADVERTISEMENT
Previous Post

Linux Process Management, kill Command

Next Post

WordPress: Renew HTTPS Certificate on WordPress Site

neo

neo

Next Post
WordPress: Renew HTTPS Certificate on WordPress Site

WordPress: Renew HTTPS Certificate on WordPress Site

Follow Us

  • Trending
  • Comments
  • Latest
MikroTik: Export Configuration in Text File

MikroTik: Export Configuration in Text File

Fortinet FortiGate: Static Route Configuration via GUI and CLI

Fortinet FortiGate: Static Route Configuration via GUI and CLI

Immich: Installation on Ubuntu 22.04

Immich: Installation on Ubuntu 22.04

Fortinet FortiGate Upgrade Path Tool

Fortinet FortiGate Upgrade Path Tool

NextCloud: Port Forwarding - Essential Ports for Smooth Functionality

NextCloud: Port Forwarding – Essential Ports for Smooth Functionality and Remote Access

Organizing and Managing Photos with Immich: Features I Use

Organizing and Managing Photos with Immich: Features I Use

Install Ubuntu 22.04 on Oracle VM Virtual Box

Install Ubuntu 22.04 on Oracle VM Virtual Box

Linux Directories: cd (Change Directory), pwd (Print Working Directory), mkdir (Make Directory)

Linux Directories: cd (Change Directory), pwd (Print Working Directory), mkdir (Make Directory)

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

Recent News

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nginx Proxy Manager on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

MikroTik: Check Your Wireless Password

MikroTik: Check Your Wireless Password

ADVERTISEMENT

DefenceDev Tutorials

defencedev Logo

Whether you’re just starting or looking to expand your skills, I hope you find useful information and engaging discussions here. Let me take you through my journey and the goals behind this space!

Follow Us

Browse by Category

  • Blog
  • Cloud
    • Private
  • Linux Tutorials
    • Bash Scripting Tutorials
    • Commands
    • Networking
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Solutions
    • Docker
  • Video Tutorials
    • MikroTik Videos

Recent News

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install WordPress on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

Install Nextcloud on Raspberry Pi 4 with Docker (Ubuntu 22.04)

  • Site Map
  • Privacy Policy
  • Facebook Page
  • Disclaimer
  • Contact
  • About Me

© 2025 defencedev.com - All rights reserved.

No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos

© 2025 defencedev.com - All rights reserved.