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

Automate Daily Backups of Remote Ubuntu Server Folders Using a Bash Script

neo by neo
September 11, 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
16
VIEWS
Share on FacebookShare on LinkedIn
Table of Contents
  • Automate Daily Backup (Sync) Ubuntu Server Folder
  • Introduction
  • Prerequisites
  • The Bash Script for Automate Daily Folder Backup
    • Script Breakdown
  • Setting Up the Cron Job

Automate Daily Backup (Sync) Ubuntu Server Folder

Protecting your data on remote Ubuntu servers starts with regular backups. Automating daily backups not only saves you time but also prevents data loss caused by hardware failures or unexpected issues. First, you’ll learn how to set up a simple script to back up a specific folder. Then, you’ll automate the process to run daily, ensuring your important files stay safe without manual effort.

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.

Every system administrator needs to back up data regularly to ensure its integrity and availability. Automating backups saves time and reduces the chance of human error. In this post, we’ll show you how to set up a daily backup of a folder from a remote Ubuntu server using a bash script and rsync.

Prerequisites

  • Two Ubuntu machines: one serves as the backup server, and the other acts 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:

ADVERTISEMENT
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
  • 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

Tags: bash-scripting
ADVERTISEMENT
Previous Post

Linux Process Management, kill Command

Next Post

WordPress: Renew HTTPS Certificate on WordPress Site

neo

neo

Related Posts

Install Portainer on Ubuntu 24.04 Using Docker Compose
Docker

Install Portainer on Ubuntu 24.04 Using Docker Compose

Automatic Windows Service Monitoring and Restart Script
Solutions

Automatic Windows Service Monitoring and Restart Script

Host a Website on Raspberry Pi – Real Benchmark Tests
Solutions

Host a Website on Raspberry Pi – Real Benchmark Tests

My Ultimate Docker Command Cheat Sheet (2025 Edition)
Docker

My Ultimate Docker Commands Cheat Sheet (2025 Edition)

Protect Docker-Hosted Application from HTTP Flood (DDoS) Attacks Using a Bash Script
Docker

Protect Docker-Hosted Application from HTTP Flood (DDoS) Attacks Using a Bash Script

Raspberry Pi Monitoring with Monit: Docker, Temperature, Network & More
Solutions

Raspberry Pi Monitoring with Monit: Docker, Temperature & More

Next Post
WordPress: Renew HTTPS Certificate on WordPress Site

WordPress: Renew HTTPS Certificate on WordPress Site

Recommended

Remote Desktop Access to Ubuntu 24.04 from Windows

Remote Desktop Access to Ubuntu 24.04 from Windows

Fortinet FortiGate: Default Admin Profiles and Permissions

Fortinet FortiGate: Default Admin Profiles and Permissions

Install Portainer on Ubuntu 24.04 Using Docker Compose

Install Portainer on Ubuntu 24.04 Using Docker Compose

FortiGate Country Address Object Generator – Block or Allow Access by Country

FortiGate Country Address Object Generator – Block or Allow Access by Country

View & Copy Image Metadata Online – Camera & GPS Info

View & Copy Image Metadata Online – Camera & GPS Info

Categories

  • 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
  • Web Tools
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

Recent News

Install Portainer on Ubuntu 24.04 Using Docker Compose

Install Portainer on Ubuntu 24.04 Using Docker Compose

FortiGate Country Address Object Generator – Block or Allow Access by Country

FortiGate Country Address Object Generator – Block or Allow Access by Country

  • 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
    • Bash Scripts
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
  • Web Tools

© 2025 defencedev.com - All rights reserved.