• Contact
  • About Me
  • Privacy Policy
  • Disclaimer
DefenceDev
  • Home
  • Linux
    • Bash Scripting
      • Bash Scripting Lessons
    • Commands&Tips
    • Monitoring
  • Networking
    • FortiGate
    • MikroTik
  • Projects & Case Studies
  • Self-Hosted
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Virtualization
    • Proxmox
    • VMware
  • Tools
No Result
View All Result
  • Home
  • Linux
    • Bash Scripting
      • Bash Scripting Lessons
    • Commands&Tips
    • Monitoring
  • Networking
    • FortiGate
    • MikroTik
  • Projects & Case Studies
  • Self-Hosted
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Virtualization
    • Proxmox
    • VMware
  • Tools
No Result
View All Result
DefenceDev
No Result
View All Result
Home Linux

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

by neo
September 3, 2026
in Linux
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
29
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:

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:

  • 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
Tags: bash-scripting
Previous Post

Linux Process Management, kill Command

Next Post

WordPress: Renew HTTPS Certificate on WordPress Site

neo

Related Posts

Get Instant SSH Login Alerts on Ubuntu with Bash and Telegram
Monitoring

Get Instant SSH Login Alerts on Ubuntu with Bash and Telegram

Terminal Colors in Bash: How and When to Use Them
Bash Scripting

Terminal Colors in Bash: How and When to Use Them

Solution: Ubuntu Internet Connection Monitoring Script (Create a Log File, Possible to Send per Email)
Monitoring

Ubuntu Internet Connection Monitoring Script: Log Creation and Email Alerts

Free Online Image Tools– Fast, Private, and 100% Browser‑Based
Linux

Free Online Image Tools– Fast, Private, and 100% Browser‑Based

10+ Useful Linux find Command Examples You Should Know
Commands&Tips

10+ Useful Linux find Command Examples You Should Know

Server Reboot with Ansible
Linux

Automating Linux Server Reboot with Ansible

Next Post
WordPress: Renew HTTPS Certificate on WordPress Site

WordPress: Renew HTTPS Certificate on WordPress Site

Recommended

Lesson 10: Bash File Operation (Read, Write, Copy)

Lesson 10: Bash File Operation (Read, Write, Copy)

Linux Networking: Add VLAN on Ubuntu with netplan

Linux Networking: Add VLAN on Ubuntu with netplan

Briefly unavailable for scheduled maintenance. Check back in a minute.

WordPress Stuck in Maintenance Mode After Plugin Update

Promox Telegram Alert Example

Configure Telegram Notifications in Proxmox VE Using Webhooks

Fix Proxmox VM Snapshot “Out of Space” Errors

Fix Proxmox VM Snapshot “Out of Space” Errors

Categories

  • Linux
    • Bash Scripting
    • Commands&Tips
    • Monitoring
  • Networking
    • FortiGate
    • MikroTik
  • Projects & Case Studies
  • Self-Hosted
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Virtualization
    • Proxmox
    • VMware
  • Web Tools

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

Briefly unavailable for scheduled maintenance. Check back in a minute.

WordPress Stuck in Maintenance Mode After Plugin Update

Promox Telegram Alert Example

Configure Telegram Notifications in Proxmox VE Using Webhooks

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

© 2025 defencedev.com - All rights reserved.

No Result
View All Result
  • Home
  • Linux
    • Bash Scripting
      • Bash Scripting Lessons
    • Commands&Tips
    • Monitoring
  • Networking
    • FortiGate
    • MikroTik
  • Projects & Case Studies
  • Self-Hosted
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Virtualization
    • Proxmox
    • VMware
  • Tools

© 2025 defencedev.com - All rights reserved.