{"id":12,"date":"2009-08-08T16:03:06","date_gmt":"2009-08-08T16:03:06","guid":{"rendered":"http:\/\/www.adriangrigoras.com\/blog\/?p=12"},"modified":"2009-08-08T16:03:06","modified_gmt":"2009-08-08T16:03:06","slug":"simple-backup-in-linux","status":"publish","type":"post","link":"https:\/\/adriangrigoras.com\/blog\/simple-backup-in-linux\/","title":{"rendered":"Simple backup in Linux"},"content":{"rendered":"<p>Sometimes, when you writing code you make a lot of mistakes and you want to go back to a previous version, but you don&#8217;t have one. It&#8217;s recommended that you always back-up your data.<\/p>\n<p>I tried a lot of back-up solutions, but they didn&#8217;t satisfy what I need. I need to make a backup to a certain moment and to have permission to read data.<\/p>\n<p>So, I started to write a simple backup program. Here it is:<\/p>\n<p>First, we need to write a Perl script which will tar and gzip some data and also write to a log file<\/p>\n<blockquote>\n<address>!# \/usr\/bin\/perl<br \/>\nprint &#8220;Starting..n&#8221;;<br \/>\n($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();<br \/>\n$year=1900+$yearOffset;<br \/>\n$x=int(rand(100));<br \/>\n$time=&#8221;$year-$month-$dayOfMonth-$hour$minute$second&#8221;;<br \/>\nopen (FILE, &#8220;&gt;&gt;&#8221;, &#8220;$ARGV[0]-backup.log&#8221;);<br \/>\nprint FILE &#8220;Start back-up $timen&#8221;;<br \/>\n&amp;run(&#8220;tar -cf $ARGV[0]-$time.tar $ARGV[1]&#8221;);<br \/>\n&amp;run(&#8220;gzip $ARGV[0]-$time.tar&#8221;);<br \/>\n&amp;run(&#8220;chmod 777 $ARGV[0]-$time.tar.gz&#8221;);<br \/>\nprint FILE &#8220;Finished back-up $timenn &#8220;;<br \/>\nclose (FILE);<br \/>\nprint &#8220;Finished!nn&#8221;;<br \/>\nsub run(){<br \/>\n# print &#8220;&#8221;;<br \/>\n@args = ($_[0]);<br \/>\nsystem(@args) == 0 or die &#8220;system @args failed: $?&#8221;;<\/p>\n<p>if ($? == -1) {<br \/>\nprint &#8220;failed to execute: $!n&#8221;;<br \/>\n}<br \/>\nelsif ($? &amp; 127) {<br \/>\nprintf &#8220;child died with signal %d, %s coredumpn&#8221;,<br \/>\n($? &amp; 127),\u00a0 ($? &amp; 128) ? &#8216;with&#8217; : &#8216;without&#8217;;<br \/>\n}<br \/>\nelse {<br \/>\n#printf &#8220;child exited with value %dn&#8221;, $? &gt;&gt; 8;<br \/>\n}<br \/>\n}<\/p>\n<\/address>\n<address>1;<\/address>\n<\/blockquote>\n<p>After this we need a bash file which will be a cronjob<\/p>\n<blockquote>\n<address>#!\/bin\/bash<br \/>\necho -n &#8220;Running backup.cgi&#8221;<br \/>\ncd $(dirname $0)<br \/>\nperl $(dirname $0)\/backup.cgi $@<br \/>\nexit 0<\/address>\n<\/blockquote>\n<p>Copy those scripts to a directory and chmod 755 them to be executable:<\/p>\n<blockquote>\n<address>chmod 755 backup.cgi<\/address>\n<address>chmod 755 backup.sh<br \/>\n<\/address>\n<\/blockquote>\n<p>Finally, we add those cron jobs. Because I need to backup some files that my user don&#8217;t have permission I insert those cron jobs into root&#8217;s crontab:<\/p>\n<blockquote>\n<address># crontab -e<\/address>\n<address>0 * * * * \/home\/adrian\/backup\/backup.sh apache \/srv\/www<br \/>\n15 * * * * \/home\/adrian\/backup\/backup.sh apache \/srv\/www<br \/>\n30 * * * * \/home\/adrian\/backup\/backup.sh apache \/srv\/www<br \/>\n45 * * * * \/home\/adrian\/backup\/backup.sh apache \/srv\/www<br \/>\n0 * * * * \/home\/adrian\/backup\/backup.sh mysql \/var\/lib\/mysq<br \/>\n<\/address>\n<\/blockquote>\n<p>This will backup the content of Apache server every 15 minutes and the MySQL databases every hour.<\/p>\n<p>The syntax for crontab is the following:<\/p>\n<address>.&#8212;&#8212;&#8212;&#8212;&#8212;- minute (0 &#8211; 59) <\/address>\n<address>|  .&#8212;&#8212;&#8212;&#8212;- hour (0 &#8211; 23)<\/address>\n<address>|  |  .&#8212;&#8212;&#8212;- day of month (1 &#8211; 31)<\/address>\n<address>|  |  |  .&#8212;&#8212;- month (1 &#8211; 12) OR jan,feb,mar,apr &#8230; <\/address>\n<address>|  |  |  |  .&#8212;- day of week (0 &#8211; 6) (Sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat <\/address>\n<address>|  |  |  |  | <\/address>\n<address>*  *  *  *  *  command to be executed &gt; file_where_to_write_output<\/address>\n<address> <\/address>\n<p>I hope you find useful this program!<\/p>\n<div id=\"_mcePaste\" style=\"overflow: hidden; position: absolute; left: -10000px; top: 1009px; width: 1px; height: 1px;\">#!\/bin\/bash<\/p>\n<p>echo -n &#8220;Running backup.cgi&#8221;<\/p>\n<p>cd $(dirname $0)<\/p>\n<p>perl $(dirname $0)\/backup.cgi $@<\/p>\n<p>exit 0<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, when you writing code you make a lot of mistakes and you want to go back to a previous version, but you don&#8217;t have one. It&#8217;s recommended that you always back-up your data. I tried a lot of back-up solutions, but they didn&#8217;t satisfy what I need. I need to make a backup to\u2026 <span class=\"read-more\"><a href=\"https:\/\/adriangrigoras.com\/blog\/simple-backup-in-linux\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,11,14],"tags":[],"class_list":["post-12","post","type-post","status-publish","format-standard","hentry","category-bash","category-linux","category-perl"],"_links":{"self":[{"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/posts\/12","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/comments?post=12"}],"version-history":[{"count":0,"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/posts\/12\/revisions"}],"wp:attachment":[{"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/media?parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/categories?post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/adriangrigoras.com\/blog\/wp-json\/wp\/v2\/tags?post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}