{"id":21237,"date":"2020-05-05T10:00:00","date_gmt":"2020-05-05T14:00:00","guid":{"rendered":"https:\/\/osharaprd.wpenginepowered.com\/?p=21237"},"modified":"2024-10-22T16:21:34","modified_gmt":"2024-10-22T20:21:34","slug":"back-up-your-system-using-a-simple-shell-script","status":"publish","type":"post","link":"https:\/\/oshara.ca\/en\/back-up-your-system-using-a-simple-shell-script\/","title":{"rendered":"Back Up Your System Using a Simple Shell Script"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">This is a super easy way to back up your system without spending a dime. It can really come in handy, as backup is always important.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Someone said &#8220;When your data doesn&#8217;t exist three times, it means it doesn&#8217;t exist at all.&#8221; So make backups and save your backups.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Here we are:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/\n\ndate\n\necho \"############### Backup of files on my server ###############\"\n\nbackupfilename=server_backup_`date '+%Y-%m-%d'`\n\necho \"----- Sql first and dumping current DB -----\"\n\nrm -f \/tmp\/backup.sql\n\nmysqldump --user=youruser --password=yourpassword --all-databases --add-drop-table &gt; \/tmp\/backup.sql\n\necho \"----- Tar plus zip all files -----\"\n\ntar cvf \/home\/mybackups\/files\/${backupfilename}.tar \/home\/www\/html\/* \/tmp\/backup.sql\n\ngzip \/home\/mybackups\/files\/${backupfilename}.tar\n\nchmod 666 \/home\/mybackups\/files\/${backupfilename}.tar.gz\n\necho \"############### Backup done successfully.... ###############\"\n\ndate\n\n***<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Now let&#8217;s explain each line:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">date =&gt; we want to see the server date in our log file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">backupfilename=server_backup_`date &#8216;+%Y-%m-%d&#8217;` =&gt; backup file name with date<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">rm -f \/tmp\/backup.sql ==&gt; we delete the old generated sql backup file<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">mysqldump =&gt; we backup all sql databases again<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">tar cvf home\/www\/html\/ \/tmp\/backup.sql =&gt; You back up everything home\/www\/html\/* and \/tmp\/backup.sql You can also exclude certain paths with: \u2013exclude=&#8217;yourfilepath&#8217;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Now let&#8217;s run the script<\/strong>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Name your file &#8216;backup.sh&#8217;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To run it, you need to open the terminal prompt and start with a dot &#8216;.&#8217;. So if you name your file backup.sh, you will get .backup.sh.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now we&#8217;re going to automate it because you probably don&#8217;t want to do it manually every day.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So if you want to run it automatically every day at 11:55 p.m., you open your crontab with crontab -e and add this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>55 23 * * * \/path\/toyourfile\/backup.sh &gt;&gt; \/path\/toyourlogfile\/backups.log<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And, close your crontab terminal with \u201c:x\u201d<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>NOTE:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&gt;&gt; \/path\/toyourlogfile\/mybackups.log is not required, it is only necessary if you want to be able to see your backup log in case of an error.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to set a different time in your cron I recommend using this tool which is super easy and will get the job done.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In a Linux environment, I use a Bash backup script to specify the directory I want to archive, copy the files to a backup location, and maintain a reliable backup of my file system.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you are seeking assistance with custom web development, please&nbsp;<a href=\"https:\/\/oshara.ca\/en\/contact\/\" target=\"_blank\" rel=\"noopener\" title=\"\">contact our web development agency<\/a>. We have over a decade of experience in web development and we are the best web agency in Montreal.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a super easy way to back up your system without spending a dime. It can really come in handy, as backup is always important. Someone said &#8220;When your data doesn&#8217;t exist three times, it means it doesn&#8217;t exist at all.&#8221; So make backups and save your backups. Here we are: Now let&#8217;s explain [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":21307,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[26],"tags":[378,377,412],"class_list":["post-21237","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology","tag-backupyoursystem","tag-simpleshellscript","tag-website-development"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/oshara.ca\/en\/wp-json\/wp\/v2\/posts\/21237","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oshara.ca\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oshara.ca\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oshara.ca\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/oshara.ca\/en\/wp-json\/wp\/v2\/comments?post=21237"}],"version-history":[{"count":0,"href":"https:\/\/oshara.ca\/en\/wp-json\/wp\/v2\/posts\/21237\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/oshara.ca\/en\/wp-json\/wp\/v2\/media\/21307"}],"wp:attachment":[{"href":"https:\/\/oshara.ca\/en\/wp-json\/wp\/v2\/media?parent=21237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oshara.ca\/en\/wp-json\/wp\/v2\/categories?post=21237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oshara.ca\/en\/wp-json\/wp\/v2\/tags?post=21237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}