{"id":586,"date":"2026-07-21T03:23:57","date_gmt":"2026-07-21T03:23:57","guid":{"rendered":"https:\/\/virtualserversvps.com\/blog\/?p=586"},"modified":"2026-07-28T22:12:16","modified_gmt":"2026-07-28T22:12:16","slug":"vps-backup-pipeline-automation-shell-scripts","status":"publish","type":"post","link":"https:\/\/virtualserversvps.com\/blog\/vps-backup-pipeline-automation-shell-scripts\/","title":{"rendered":"VPS Backup Pipeline: Automating Database and File Backups with Shell Scripts"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Data loss on a VPS is not a matter of <em>if<\/em> but <em>when<\/em>. Disk failure, accidental <code>rm -rf<\/code>, security breaches, or provider outages can wipe out critical data in seconds. A robust automated backup pipeline is your safety net. This guide walks through building a complete backup system using shell scripts \u2014 supporting MySQL databases, application files, encrypted archives, and off-site storage \u2014 all running on your VPS without proprietary backup agents.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"why-automate\">Why Automate Backups on Your VPS?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Manual backups are unreliable. Automation removes human error. A well-designed pipeline gives you:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Recovery Point Objective (RPO) control<\/strong> \u2014 Define exactly how much data you are willing to lose (15 minutes, 1 hour, 24 hours).<\/li>\n<li><strong>Recovery Time Objective (RTO)<\/strong> \u2014 Restore from backup in minutes with pre-tested procedures.<\/li>\n<li><strong>Off-site redundancy<\/strong> \u2014 Encrypted backups stored on separate infrastructure.<\/li>\n<li><strong>Point-in-time recovery<\/strong> \u2014 Restore any previous snapshot from your retention window.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">When evaluating VPS hardware for backup storage, <a href=\"https:\/\/virtualserversvps.com\/#providers\">compare VPS providers on our comparison table<\/a> to find plans with high-capacity NVMe storage and generous bandwidth allowances.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"architecture\">Backup Architecture Overview<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Our pipeline follows the 3-2-1 rule: three copies of data, on two different media types, with one copy off-site. The pipeline stages are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Stage 1<\/strong> \u2014 MySQL database dump with <code>mysqldump<\/code> and <code>--single-transaction<\/code><\/li>\n<li><strong>Stage 2<\/strong> \u2014 Filesystem archive with <code>tar<\/code> and <code>pigz<\/code> (parallel gzip)<\/li>\n<li><strong>Stage 3<\/strong> \u2014 GPG encryption before leaving the server<\/li>\n<li><strong>Stage 4<\/strong> \u2014 Off-site transfer via <code>rclone<\/code> to S3-compatible storage<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"stage-1-database\">Stage 1: Database Backup Script<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n# \/usr\/local\/bin\/backup-db.sh \u2014 Database backup for MySQL\/MariaDB\nset -euo pipefail\n\nDB_HOST=\"localhost\"\nDB_USER=\"backup_user\"\nDB_PASS=\"$(cat \/etc\/backup\/mysql_password)\"\nBACKUP_DIR=\"\/var\/backups\/db\"\nRETENTION_DAYS=30\nTIMESTAMP=$(date +%Y%m%d_%H%M%S)\n\nmkdir -p \"$BACKUP_DIR\"\n\n# List user databases, excluding system schemas\nDATABASES=$(mysql -h \"$DB_HOST\" -u \"$DB_USER\" -p\"$DB_PASS\" \\\n  -e \"SHOW DATABASES;\" | grep -Ev \"^(Database|information_schema|performance_schema|mysql|sys)$\")\n\nfor DB in $DATABASES; do\n  echo \"Backing up: $DB\"\n  mysqldump -h \"$DB_HOST\" -u \"$DB_USER\" -p\"$DB_PASS\" \\\n    --single-transaction --quick --lock-tables=false \\\n    --routines --triggers --events \"$DB\" | gzip > \"$BACKUP_DIR\/${DB}_${TIMESTAMP}.sql.gz\"\n\n  # Verify backup integrity\n  gzip -t \"$BACKUP_DIR\/${DB}_${TIMESTAMP}.sql.gz\" || {\n    echo \"Backup corrupted: ${DB}_${TIMESTAMP}.sql.gz\" | logger -t backup\n    rm -f \"$BACKUP_DIR\/${DB}_${TIMESTAMP}.sql.gz\"\n  }\ndone\n\n# Clean old backups\nfind \"$BACKUP_DIR\" -name \"*.sql.gz\" -mtime +$RETENTION_DAYS -delete\necho \"Database backup completed at $(date)\" | logger -t backup<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"stage-2-files\">Stage 2: Filesystem Archive<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n# \/usr\/local\/bin\/backup-files.sh \u2014 Filesystem backup with parallel compression\nset -euo pipefail\n\nBACKUP_DIR=\"\/var\/backups\/files\"\nSRC_DIRS=\"\/var\/www \/etc \/home\"\nEXCLUDE=\"--exclude=cache --exclude=tmp --exclude=.git\"\nTIMESTAMP=$(date +%Y%m%d_%H%M%S)\nRETENTION_DAYS=14\n\nmkdir -p \"$BACKUP_DIR\"\n\n# Use pigz for parallel gzip compression (multi-core)\ntar -I pigz -cf \"$BACKUP_DIR\/files_${TIMESTAMP}.tar.gz\" \\\n  $EXCLUDE $SRC_DIRS 2>\/dev\/null\n\n# Generate checksum for integrity verification\nsha256sum \"$BACKUP_DIR\/files_${TIMESTAMP}.tar.gz\" \\\n  > \"$BACKUP_DIR\/files_${TIMESTAMP}.tar.gz.sha256\"\n\nfind \"$BACKUP_DIR\" -name \"*.tar.gz\" -mtime +$RETENTION_DAYS -delete\nfind \"$BACKUP_DIR\" -name \"*.sha256\" -mtime +$RETENTION_DAYS -delete\necho \"Filesystem backup completed at $(date)\" | logger -t backup<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"stage-3-encryption\">Stage 3: Encryption and Off-Site Transfer<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n# \/usr\/local\/bin\/backup-encrypt-send.sh \u2014 Encrypt and transfer off-site\nset -euo pipefail\n\nGPG_RECIPIENT=\"backup@example.com\"\nREMOTE_PATH=\"my-backup-bucket:vps-backups\/$(hostname)\/\"\n\n# Encrypt database backups\nfor f in \/var\/backups\/db\/*.sql.gz; do\n  if [ -f \"$f\" ] && [ ! -f \"${f}.gpg\" ]; then\n    gpg --encrypt --recipient \"$GPG_RECIPIENT\" --output \"${f}.gpg\" \"$f\"\n    rm -f \"$f\"\n  fi\ndone\n\n# Encrypt filesystem archives\nfor f in \/var\/backups\/files\/*.tar.gz; do\n  if [ -f \"$f\" ] && [ ! -f \"${f}.gpg\" ]; then\n    gpg --encrypt --recipient \"$GPG_RECIPIENT\" --output \"${f}.gpg\" \"$f\"\n    rm -f \"$f\"\n  fi\ndone\n\n# Sync encrypted backups to S3-compatible storage via rclone\nrclone sync \/var\/backups\/ \"$REMOTE_PATH\" --progress --checksum\necho \"Off-site transfer completed at $(date)\" | logger -t backup<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"automation\">Step 4: Automate with Cron<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Schedule the pipeline in <code>\/etc\/crontab<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Database backup every 4 hours\n0 *\/4 * * * root \/usr\/local\/bin\/backup-db.sh\n\n# Filesystem backup daily at 2 AM\n0 2 * * * root \/usr\/local\/bin\/backup-files.sh\n\n# Encrypt and transfer to off-site daily at 4 AM\n0 4 * * * root \/usr\/local\/bin\/backup-encrypt-send.sh<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This shell-based backup pipeline runs entirely on your VPS with no third-party dependencies, encrypts data before transmission, and stores copies off-site for disaster recovery. Test your restore procedure monthly by spinning up a temporary VPS and restoring from the encrypted backup to verify integrity. Before provisioning backup storage infrastructure, <a href=\"https:\/\/virtualserversvps.com\/#providers\">see performance benchmarks on our comparison page<\/a> to select providers with sufficient transfer speeds and storage IOPS for your backup workload.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Pro tip:<\/strong> For the database backup user, grant only <code>SELECT, LOCK TABLES, SHOW VIEW, TRIGGER<\/code> privileges \u2014 never use root credentials in backup scripts. Store the GPG private key on an offline machine for recovery scenarios.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Data loss on a VPS is not a matter of if but when. Disk failure, accidental rm -rf, security breaches, or provider outages can wipe out critical data in seconds&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-586","post","type-post","status-publish","format-standard","hentry","category-vps-guides-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.1 (Yoast SEO v26.1) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>VPS Backup Pipeline: Automating Database and File Backups with Shell Scripts - Virtual Servers VPS Blog<\/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:\/\/virtualserversvps.com\/blog\/vps-backup-pipeline-automation-shell-scripts\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"VPS Backup Pipeline: Automating Database and File Backups with Shell Scripts\" \/>\n<meta property=\"og:description\" content=\"VPS Backup Pipeline: Automating Database and File Backups with Shell Scripts\" \/>\n<meta property=\"og:url\" content=\"https:\/\/virtualserversvps.com\/blog\/vps-backup-pipeline-automation-shell-scripts\/\" \/>\n<meta property=\"og:site_name\" content=\"Virtual Servers VPS Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-07-21T03:23:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-07-28T22:12:16+00:00\" \/>\n<meta name=\"author\" content=\"Virtual-Servers-Vps-Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Virtual-Servers-Vps-Editor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/vps-backup-pipeline-automation-shell-scripts\/\",\"url\":\"https:\/\/virtualserversvps.com\/blog\/vps-backup-pipeline-automation-shell-scripts\/\",\"name\":\"VPS Backup Pipeline: Automating Database and File Backups with Shell Scripts - Virtual Servers VPS Blog\",\"isPartOf\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#website\"},\"datePublished\":\"2026-07-21T03:23:57+00:00\",\"dateModified\":\"2026-07-28T22:12:16+00:00\",\"author\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0\"},\"breadcrumb\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/vps-backup-pipeline-automation-shell-scripts\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/virtualserversvps.com\/blog\/vps-backup-pipeline-automation-shell-scripts\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/vps-backup-pipeline-automation-shell-scripts\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/virtualserversvps.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"VPS Backup Pipeline: Automating Database and File Backups with Shell Scripts\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#website\",\"url\":\"https:\/\/virtualserversvps.com\/blog\/\",\"name\":\"Virtual Servers VPS Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/virtualserversvps.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0\",\"name\":\"Virtual-Servers-Vps-Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g\",\"caption\":\"Virtual-Servers-Vps-Editor\"},\"sameAs\":[\"https:\/\/virtualserversvps.com\/blog\"],\"url\":\"https:\/\/virtualserversvps.com\/blog\/author\/virtualserversvps\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"VPS Backup Pipeline: Automating Database and File Backups with Shell Scripts - Virtual Servers VPS Blog","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:\/\/virtualserversvps.com\/blog\/vps-backup-pipeline-automation-shell-scripts\/","og_locale":"en_US","og_type":"article","og_title":"VPS Backup Pipeline: Automating Database and File Backups with Shell Scripts","og_description":"VPS Backup Pipeline: Automating Database and File Backups with Shell Scripts","og_url":"https:\/\/virtualserversvps.com\/blog\/vps-backup-pipeline-automation-shell-scripts\/","og_site_name":"Virtual Servers VPS Blog","article_published_time":"2026-07-21T03:23:57+00:00","article_modified_time":"2026-07-28T22:12:16+00:00","author":"Virtual-Servers-Vps-Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Virtual-Servers-Vps-Editor","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/virtualserversvps.com\/blog\/vps-backup-pipeline-automation-shell-scripts\/","url":"https:\/\/virtualserversvps.com\/blog\/vps-backup-pipeline-automation-shell-scripts\/","name":"VPS Backup Pipeline: Automating Database and File Backups with Shell Scripts - Virtual Servers VPS Blog","isPartOf":{"@id":"https:\/\/virtualserversvps.com\/blog\/#website"},"datePublished":"2026-07-21T03:23:57+00:00","dateModified":"2026-07-28T22:12:16+00:00","author":{"@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0"},"breadcrumb":{"@id":"https:\/\/virtualserversvps.com\/blog\/vps-backup-pipeline-automation-shell-scripts\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/virtualserversvps.com\/blog\/vps-backup-pipeline-automation-shell-scripts\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/virtualserversvps.com\/blog\/vps-backup-pipeline-automation-shell-scripts\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/virtualserversvps.com\/blog\/"},{"@type":"ListItem","position":2,"name":"VPS Backup Pipeline: Automating Database and File Backups with Shell Scripts"}]},{"@type":"WebSite","@id":"https:\/\/virtualserversvps.com\/blog\/#website","url":"https:\/\/virtualserversvps.com\/blog\/","name":"Virtual Servers VPS Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/virtualserversvps.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0","name":"Virtual-Servers-Vps-Editor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g","caption":"Virtual-Servers-Vps-Editor"},"sameAs":["https:\/\/virtualserversvps.com\/blog"],"url":"https:\/\/virtualserversvps.com\/blog\/author\/virtualserversvps\/"}]}},"_links":{"self":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/586","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/comments?post=586"}],"version-history":[{"count":3,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/586\/revisions"}],"predecessor-version":[{"id":739,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/586\/revisions\/739"}],"wp:attachment":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/media?parent=586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/categories?post=586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/tags?post=586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}