{"id":2461,"date":"2024-01-24T16:44:54","date_gmt":"2024-01-24T11:14:54","guid":{"rendered":"https:\/\/www.veeble.org\/kb\/?p=2461"},"modified":"2025-04-15T15:52:11","modified_gmt":"2025-04-15T10:22:11","slug":"setup-linux-vps-backup-server-using-rsync","status":"publish","type":"post","link":"https:\/\/www.veeble.com\/kb\/setup-linux-vps-backup-server-using-rsync\/","title":{"rendered":"Setup Linux VPS Backup Server using Rsync"},"content":{"rendered":"\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/01\/Step-by-Step-Guide-to-Setup-a-Linux-VPS-Backup-Server-using-Rsync-1.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"513\" data-id=\"2546\" src=\"https:\/\/www.veeble.org\/kb\/wp-content\/uploads\/2024\/01\/Step-by-Step-Guide-to-Setup-a-Linux-VPS-Backup-Server-using-Rsync-1.png\" alt=\"Step-by-Step-Guide-to-Setup-a-Linux-VPS-Backup-Server-using-Rsync\" class=\"wp-image-2546\" srcset=\"https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Step-by-Step-Guide-to-Setup-a-Linux-VPS-Backup-Server-using-Rsync-1.png 1024w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Step-by-Step-Guide-to-Setup-a-Linux-VPS-Backup-Server-using-Rsync-1-300x150.png 300w, https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Step-by-Step-Guide-to-Setup-a-Linux-VPS-Backup-Server-using-Rsync-1-768x385.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/figure>\n\n\n\n<p>Creating regular backups for your Linux-based <a href=\"https:\/\/www.veeble.org\/blog\/8-types-of-vps-hosting-everything-you-need-to-know\/\" target=\"_blank\" rel=\"noopener\">VPS<\/a> is crucial for safeguarding your data and configurations. This tutorial walks you through the process of configuring a simple backup system with the useful and effective file synchronization program <a href=\"https:\/\/man7.org\/linux\/man-pages\/man1\/rsync.1.html\" target=\"_blank\" rel=\"noopener\">rsync<\/a>. There are other file synchronization programs also for this function such as <a href=\"https:\/\/diffingo.com\/oss\/fwbackups\" target=\"_blank\" rel=\"noopener\">fwbackups<\/a>, <code><a href=\"https:\/\/duplicity.us\/\" target=\"_blank\" rel=\"noopener\">Duplicity<\/a><\/code> and <code><a href=\"https:\/\/www.bacula.org\/free-linux-backup\/\" target=\"_blank\" rel=\"noopener\">Bacula<\/a><\/code> but for this tutorial we use <code>rsync<\/code> for its simplicity of use.<\/p>\n\n\n\n<p>In this tutorial, we will be using another Linux-based VPS for backup storage. Alternatively, you can consider using a different service such as cloud services like <a href=\"https:\/\/aws.amazon.com\/s3\/\" target=\"_blank\" rel=\"noopener\">Amazon S3<\/a>, <a href=\"https:\/\/cloud.google.com\/storage\" target=\"_blank\" rel=\"noopener\">Google Cloud Storage<\/a>, or external servers depending on your choice.<\/p>\n\n\n\n<p><\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"prerequisite\">Prerequisite<\/h4>\n\n\n<p><strong>Install rsync on both devices:<\/strong><br>You can install it on a Debian\/Ubuntu system using the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update<br>sudo apt install rsync<\/code><\/pre>\n\n\n\n<p>For RedHat\/Centos system:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo yum install rsync<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>After installation, ensure that the <code>rsync<\/code> service is enabled and running:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl enable rsync\nsudo systemctl start rsync\nsudo systemctl status rsync<\/code><\/pre>\n\n\n\n<p class=\"has-small-font-size\"><em>(rsync is usually pre-installed on most Linux distributions, but it&#8217;s always good to make sure you have the latest version<\/em>)<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Install Source Server SSH-Key on the Backup Server:<\/strong><br>You can generate a key on your source server using the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh-keygen -t rsa<\/code><\/pre>\n\n\n\n<p class=\"has-small-font-size\"><em>(Press Enter to accept the default file location and passphrase)<\/em><\/p>\n\n\n\n<p>After generating the key, copy the public key to the backup server. For that execute the following command replacing the username and IP address on your source server:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh-copy-id username@backup_server_ip<\/code><\/pre>\n\n\n\n<p class=\"has-small-font-size\"><em>(Follow the prompts to complete the copy)<\/em><\/p>\n\n\n\n<p>From the source server, try to SSH into the backup server to ensure the key is working!<\/p>\n\n\n\n<p>Reference: <a href=\"https:\/\/www.veeble.org\/kb\/ssh-login-without-password\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.veeble.org\/kb\/ssh-login-without-password\/<\/a><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"create-and-configure-the-rsync-script\">Create and Configure the Rsync Script<\/h4>\n\n\n<p>A sample script, <code>backup_script.sh<\/code>, is provided for synchronization. Customize paths, usernames, and IP addresses as needed. The script logs actions, checks Rsync&#8217;s exit status, and deletes old backups based on a specified retention period.<\/p>\n\n\n\n<p><strong>Example Script:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-ast-global-color-4-color has-black-background-color has-text-color has-background has-link-color has-small-font-size wp-elements-a1e4685b541ee6bbdf46e6f34637415d\"><code>#!\/bin\/bash\n\n# Configuration\nSOURCE=\"\/path\/to\/source\/\"\nDEST=\"backup_user@backup_server_ip:\/path\/to\/backup\/\"\nLOGFILE=\"\/var\/log\/rsync_backup.log\"\nDATE=`date +%Y-%m-%d`\nHOSTNAME=$(hostname)\nRETENTION_DAYS=30  # Number of days to keep backups\n\n# Start backup\necho \"Backup for $DATE on $HOSTNAME\" &gt; $LOGFILE\nrsync -avz --delete $SOURCE $DEST &gt;&gt; $LOGFILE 2&gt;&amp;1\n\n# Check if rsync is successful\nif &#91; $? -eq 0 ]; then\n    echo \"Backup completed successfully\" &gt;&gt; $LOGFILE\nelse\n    echo \"Backup failed\" &gt;&gt; $LOGFILE\nfi\n\n# Delete old backups\necho \"Deleting backups older than $RETENTION_DAYS days\" &gt;&gt; $LOGFILE\nssh backup_user@backup_server_ip \"find \/path\/to\/backup\/ -mtime +$RETENTION_DAYS -type f -delete\" &gt;&gt; $LOGFILE 2&gt;&amp;1<\/code><\/pre>\n\n\n\n<p>This script facilitates the synchronization of the &#8220;\/path\/to\/source\/&#8221; directory on your VPS with the &#8220;\/path\/to\/backup\/&#8221; directory on the backup server. Ensure to replace placeholders with precise values for the source and backup paths, username, and IP address of the backup server.<\/p>\n\n\n\n<p>The script meticulously logs every action it takes, providing a straightforward means for debugging and monitoring the backup process. It verifies the exit status of rsync, logging any failures if the status is not equal to 0.<\/p>\n\n\n\n<p>To enhance tracking, the script incorporates the date and hostname in its logs, which proves especially useful when managing multiple servers.<\/p>\n\n\n\n<p>Additionally, the script utilizes the find command to locate and remove files older than a specified retention period (in this instance, 30 days). The &#8220;-mtime +$RETENTION_DAYS&#8221; option in find identifies files modified more than $RETENTION_DAYS days ago, while the &#8220;-type f -delete&#8221; options instruct find to delete these identified files.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Make the Script Executable<\/strong>:<\/p>\n\n\n\n<p>Set the script as executable with the command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>chmod +x ~\/backup_script.sh<\/code><\/pre>\n\n\n\n<p>Reference: <a href=\"https:\/\/www.veeble.org\/kb\/linux-file-permissions\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.veeble.org\/kb\/linux-file-permissions\/<\/a><\/p>\n\n\n\n<p><\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"automating-task-execution-with-cron\">Automating Task Execution with Cron<\/h4>\n\n\n<p>To create a cron job, execute the following command to open the cron job editor:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>crontab -e<\/code><\/pre>\n\n\n\n<p>Add or modify the line for your backup schedule:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0 1 * * * \/home\/yourusername\/backup_script.sh<\/code><\/pre>\n\n\n\n<p>Refer <a href=\"https:\/\/www.veeble.org\/kb\/cron-job-how-to-schedule-recurring-user-jobs\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.veeble.org\/kb\/cron-job-how-to-schedule-recurring-user-jobs\/<\/a> for modifying cron according to your choice.<\/p>\n\n\n\n<p>To confirm the addition of your cron job, you can view a list of your user&#8217;s scheduled tasks using the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>crontab -l<\/code><\/pre>\n\n\n\n<p>Your script is set to execute automatically at the designated time specified in the cron job.<\/p>\n\n\n<h4 class=\"wp-block-heading\" id=\"notes\">Notes<\/h4>\n\n\n<ul class=\"wp-block-list\">\n<li>Regularly check <code>rsync_backup.log<\/code> to make sure there are no errors.<\/li>\n\n\n\n<li>Verify that the backup server is receiving the files as expected.<\/li>\n\n\n\n<li>The script executes the <code>find<\/code> command on the backup server via SSH. Ensure that the SSH user (<code>backup_user<\/code>) has the necessary permissions to delete these backup files.<\/li>\n\n\n\n<li>This approach assumes that your backups are stored in a way that each backup is a separate file and that these files have modification dates that reflect their backup dates. If your backup structure is different (e.g., using directories for each backup), you&#8217;ll need to modify the deletion command accordingly.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>With this guide, you&#8217;re well-equipped to set up a secure and efficient backup system using Rsync for your Linux VPS, ensuring your data is reliably backed up.<\/p>\n\n\n\n<div style=\"height:25px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-uagb-call-to-action uagb-block-cd2ad96f wp-block-button\"><div class=\"uagb-cta__wrap\"><h3 class=\"uagb-cta__title\">Unleash the Power of AWS, Without the Hassle<\/h3><p class=\"uagb-cta__desc\">AWS offers incredible potential, but managing it can be complex. Veeble&#8217;s managed hosting simplifies everything, from setup to security, so you can benefit from AWS without the headaches.<\/p><\/div><div class=\"uagb-cta__buttons\"><a href=\"https:\/\/www.veeble.com\/in\/managed-aws-hosting\/\" class=\"uagb-cta__button-link-wrapper wp-block-button__link\" target=\"_blank\" rel=\"noopener noreferrer\">Explore Managed AWS<svg xmlns=\"https:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\"><path d=\"M504.3 273.6l-112.1 104c-6.992 6.484-17.18 8.218-25.94 4.406c-8.758-3.812-14.42-12.45-14.42-21.1L351.9 288H32C14.33 288 .0002 273.7 .0002 255.1S14.33 224 32 224h319.9l0-72c0-9.547 5.66-18.19 14.42-22c8.754-3.809 18.95-2.075 25.94 4.41l112.1 104C514.6 247.9 514.6 264.1 504.3 273.6z\"><\/path><\/svg><\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Creating regular backups for your Linux-based VPS is crucial for safeguarding your data and configurations. This tutorial walks you through the process of configuring [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":2546,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[5],"tags":[],"class_list":["post-2461","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"uagb_featured_image_src":{"full":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Step-by-Step-Guide-to-Setup-a-Linux-VPS-Backup-Server-using-Rsync-1.png",1024,513,false],"thumbnail":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Step-by-Step-Guide-to-Setup-a-Linux-VPS-Backup-Server-using-Rsync-1-150x150.png",150,150,true],"medium":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Step-by-Step-Guide-to-Setup-a-Linux-VPS-Backup-Server-using-Rsync-1-300x150.png",300,150,true],"medium_large":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Step-by-Step-Guide-to-Setup-a-Linux-VPS-Backup-Server-using-Rsync-1-768x385.png",768,385,true],"large":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Step-by-Step-Guide-to-Setup-a-Linux-VPS-Backup-Server-using-Rsync-1.png",1024,513,false],"1536x1536":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Step-by-Step-Guide-to-Setup-a-Linux-VPS-Backup-Server-using-Rsync-1.png",1024,513,false],"2048x2048":["https:\/\/www.veeble.com\/kb\/wp-content\/uploads\/2024\/01\/Step-by-Step-Guide-to-Setup-a-Linux-VPS-Backup-Server-using-Rsync-1.png",1024,513,false]},"uagb_author_info":{"display_name":"Adrian Antony","author_link":"https:\/\/www.veeble.com\/kb\/author\/adrian\/"},"uagb_comment_info":0,"uagb_excerpt":"Creating regular backups for your Linux-based VPS is crucial for safeguarding your data and configurations. This tutorial walks you through the process of configuring [&hellip;]","_links":{"self":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/2461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/comments?post=2461"}],"version-history":[{"count":22,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/2461\/revisions"}],"predecessor-version":[{"id":8609,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/posts\/2461\/revisions\/8609"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/media\/2546"}],"wp:attachment":[{"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/media?parent=2461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/categories?post=2461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.veeble.com\/kb\/wp-json\/wp\/v2\/tags?post=2461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}