<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Cron on K-Life Hack | Systems Architecture &amp; DevOps</title><link>https://klifehack.com/en/tags/cron/</link><description>Recent content in Cron on K-Life Hack | Systems Architecture &amp; DevOps</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Thu, 11 Jun 2026 10:16:07 +0900</lastBuildDate><atom:link href="https://klifehack.com/en/tags/cron/index.xml" rel="self" type="application/rss+xml"/><item><title>Automating Linux Data Synchronization Systems with Rsync and Cron</title><link>https://klifehack.com/en/p/rsync-cron-linux-backup-automation/</link><pubDate>Thu, 11 Jun 2026 10:16:07 +0900</pubDate><guid>https://klifehack.com/en/p/rsync-cron-linux-backup-automation/</guid><description>&lt;h1 id="building-an-automated-data-synchronization-system-with-rsync-and-cron"&gt;Building an Automated Data Synchronization System with Rsync and Cron
&lt;/h1&gt;&lt;p&gt;In server operations, there is always a risk of data loss due to human error, hardware failure, or external threats. Manual data replication is inefficient and can lack consistency, making it essential to build an automated system that identifies only modified files and synchronizes them periodically. This document describes the implementation steps for robust data synchronization and backup by combining Rsync and Cron, which are standard Linux utilities.&lt;/p&gt;
&lt;h3 id="1-overview-of-technical-components"&gt;1. Overview of Technical Components
&lt;/h3&gt;&lt;p&gt;Rsync (Remote Sync) is a command-line utility for synchronizing files and directories between local or remote endpoints. Unlike the standard cp command, it employs a delta transfer algorithm. This significantly reduces network bandwidth and disk I/O load by transferring only the differences (newly added or modified segments) between the source and destination.&lt;/p&gt;
&lt;p&gt;Cron (Job Scheduler) is a time-based job scheduler in Unix-like operating systems. A daemon running in the background executes specified commands or shell scripts at precise times based on configured parameters (minute, hour, day, month, day of the week).&lt;/p&gt;
&lt;h3 id="2-verification-in-local-environment-and-basic-rsync-operation"&gt;2. Verification in Local Environment and Basic Rsync Operation
&lt;/h3&gt;&lt;p&gt;Before introducing automation, verify the synchronization logic in a local environment. First, create the source and destination directories, and generate test files.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;mkdir -p ~/source_dir ~/dest_dir
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;touch ~/source_dir/file&lt;span style="color:#f92672"&gt;{&lt;/span&gt;1..5&lt;span style="color:#f92672"&gt;}&lt;/span&gt;.txt
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Run the Rsync command and confirm manual synchronization.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;rsync -avh ~/source_dir/ ~/dest_dir/
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The main options used are &lt;b&gt;-a&lt;/b&gt; (archive) to synchronize while preserving permissions, ownership, and symbolic links, &lt;b&gt;-v&lt;/b&gt; (verbose) to output details of the transfer process, and &lt;b&gt;-h&lt;/b&gt; (human-readable) to display numbers in a readable format (K, M, G).&lt;/p&gt;
&lt;h3 id="3-automating-backup-schedules-with-cron"&gt;3. Automating Backup Schedules with Cron
&lt;/h3&gt;&lt;p&gt;After manual verification is complete, integrate it into the Cron scheduler. Open the Cron configuration for the root user.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;sudo crontab -e
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Add the configuration line to the end of the file. This will run the backup every day at 3:00 AM.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#ae81ff"&gt;00&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;03&lt;/span&gt; * * * rsync -avh /home/user/source_dir/ /home/user/dest_dir/
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="4-advanced-implementation-and-log-management-with-shell-scripts"&gt;4. Advanced Implementation and Log Management with Shell Scripts
&lt;/h3&gt;&lt;p&gt;In production environments, rather than executing a single command, it is recommended to wrap it in a shell script and log the execution. Create a script that includes timestamps and execution status.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;nano ~/backup_script.sh
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Within the script, implement logic to record the execution start and end times, and aggregate standard output and standard error to a log file.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#75715e"&gt;#!/bin/bash
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;LOG_FILE&lt;span style="color:#f92672"&gt;=&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;/var/log/rsync_backup.log&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;echo &lt;span style="color:#e6db74"&gt;&amp;#34;Backup started at &lt;/span&gt;&lt;span style="color:#66d9ef"&gt;$(&lt;/span&gt;date&lt;span style="color:#66d9ef"&gt;)&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt; &amp;amp;gt;&amp;amp;gt; $LOG_FILE
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;rsync -avh /home/user/source_dir/ /home/user/dest_dir/ &amp;amp;gt;&amp;amp;gt; $LOG_FILE 2&amp;amp;gt;&amp;amp;amp;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;echo &lt;span style="color:#e6db74"&gt;&amp;#34;Backup finished at &lt;/span&gt;&lt;span style="color:#66d9ef"&gt;$(&lt;/span&gt;date&lt;span style="color:#66d9ef"&gt;)&lt;/span&gt;&lt;span style="color:#e6db74"&gt;&amp;#34;&lt;/span&gt; &amp;amp;gt;&amp;amp;gt; $LOG_FILE
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Grant execution permissions to the script and update Crontab to redirect log output.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;chmod +x ~/backup_script.sh
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;sudo crontab -e
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Modify the Crontab configuration to specify the trigger for automatic execution.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#ae81ff"&gt;00&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;03&lt;/span&gt; * * * /home/user/backup_script.sh
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;By specifying &lt;b&gt;2&amp;gt;&amp;amp;1&lt;/b&gt;, error messages are also recorded in the log file, facilitating subsequent troubleshooting.&lt;/p&gt;
&lt;h2 id="configuration-notes"&gt;Configuration Notes
&lt;/h2&gt;&lt;p&gt;🛠️ &lt;b&gt;Trailing Slash on Directories&lt;/b&gt;: In Rsync, the behavior changes depending on whether a trailing slash (/) is added to the source directory. If a trailing slash is specified, the contents inside that directory are synchronized. If no trailing slash is specified, the directory itself is copied to the destination.&lt;/p&gt;
&lt;p&gt;⚠️ &lt;b&gt;Using Dry Run&lt;/b&gt;: To avoid destructive changes, it is recommended to use the &lt;b&gt;-n&lt;/b&gt; or &lt;b&gt;&amp;ndash;dry-run&lt;/b&gt; option before applying to production to verify the files that will actually be transferred.&lt;/p&gt;
&lt;p&gt;💡 &lt;b&gt;Resource Limits&lt;/b&gt;: When synchronizing large datasets, consider using the &lt;b&gt;&amp;ndash;bwlimit&lt;/b&gt; option to limit bandwidth and minimize the impact on other services.&lt;/p&gt;</description></item></channel></rss>