{"id":285,"date":"2020-06-12T17:27:38","date_gmt":"2020-06-12T17:27:38","guid":{"rendered":"https:\/\/shreyapohekar.com\/blogs\/?p=285"},"modified":"2020-06-12T17:30:22","modified_gmt":"2020-06-12T17:30:22","slug":"automatic-backup-of-folder-contents-on-github","status":"publish","type":"post","link":"https:\/\/shreyapohekar.com\/blogs\/automatic-backup-of-folder-contents-on-github\/","title":{"rendered":"Automatic backup of folder contents on Github"},"content":{"rendered":"\n<p>We generate enormous amount of data everyday working with virtual machines. Several file are so important that one cant afford to loose them. But what if, oneday you realize that you virtual machine has crashed due to some reason and all your data is gone forever. Seems like a nightmare that no-one wanna encounter.<\/p>\n\n\n\n<p>Most of us take file backup on <a href=\"https:\/\/github.com\/\">github<\/a>, but doing it manually all the time is so cumbersome.<\/p>\n\n\n\n<p>In this post, I am gonna show you how can take your important folder backups on git using cron jobs in linux. The cron can be configured to run every hour so that all your important work is safely backed up.<\/p>\n\n\n\n<p>Let&#8217;s get started!!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create a new repository<\/h2>\n\n\n\n<p>Create a new repository on github (it can be public or private)<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"938\" height=\"694\" src=\"https:\/\/shreyapohekar.com\/blogs\/wp-content\/uploads\/2020\/06\/image.png\" alt=\"\" class=\"wp-image-286\" srcset=\"https:\/\/shreyapohekar.com\/blogs\/wp-content\/uploads\/2020\/06\/image.png 938w, https:\/\/shreyapohekar.com\/blogs\/wp-content\/uploads\/2020\/06\/image-300x222.png 300w, https:\/\/shreyapohekar.com\/blogs\/wp-content\/uploads\/2020\/06\/image-768x568.png 768w\" sizes=\"(max-width: 938px) 100vw, 938px\" \/><\/figure>\n\n\n\n<p>The next page will ask you  to do a quick setup. Switch the tab to<strong> ssh url.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"975\" height=\"375\" src=\"https:\/\/shreyapohekar.com\/blogs\/wp-content\/uploads\/2020\/06\/image-1.png\" alt=\"\" class=\"wp-image-287\" srcset=\"https:\/\/shreyapohekar.com\/blogs\/wp-content\/uploads\/2020\/06\/image-1.png 975w, https:\/\/shreyapohekar.com\/blogs\/wp-content\/uploads\/2020\/06\/image-1-300x115.png 300w, https:\/\/shreyapohekar.com\/blogs\/wp-content\/uploads\/2020\/06\/image-1-768x295.png 768w\" sizes=\"(max-width: 975px) 100vw, 975px\" \/><figcaption>setup<\/figcaption><\/figure>\n\n\n\n<p>Go to your linux machine and install git using:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted wpf-blue-background\"># apt install git<\/pre>\n\n\n\n<p>Now got to the parent folder, whose backup has to be taken on git and run the following set of commands.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted wpf-blue-background\"># git init\n# git add README.md\n# git commit -m \"first commit\"\n# git remote add origin git@github.com:shreya2201\/my-repo.git\n# git push -u origin master<\/pre>\n\n\n\n<p>This will create a .git folder inside that folder. And we are trying to do our first commit. We the git remote add command is run, the user is asked to enter the username and password of the github account. So when automating, we need to get rid of it. This is where  <strong>ssh keys<\/strong> are handy.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Configure ssh keys<\/h2>\n\n\n\n<p>Since we are atomating the process of backup, we need <a href=\"https:\/\/www.ssh.com\/ssh\/\">ssh<\/a> keys to login to our git account. And as a first step, ssh keys have to be generated. This can be done using <strong>ssh-keygen<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted wpf-blue-background\"># ssh-keygen -t rsa -b 4096 -C \"your mail id attached on github\"<\/pre>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"265\" src=\"https:\/\/lh5.googleusercontent.com\/YvtDFHqAwi9oG541F8yju_auPnBAE6JtvkiFWMLNqLKCow18tgj-KjYsHpFuYSJOL_T7D47BX3saUWMhLsLGwfU0qZP5Vxh-3axM70qQzmHls1Z9S0tBsADK6dcdk_MWgbcvYVq7\"><\/p>\n\n\n\n<p>The keys generated will be stored inside <strong>\/root\/.ssh<\/strong> with names id_rsa_git and id_rsa_git.pub<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>cat the contents of  id_rsa_git.pub and copy to  clipboard.<\/li><li>Go to github.com and login with your creds.<\/li><li>Got to your account settings and click on ssh and gpg keys.<\/li><\/ol>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"307\" src=\"https:\/\/lh5.googleusercontent.com\/PY023mgVi9k9KXkoIz1sQ9ZBN6Ksnsl_DFw7kGrreicvn-KMaQyNhQyewr8W3dtcpYYIgk7xRqkCd6lvXzMB4UaQDCMRvuSLlbPRT2aJJf-2JZBjCgw85YhN7lwrRGjxLA_TDbQp\"><\/p>\n\n\n\n<p>Paste the public key (copied earlier) here and give your key a name. ssh key s can be specifically configured for individual repos as well. But from here, the keys will work with any repo you want.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">SSH configuration<\/h2>\n\n\n\n<p>Now again get back to your linux machine and setup the <strong>ssh configuration file.<\/strong><\/p>\n\n\n\n<p>open <strong>\/root\/.ssh\/config<\/strong> file. Create config inside .ssh, it is not present. Put the following contents to the file<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"71\" src=\"https:\/\/lh6.googleusercontent.com\/FiGGeVx3lQVtkElXkQLARYibdO_Xq8BwFBcFp0sPpUpjtEl7DxqPtvnEMdpDwQaTB3UQqEE-HuqFHmQtmOVQ_8bQB22thKemwyoJMFJwe6saJ8YtftxQAACWZphGOWndqEuG33Hf\">host is github.com and path to private key has to be given.<\/p>\n\n\n\n<p>To test whether the ssh keys are working or not, run the following:<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/bQp4okENsmViBy6g9q2Yp3-J8SddbFXWsOf8URFRLMQ-H767Ai6jKN_KIKjjrnj8-9WeI0cxbKM8OYQEnYVJ_PdXVAPwinTUmoZT2X7F_ytFW2qEuzIlAMJ5hC-4VAmrJD4g1-3p\" width=\"624\" height=\"87\"><\/p>\n\n\n\n<p>Cool! I am now able to authenticate. Next up, we need to set the url to ssh git@github.com:username\/my-repo.git( the default url is set to https). Run git push command on the folder to check if everything is working fine!!<\/p>\n\n\n\n<pre class=\"wp-block-preformatted wpf-blue-background\">\u26a1 root@kali\u00a0 ~\/Desktop\/htb > git remote set-url origin git@github.com:username\/my-repo.git<br>\u26a1 root@kali  ~\/Desktop\/htb >  git push origin master<\/pre>\n\n\n\n<p>I am able to push to git without requiring any password, so the <a href=\"https:\/\/docs.oracle.com\/cloud\/latest\/big-data-discovery-cloud\/BDDDP\/tdp_updates_cron.htm#BDDDP-task_202F7B2F63BB4F32965313FE59CC13FF\">cron job<\/a> can be set up now.<\/p>\n\n\n\n<p>Create a script commit.sh inside \/root and add the following contents<\/p>\n\n\n\n<pre class=\"wp-block-code wpf-blue-background\"><code># Git: add and commit changes\ncd \/root\/Desktop\/htb &amp;&amp; \/usr\/bin\/git add --all &amp;&amp; \/usr\/bin\/git commit -m \"daily crontab backup `date`\"\n\n# send data to Git server\ncd \/root\/Desktop\/htb &amp;&amp; \/usr\/bin\/git push origin master<\/code><\/pre>\n\n\n\n<p>On the terminal run<\/p>\n\n\n\n<pre class=\"wp-block-preformatted wpf-blue-background\"># chmod +x \/root\/commit.sh<\/pre>\n\n\n\n<p>To create a cron job, run crontab -e and add the following contents to the file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted wpf-blue-background\"># crontab -e\n         *\/30 * * * * \/root\/commit.sh<\/pre>\n\n\n\n<p>In a nutshell, this cron job will be running every 30 minutes and forever.<\/p>\n\n\n\n<p>Make sure that the cron service is running. If not, run<\/p>\n\n\n\n<pre class=\"wp-block-preformatted wpf-blue-background\"># systemctl start cron\n# systemctl enable cron<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">If file size is more than 100 Mb<\/h2>\n\n\n\n<p>There might be instances when the file inside your folder has size greater than 100Mb. Presence of such files can lead to failed git push.<\/p>\n\n\n\n<p>Error will look something like this<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">remote: error: GH001: Large files detected. You may want to try Git Large File Storage - https:\/\/git-lfs.github.com.\nremote: error: Trace: 204cde5b555213065bf8a915ca39bcda\nremote: error: See http:\/\/git.io\/iEPt8g for more information.\nremote: error: File node\/hydra.restore is 147.14 MB; this exceeds GitHub's file size limit of 100.00 MB<\/pre>\n\n\n\n<p>Here, hydra.restore is a file inside folder node that is causing the error. So here&#8217;s a trick you can use to get rid of errors and do a successful git push<\/p>\n\n\n\n<pre class=\"wp-block-preformatted wpf-blue-background\"># git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch node\/hydra.restore'<\/pre>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/2hXJl1-D2kVNtfU_4UJFeQK9MXvNk5gkC3fnKD0J69xygvur7LtqezpKkjEwHVoZTl6nEqfscoom_JiZFs_-9dcR_xXzeXOVMBsT8b9loW6-e7MSVMIXVIAxzgxhsdcCK9HsnkMj\" style=\"width: undefinedpx;\"><\/p>\n\n\n\n<p>Thats all for the blog post!! I hope you got what you needed! For much content, visit  <a href=\"https:\/\/shreyapohekar.com\/blogs\">here<\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>We generate enormous amount of data everyday working with virtual machines. Several file are so important that one cant afford to loose them. But what if, oneday you realize that you virtual machine has crashed due to some reason and all your data is gone forever. Seems like a nightmare that no-one wanna encounter. Most [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":293,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"","ocean_second_sidebar":"","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"","ocean_custom_header_template":"","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"","ocean_menu_typo_font_family":"","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"on","ocean_gallery_id":[],"footnotes":""},"categories":[142,141],"tags":[144,152,143,145,149,148,146,150,147],"class_list":["post-285","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-github","category-linux","tag-automation-with-github","tag-backup","tag-gihub","tag-git","tag-git-pull","tag-git-push","tag-ssh","tag-ssh-url-github","tag-ssh-keygen","entry","has-media"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/shreyapohekar.com\/blogs\/wp-json\/wp\/v2\/posts\/285"}],"collection":[{"href":"https:\/\/shreyapohekar.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shreyapohekar.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shreyapohekar.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shreyapohekar.com\/blogs\/wp-json\/wp\/v2\/comments?post=285"}],"version-history":[{"count":5,"href":"https:\/\/shreyapohekar.com\/blogs\/wp-json\/wp\/v2\/posts\/285\/revisions"}],"predecessor-version":[{"id":294,"href":"https:\/\/shreyapohekar.com\/blogs\/wp-json\/wp\/v2\/posts\/285\/revisions\/294"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/shreyapohekar.com\/blogs\/wp-json\/wp\/v2\/media\/293"}],"wp:attachment":[{"href":"https:\/\/shreyapohekar.com\/blogs\/wp-json\/wp\/v2\/media?parent=285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shreyapohekar.com\/blogs\/wp-json\/wp\/v2\/categories?post=285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shreyapohekar.com\/blogs\/wp-json\/wp\/v2\/tags?post=285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}