{"id":16369,"date":"2024-10-31T19:36:43","date_gmt":"2024-10-31T19:36:43","guid":{"rendered":"https:\/\/truehost.com\/support\/?post_type=docs&#038;p=16369"},"modified":"2024-11-02T07:52:46","modified_gmt":"2024-11-02T07:52:46","password":"","slug":"automating-server-backups-using-rsync-and-cron","status":"publish","type":"docs","link":"https:\/\/truehost.com\/support\/knowledge-base\/automating-server-backups-using-rsync-and-cron\/","title":{"rendered":"Automating Server Backups Using rsync and cron"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Why Use <code>rsync<\/code> and <code>cron<\/code> for Backups?<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>rsync<\/strong>: A powerful file synchronization tool that copies only changed files, minimizing bandwidth and storage usage.<\/li>\n\n\n\n<li><strong>cron<\/strong>: A scheduling tool that allows you to run commands at specified intervals automatically.<\/li>\n<\/ul>\n\n\n\n<p>Using <code>rsync<\/code> with <code>cron<\/code> enables efficient, automated, and scheduled backups for important files or directories on your Linux server.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Prerequisites<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>A <strong>Linux server<\/strong> with <code>rsync<\/code> installed.\n<ul class=\"wp-block-list\">\n<li>Install <code>rsync<\/code> if it\u2019s not already installed:<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install rsync # On Debian\/Ubuntu\n\nsudo yum install rsync # On CentOS\/RHEL<\/code><\/pre>\n\n\n\n<p>  2. <strong>SSH Access<\/strong> (if backing up to a remote server).<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Set up SSH key authentication for passwordless access to the remote server.<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\"><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Basic <code>rsync<\/code> Command for Backups<\/h3>\n\n\n\n<p>The basic syntax for using <code>rsync<\/code> to back up files is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rsync -avz \/path\/to\/source \/path\/to\/destination<\/code><\/pre>\n\n\n\n<p><strong>Flags:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>-a<\/code>: Archive mode (preserves permissions, timestamps, symlinks).<\/li>\n\n\n\n<li><code>-v<\/code>: Verbose (shows details of files being transferred).<\/li>\n\n\n\n<li><code>-z<\/code>: Compress data during transfer (useful for remote backups).<\/li>\n<\/ul>\n\n\n\n<p><strong>Example:<\/strong> To back up a directory <code>\/var\/www<\/code> to another local directory <code>\/backup<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rsync -avz \/var\/www \/backup<\/code><\/pre>\n\n\n\n<p><strong>Remote Backup Example:<\/strong> To back up <code>\/var\/www<\/code> to a remote server:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rsync -avz \/var\/www username@remote_server:\/backup<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Create a Backup Script<\/h3>\n\n\n\n<p>To simplify the backup process and make it suitable for scheduling, create a script.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Create a new file<\/strong> for the script:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo vi \/usr\/local\/bin\/backup.sh<\/code><\/pre>\n\n\n\n<p>   2. <strong>Add the following script<\/strong>, modifying paths and credentials as needed:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\n# Set variables\nSOURCE_DIR=\"\/var\/www\"\nDESTINATION_DIR=\"\/backup\"\nREMOTE_USER=\"username\"\nREMOTE_HOST=\"remote_server\"\nREMOTE_DESTINATION=\"\/backup\"\n\n# Local backup\nrsync -avz $SOURCE_DIR $DESTINATION_DIR\n\n# Uncomment the following line for remote backup\n# rsync -avz $SOURCE_DIR $REMOTE_USER@$REMOTE_HOST:$REMOTE_DESTINATION\n\n# Print completion message\necho \"Backup completed on $(date)\"\n<\/code><\/pre>\n\n\n\n<p>3. <strong>Make the script executable<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo chmod +x \/usr\/local\/bin\/backup.sh<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Schedule the Script with <code>cron<\/code><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Open the cron editor<\/strong> for the root user or a specific user (e.g., root):<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo crontab -e<\/code><\/pre>\n\n\n\n<p>   2. <strong>Add a cron job<\/strong> to schedule the backup. For example, to run the backup daily at 2 AM, add the   following line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0 2 * * * \/usr\/local\/bin\/backup.sh >> \/var\/log\/backup.log 2>&amp;1<\/code><\/pre>\n\n\n\n<p><strong>Explanation of the schedule:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>0 2 * * *<\/code>: Runs every day at 2:00 AM.<\/li>\n\n\n\n<li><code>>> \/var\/log\/backup.log 2>&amp;1<\/code>: Appends output and errors to a log file, helping with troubleshooting.<\/li>\n<\/ul>\n\n\n\n<p>3. <strong>Save and exit<\/strong> the cron editor.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Verify and Monitor the Backup<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Check the Backup Directory<\/strong>: After the cron job runs, verify that files are being copied to the backup location:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>ls \/backup # Check the backup directory<\/code><\/pre>\n\n\n\n<p>2. <strong>Review the Log File<\/strong>: The log file (<code>\/var\/log\/backup.log<\/code>) will contain details of each backup run, useful for verifying successful execution and troubleshooting errors.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat \/var\/log\/backup.log<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: (Optional) Set Up Rotations to Manage Storage<\/h3>\n\n\n\n<p>To prevent backups from consuming excessive storage, set up log rotation for backups. For example, use <code>find<\/code> to remove backups older than 7 days.<\/p>\n\n\n\n<p>Add this to the script for automatic cleanup:<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Delete backups older than 7 days<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>find \/backup -type f -mtime +7 -exec rm {} \\;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example Full Backup Script with Cleanup<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\n# Set variables\nSOURCE_DIR=\"\/var\/www\"\nDESTINATION_DIR=\"\/backup\"\nREMOTE_USER=\"username\"\nREMOTE_HOST=\"remote_server\"\nREMOTE_DESTINATION=\"\/backup\"\n\n# Local backup\nrsync -avz $SOURCE_DIR $DESTINATION_DIR\n\n# Uncomment the following line for remote backup\n# rsync -avz $SOURCE_DIR $REMOTE_USER@$REMOTE_HOST:$REMOTE_DESTINATION\n\n# Delete backups older than 7 days\nfind $DESTINATION_DIR -type f -mtime +7 -exec rm {} \\;\n\n# Print completion message\necho \"Backup completed on $(date)\"\n<\/code><\/pre>\n\n\n\n<p>This script backs up the <code>\/var\/www<\/code> directory, optionally to a remote server, and deletes backups older than seven days.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>Automating server backups with <code>rsync<\/code> and <code>cron<\/code> provides a robust solution for maintaining regular backups. By customizing the schedule and locations, you can ensure reliable and efficient backups with minimal manual intervention.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Why Use rsync and cron for Backups? Using rsync with cron enables efficient, automated, and scheduled backups for important files or directories on your Linux server. Prerequisites 2. SSH Access (if backing up to a remote server). Step 1: Basic rsync Command for Backups The basic syntax for using rsync to back up files is: [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_eb_attr":"","_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"doc_category":[1820,1824,1879,2128],"doc_tag":[],"class_list":["post-16369","docs","type-docs","status-publish","hentry","doc_category-cloud-servers-in-kenya","doc_category-dedicated-servers","doc_category-servers","doc_category-vps-servers"],"year_month":"2026-07","word_count":628,"total_views":0,"reactions":{"happy":0,"normal":0,"sad":0},"author_info":{"name":"Eugene","author_nicename":"eugene","author_url":"https:\/\/truehost.com\/support\/author\/eugene\/"},"doc_category_info":[{"term_name":"Cloud servers in Kenya","term_url":"https:\/\/truehost.com\/support\/docs-category\/cloud-servers-in-kenya\/"},{"term_name":"dedicated servers","term_url":"https:\/\/truehost.com\/support\/docs-category\/dedicated-servers\/"},{"term_name":"Servers","term_url":"https:\/\/truehost.com\/support\/docs-category\/servers\/"},{"term_name":"VPS-Servers","term_url":"https:\/\/truehost.com\/support\/docs-category\/vps-servers\/"}],"doc_tag_info":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Automating Server Backups Using rsync and cron -<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/truehost.com\/support\/knowledge-base\/automating-server-backups-using-rsync-and-cron\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Automating Server Backups Using rsync and cron -\" \/>\n<meta property=\"og:description\" content=\"Why Use rsync and cron for Backups? Using rsync with cron enables efficient, automated, and scheduled backups for important files or directories on your Linux server. Prerequisites 2. SSH Access (if backing up to a remote server). Step 1: Basic rsync Command for Backups The basic syntax for using rsync to back up files is: [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/truehost.com\/support\/knowledge-base\/automating-server-backups-using-rsync-and-cron\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-02T07:52:46+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/automating-server-backups-using-rsync-and-cron\\\/\",\"url\":\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/automating-server-backups-using-rsync-and-cron\\\/\",\"name\":\"Automating Server Backups Using rsync and cron -\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#website\"},\"datePublished\":\"2024-10-31T19:36:43+00:00\",\"dateModified\":\"2024-11-02T07:52:46+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/automating-server-backups-using-rsync-and-cron\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/automating-server-backups-using-rsync-and-cron\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/automating-server-backups-using-rsync-and-cron\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/truehost.com\\\/support\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Automating Server Backups Using rsync and cron\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#website\",\"url\":\"https:\\\/\\\/truehost.com\\\/support\\\/\",\"name\":\"\",\"description\":\"Help In a Click\",\"publisher\":{\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/truehost.com\\\/support\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#organization\",\"name\":\"Truehost Kenya\",\"url\":\"https:\\\/\\\/truehost.com\\\/support\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/truehost.com\\\/support\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/cropped-image_2026-04-16_174808866.png\",\"contentUrl\":\"https:\\\/\\\/truehost.com\\\/support\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/cropped-image_2026-04-16_174808866.png\",\"width\":240,\"height\":48,\"caption\":\"Truehost Kenya\"},\"image\":{\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#\\\/schema\\\/logo\\\/image\\\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Automating Server Backups Using rsync and cron -","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/truehost.com\/support\/knowledge-base\/automating-server-backups-using-rsync-and-cron\/","og_locale":"en_US","og_type":"article","og_title":"Automating Server Backups Using rsync and cron -","og_description":"Why Use rsync and cron for Backups? Using rsync with cron enables efficient, automated, and scheduled backups for important files or directories on your Linux server. Prerequisites 2. SSH Access (if backing up to a remote server). Step 1: Basic rsync Command for Backups The basic syntax for using rsync to back up files is: [&hellip;]","og_url":"https:\/\/truehost.com\/support\/knowledge-base\/automating-server-backups-using-rsync-and-cron\/","article_modified_time":"2024-11-02T07:52:46+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/truehost.com\/support\/knowledge-base\/automating-server-backups-using-rsync-and-cron\/","url":"https:\/\/truehost.com\/support\/knowledge-base\/automating-server-backups-using-rsync-and-cron\/","name":"Automating Server Backups Using rsync and cron -","isPartOf":{"@id":"https:\/\/truehost.com\/support\/#website"},"datePublished":"2024-10-31T19:36:43+00:00","dateModified":"2024-11-02T07:52:46+00:00","breadcrumb":{"@id":"https:\/\/truehost.com\/support\/knowledge-base\/automating-server-backups-using-rsync-and-cron\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/truehost.com\/support\/knowledge-base\/automating-server-backups-using-rsync-and-cron\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/truehost.com\/support\/knowledge-base\/automating-server-backups-using-rsync-and-cron\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/truehost.com\/support\/"},{"@type":"ListItem","position":2,"name":"Automating Server Backups Using rsync and cron"}]},{"@type":"WebSite","@id":"https:\/\/truehost.com\/support\/#website","url":"https:\/\/truehost.com\/support\/","name":"","description":"Help In a Click","publisher":{"@id":"https:\/\/truehost.com\/support\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/truehost.com\/support\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/truehost.com\/support\/#organization","name":"Truehost Kenya","url":"https:\/\/truehost.com\/support\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/truehost.com\/support\/#\/schema\/logo\/image\/","url":"https:\/\/truehost.com\/support\/wp-content\/uploads\/2026\/04\/cropped-image_2026-04-16_174808866.png","contentUrl":"https:\/\/truehost.com\/support\/wp-content\/uploads\/2026\/04\/cropped-image_2026-04-16_174808866.png","width":240,"height":48,"caption":"Truehost Kenya"},"image":{"@id":"https:\/\/truehost.com\/support\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/docs\/16369","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/comments?post=16369"}],"version-history":[{"count":3,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/docs\/16369\/revisions"}],"predecessor-version":[{"id":16568,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/docs\/16369\/revisions\/16568"}],"wp:attachment":[{"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/media?parent=16369"}],"wp:term":[{"taxonomy":"doc_category","embeddable":true,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/doc_category?post=16369"},{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/doc_tag?post=16369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}