<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>script to automate Archives - CNERIS</title>
	<atom:link href="https://cneris.com/en/tag/script-to-automate/feed/" rel="self" type="application/rss+xml" />
	<link>https://cneris.com/en/tag/script-to-automate/</link>
	<description></description>
	<lastBuildDate>Fri, 23 Aug 2024 20:59:51 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>
	<item>
		<title>How to Create a Script to Automate Backups on a Dedicated Server with Multiple Vhosts</title>
		<link>https://cneris.com/en/how-to-create-a-script-to-automate-backups-on-a-dedicated-server-with-multiple-vhosts/</link>
					<comments>https://cneris.com/en/how-to-create-a-script-to-automate-backups-on-a-dedicated-server-with-multiple-vhosts/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Fri, 23 Aug 2024 20:58:57 +0000</pubDate>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[System Administration]]></category>
		<category><![CDATA[automate backups]]></category>
		<category><![CDATA[dedicated server]]></category>
		<category><![CDATA[script to automate]]></category>
		<guid isPermaLink="false">https://cneris.com/?p=2003</guid>

					<description><![CDATA[<p>Automating backups on a dedicated server hosting multiple websites via vhosts is essential to ensure data security and availability. In this article, you'll learn how to create a Bash script to automate the backup process for different websites hosted on a server with vhosts. I'll include code examples so you can implement it easily. Step [...]</p>
<p>The post <a href="https://cneris.com/en/how-to-create-a-script-to-automate-backups-on-a-dedicated-server-with-multiple-vhosts/">How to Create a Script to Automate Backups on a Dedicated Server with Multiple Vhosts</a> appeared first on <a href="https://cneris.com/en">CNERIS</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Automating backups on a dedicated server hosting multiple websites via vhosts is essential to ensure data security and availability. In this article, you&#8217;ll learn how to create a Bash script to automate the backup process for different websites hosted on a server with vhosts. I&#8217;ll include code examples so you can implement it easily.</p>
<p><strong>Step 1: Setting Up the Environment</strong></p>
<p>Before you start writing the script, make sure your server has SSH access configured and that you have root or sudo privileges. Additionally, you&#8217;ll need a directory where the backups will be stored.</p>
<p><strong>Step 2: Creating the Backup Script</strong></p>
<p>Below is an example of a Bash script that automates backups of the vhosts. This script will compress the files of each website and store them in a backup directory.</p>
<div class="dark bg-gray-950 rounded-md border-[0.5px] border-token-border-medium">
<div class="flex items-center relative text-token-text-secondary bg-token-main-surface-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md"></div>
<div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-bash"><span class="hljs-meta">#!/bin/bash</span><br />
</code></div>
<div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-bash"><span class="hljs-comment"># Directory where backups will be stored</span><br />
backup_dir=<span class="hljs-string">"/var/backups"</span><br />
</code></div>
<div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-bash"><span class="hljs-comment"># Current date</span><br />
<span class="hljs-built_in">date</span>=$(<span class="hljs-built_in">date</span> +<span class="hljs-string">'%Y-%m-%d'</span>)<br />
</code></div>
<div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-bash"><code class="!whitespace-pre hljs language-bash"><span class="hljs-comment"># Vhosts directory</span><br />
vhosts_dir=<span class="hljs-string">"/etc/apache2/sites-available"</span></code></code><span class="hljs-comment"># Create backup directory if it doesn&#8217;t exist</span><br />
<span class="hljs-built_in">mkdir</span> -p <span class="hljs-string">&#8220;<span class="hljs-variable">$backup_dir</span>/<span class="hljs-variable">$date</span>&#8220;</span></p>
</div>
<div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-bash"><span class="hljs-comment"># Loop through each vhost and perform the backup</span><br />
</code></div>
<div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-bash"><span class="hljs-keyword">for</span> vhost <span class="hljs-keyword">in</span> $(<span class="hljs-built_in">ls</span> <span class="hljs-string">"<span class="hljs-variable">$vhosts_dir</span>"</span>); </code><code class="!whitespace-pre hljs language-bash"><span class="hljs-keyword">do</span><br />
</code></div>
<div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-bash">    site_name=$(<span class="hljs-built_in">basename</span> <span class="hljs-string">"<span class="hljs-variable">$vhost</span>"</span> .conf)<br />
</code></div>
<div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-bash">    tar -czf <span class="hljs-string">"<span class="hljs-variable">$backup_dir</span>/<span class="hljs-variable">$date</span>/<span class="hljs-variable">$site_name</span>.tar.gz"</span> <span class="hljs-string">"/var/www/<span class="hljs-variable">$site_name</span>"</span><br />
</code></div>
<div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-bash"><span class="hljs-keyword">done</span><br />
</code></div>
</div>
<p><strong>Step 3: Scheduling the Script</strong></p>
<p>To have the script run automatically, you can schedule it using cron. To edit the cron, run the following command:</p>
<div class="dark bg-gray-950 rounded-md border-[0.5px] border-token-border-medium">
<div class="flex items-center relative text-token-text-secondary bg-token-main-surface-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md"></div>
<div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-bash">$ crontab -e<br />
</code></div>
</div>
<p>Add the following line to have the script run daily at 2 AM:</p>
<div class="dark bg-gray-950 rounded-md border-[0.5px] border-token-border-medium">
<div class="flex items-center relative text-token-text-secondary bg-token-main-surface-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md"></div>
<div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-javascript"><span class="hljs-number">0</span> <span class="hljs-number">2</span> * * * <span class="hljs-regexp">/path/</span>to/script/backup_script.<span class="hljs-property">sh</span><br />
</code></div>
</div>
<p><strong>Conclusion:</strong></p>
<p>Automating backups of your vhosts is an excellent way to ensure your data is protected without the need for manual intervention. This script provides a simple and effective solution for maintaining regular backups of your websites.</p>
<p>The post <a href="https://cneris.com/en/how-to-create-a-script-to-automate-backups-on-a-dedicated-server-with-multiple-vhosts/">How to Create a Script to Automate Backups on a Dedicated Server with Multiple Vhosts</a> appeared first on <a href="https://cneris.com/en">CNERIS</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://cneris.com/en/how-to-create-a-script-to-automate-backups-on-a-dedicated-server-with-multiple-vhosts/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
