├── .gitignore ├── CONTRIBUTING.md ├── LICENSE.txt ├── README.md ├── agb_logo.png ├── auto-gitlab-backup.conf.sample ├── auto-gitlab-backup.sh └── restoreGitLab.sh /.gitignore: -------------------------------------------------------------------------------- 1 | ## files that shouldn't be synced 2 | auto-gitlab-backup.conf 3 | *~ 4 | 5 | .DS_Store -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to contribute 2 | 3 | _Unless you hear different, fork and MR._ 4 | 5 | ## Things to keep in mind: 6 | - keep this in ```bash``` 7 | - keep it neat and (fairly) easy to read 8 | - Merge requests will be merged to ```develop``` to fix typos and/or bugs. 9 | - Merge requests with a feature-focus will be merged into a different develop branch for testing. (as much as I can... your environment/OS may be different.) 10 | - Try and keep typo and bugs fixes sepereate from feature MRs. 11 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | GNU GENERAL PUBLIC LICENSE 3 | Version 2, June 1991 4 | 5 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., [http://fsf.org/] 6 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 7 | Everyone is permitted to copy and distribute verbatim copies 8 | of this license document, but changing it is not allowed. 9 | 10 | Preamble 11 | 12 | The licenses for most software are designed to take away your 13 | freedom to share and change it. By contrast, the GNU General Public 14 | License is intended to guarantee your freedom to share and change free 15 | software--to make sure the software is free for all its users. This 16 | General Public License applies to most of the Free Software 17 | Foundation's software and to any other program whose authors commit to 18 | using it. (Some other Free Software Foundation software is covered by 19 | the GNU Lesser General Public License instead.) You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | this service if you wish), that you receive source code or can get it 26 | if you want it, that you can change the software or use pieces of it 27 | in new free programs; and that you know you can do these things. 28 | 29 | To protect your rights, we need to make restrictions that forbid 30 | anyone to deny you these rights or to ask you to surrender the rights. 31 | These restrictions translate to certain responsibilities for you if you 32 | distribute copies of the software, or if you modify it. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must give the recipients all the rights that 36 | you have. You must make sure that they, too, receive or can get the 37 | source code. And you must show them these terms so they know their 38 | rights. 39 | 40 | We protect your rights with two steps: (1) copyright the software, and 41 | (2) offer you this license which gives you legal permission to copy, 42 | distribute and/or modify the software. 43 | 44 | Also, for each author's protection and ours, we want to make certain 45 | that everyone understands that there is no warranty for this free 46 | software. If the software is modified by someone else and passed on, we 47 | want its recipients to know that what they have is not the original, so 48 | that any problems introduced by others will not reflect on the original 49 | authors' reputations. 50 | 51 | Finally, any free program is threatened constantly by software 52 | patents. We wish to avoid the danger that redistributors of a free 53 | program will individually obtain patent licenses, in effect making the 54 | program proprietary. To prevent this, we have made it clear that any 55 | patent must be licensed for everyone's free use or not licensed at all. 56 | 57 | The precise terms and conditions for copying, distribution and 58 | modification follow. 59 | 60 | GNU GENERAL PUBLIC LICENSE 61 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 62 | 63 | 0. This License applies to any program or other work which contains 64 | a notice placed by the copyright holder saying it may be distributed 65 | under the terms of this General Public License. The "Program", below, 66 | refers to any such program or work, and a "work based on the Program" 67 | means either the Program or any derivative work under copyright law: 68 | that is to say, a work containing the Program or a portion of it, 69 | either verbatim or with modifications and/or translated into another 70 | language. (Hereinafter, translation is included without limitation in 71 | the term "modification".) Each licensee is addressed as "you". 72 | 73 | Activities other than copying, distribution and modification are not 74 | covered by this License; they are outside its scope. The act of 75 | running the Program is not restricted, and the output from the Program 76 | is covered only if its contents constitute a work based on the 77 | Program (independent of having been made by running the Program). 78 | Whether that is true depends on what the Program does. 79 | 80 | 1. You may copy and distribute verbatim copies of the Program's 81 | source code as you receive it, in any medium, provided that you 82 | conspicuously and appropriately publish on each copy an appropriate 83 | copyright notice and disclaimer of warranty; keep intact all the 84 | notices that refer to this License and to the absence of any warranty; 85 | and give any other recipients of the Program a copy of this License 86 | along with the Program. 87 | 88 | You may charge a fee for the physical act of transferring a copy, and 89 | you may at your option offer warranty protection in exchange for a fee. 90 | 91 | 2. You may modify your copy or copies of the Program or any portion 92 | of it, thus forming a work based on the Program, and copy and 93 | distribute such modifications or work under the terms of Section 1 94 | above, provided that you also meet all of these conditions: 95 | 96 | a) You must cause the modified files to carry prominent notices 97 | stating that you changed the files and the date of any change. 98 | 99 | b) You must cause any work that you distribute or publish, that in 100 | whole or in part contains or is derived from the Program or any 101 | part thereof, to be licensed as a whole at no charge to all third 102 | parties under the terms of this License. 103 | 104 | c) If the modified program normally reads commands interactively 105 | when run, you must cause it, when started running for such 106 | interactive use in the most ordinary way, to print or display an 107 | announcement including an appropriate copyright notice and a 108 | notice that there is no warranty (or else, saying that you provide 109 | a warranty) and that users may redistribute the program under 110 | these conditions, and telling the user how to view a copy of this 111 | License. (Exception: if the Program itself is interactive but 112 | does not normally print such an announcement, your work based on 113 | the Program is not required to print an announcement.) 114 | 115 | These requirements apply to the modified work as a whole. If 116 | identifiable sections of that work are not derived from the Program, 117 | and can be reasonably considered independent and separate works in 118 | themselves, then this License, and its terms, do not apply to those 119 | sections when you distribute them as separate works. But when you 120 | distribute the same sections as part of a whole which is a work based 121 | on the Program, the distribution of the whole must be on the terms of 122 | this License, whose permissions for other licensees extend to the 123 | entire whole, and thus to each and every part regardless of who wrote it. 124 | 125 | Thus, it is not the intent of this section to claim rights or contest 126 | your rights to work written entirely by you; rather, the intent is to 127 | exercise the right to control the distribution of derivative or 128 | collective works based on the Program. 129 | 130 | In addition, mere aggregation of another work not based on the Program 131 | with the Program (or with a work based on the Program) on a volume of 132 | a storage or distribution medium does not bring the other work under 133 | the scope of this License. 134 | 135 | 3. You may copy and distribute the Program (or a work based on it, 136 | under Section 2) in object code or executable form under the terms of 137 | Sections 1 and 2 above provided that you also do one of the following: 138 | 139 | a) Accompany it with the complete corresponding machine-readable 140 | source code, which must be distributed under the terms of Sections 141 | 1 and 2 above on a medium customarily used for software interchange; or, 142 | 143 | b) Accompany it with a written offer, valid for at least three 144 | years, to give any third party, for a charge no more than your 145 | cost of physically performing source distribution, a complete 146 | machine-readable copy of the corresponding source code, to be 147 | distributed under the terms of Sections 1 and 2 above on a medium 148 | customarily used for software interchange; or, 149 | 150 | c) Accompany it with the information you received as to the offer 151 | to distribute corresponding source code. (This alternative is 152 | allowed only for noncommercial distribution and only if you 153 | received the program in object code or executable form with such 154 | an offer, in accord with Subsection b above.) 155 | 156 | The source code for a work means the preferred form of the work for 157 | making modifications to it. For an executable work, complete source 158 | code means all the source code for all modules it contains, plus any 159 | associated interface definition files, plus the scripts used to 160 | control compilation and installation of the executable. However, as a 161 | special exception, the source code distributed need not include 162 | anything that is normally distributed (in either source or binary 163 | form) with the major components (compiler, kernel, and so on) of the 164 | operating system on which the executable runs, unless that component 165 | itself accompanies the executable. 166 | 167 | If distribution of executable or object code is made by offering 168 | access to copy from a designated place, then offering equivalent 169 | access to copy the source code from the same place counts as 170 | distribution of the source code, even though third parties are not 171 | compelled to copy the source along with the object code. 172 | 173 | 4. You may not copy, modify, sublicense, or distribute the Program 174 | except as expressly provided under this License. Any attempt 175 | otherwise to copy, modify, sublicense or distribute the Program is 176 | void, and will automatically terminate your rights under this License. 177 | However, parties who have received copies, or rights, from you under 178 | this License will not have their licenses terminated so long as such 179 | parties remain in full compliance. 180 | 181 | 5. You are not required to accept this License, since you have not 182 | signed it. However, nothing else grants you permission to modify or 183 | distribute the Program or its derivative works. These actions are 184 | prohibited by law if you do not accept this License. Therefore, by 185 | modifying or distributing the Program (or any work based on the 186 | Program), you indicate your acceptance of this License to do so, and 187 | all its terms and conditions for copying, distributing or modifying 188 | the Program or works based on it. 189 | 190 | 6. Each time you redistribute the Program (or any work based on the 191 | Program), the recipient automatically receives a license from the 192 | original licensor to copy, distribute or modify the Program subject to 193 | these terms and conditions. You may not impose any further 194 | restrictions on the recipients' exercise of the rights granted herein. 195 | You are not responsible for enforcing compliance by third parties to 196 | this License. 197 | 198 | 7. If, as a consequence of a court judgment or allegation of patent 199 | infringement or for any other reason (not limited to patent issues), 200 | conditions are imposed on you (whether by court order, agreement or 201 | otherwise) that contradict the conditions of this License, they do not 202 | excuse you from the conditions of this License. If you cannot 203 | distribute so as to satisfy simultaneously your obligations under this 204 | License and any other pertinent obligations, then as a consequence you 205 | may not distribute the Program at all. For example, if a patent 206 | license would not permit royalty-free redistribution of the Program by 207 | all those who receive copies directly or indirectly through you, then 208 | the only way you could satisfy both it and this License would be to 209 | refrain entirely from distribution of the Program. 210 | 211 | If any portion of this section is held invalid or unenforceable under 212 | any particular circumstance, the balance of the section is intended to 213 | apply and the section as a whole is intended to apply in other 214 | circumstances. 215 | 216 | It is not the purpose of this section to induce you to infringe any 217 | patents or other property right claims or to contest validity of any 218 | such claims; this section has the sole purpose of protecting the 219 | integrity of the free software distribution system, which is 220 | implemented by public license practices. Many people have made 221 | generous contributions to the wide range of software distributed 222 | through that system in reliance on consistent application of that 223 | system; it is up to the author/donor to decide if he or she is willing 224 | to distribute software through any other system and a licensee cannot 225 | impose that choice. 226 | 227 | This section is intended to make thoroughly clear what is believed to 228 | be a consequence of the rest of this License. 229 | 230 | 8. If the distribution and/or use of the Program is restricted in 231 | certain countries either by patents or by copyrighted interfaces, the 232 | original copyright holder who places the Program under this License 233 | may add an explicit geographical distribution limitation excluding 234 | those countries, so that distribution is permitted only in or among 235 | countries not thus excluded. In such case, this License incorporates 236 | the limitation as if written in the body of this License. 237 | 238 | 9. The Free Software Foundation may publish revised and/or new versions 239 | of the General Public License from time to time. Such new versions will 240 | be similar in spirit to the present version, but may differ in detail to 241 | address new problems or concerns. 242 | 243 | Each version is given a distinguishing version number. If the Program 244 | specifies a version number of this License which applies to it and "any 245 | later version", you have the option of following the terms and conditions 246 | either of that version or of any later version published by the Free 247 | Software Foundation. If the Program does not specify a version number of 248 | this License, you may choose any version ever published by the Free Software 249 | Foundation. 250 | 251 | 10. If you wish to incorporate parts of the Program into other free 252 | programs whose distribution conditions are different, write to the author 253 | to ask for permission. For software which is copyrighted by the Free 254 | Software Foundation, write to the Free Software Foundation; we sometimes 255 | make exceptions for this. Our decision will be guided by the two goals 256 | of preserving the free status of all derivatives of our free software and 257 | of promoting the sharing and reuse of software generally. 258 | 259 | NO WARRANTY 260 | 261 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 262 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 263 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 264 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 265 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 266 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 267 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 268 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 269 | REPAIR OR CORRECTION. 270 | 271 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 272 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 273 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 274 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 275 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 276 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 277 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 278 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 279 | POSSIBILITY OF SUCH DAMAGES. 280 | 281 | END OF TERMS AND CONDITIONS 282 | 283 | How to Apply These Terms to Your New Programs 284 | 285 | If you develop a new program, and you want it to be of the greatest 286 | possible use to the public, the best way to achieve this is to make it 287 | free software which everyone can redistribute and change under these terms. 288 | 289 | To do so, attach the following notices to the program. It is safest 290 | to attach them to the start of each source file to most effectively 291 | convey the exclusion of warranty; and each file should have at least 292 | the "copyright" line and a pointer to where the full notice is found. 293 | 294 | {description} 295 | Copyright (C) {year} {fullname} 296 | 297 | This program is free software; you can redistribute it and/or modify 298 | it under the terms of the GNU General Public License as published by 299 | the Free Software Foundation; either version 2 of the License, or 300 | (at your option) any later version. 301 | 302 | This program is distributed in the hope that it will be useful, 303 | but WITHOUT ANY WARRANTY; without even the implied warranty of 304 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 305 | GNU General Public License for more details. 306 | 307 | You should have received a copy of the GNU General Public License along 308 | with this program; if not, write to the Free Software Foundation, Inc., 309 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 310 | 311 | Also add information on how to contact you by electronic and paper mail. 312 | 313 | If the program is interactive, make it output a short notice like this 314 | when it starts in an interactive mode: 315 | 316 | Gnomovision version 69, Copyright (C) year name of author 317 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 318 | This is free software, and you are welcome to redistribute it 319 | under certain conditions; type `show c' for details. 320 | 321 | The hypothetical commands `show w' and `show c' should show the appropriate 322 | parts of the General Public License. Of course, the commands you use may 323 | be called something other than `show w' and `show c'; they could even be 324 | mouse-clicks or menu items--whatever suits your program. 325 | 326 | You should also get your employer (if you work as a programmer) or your 327 | school, if any, to sign a "copyright disclaimer" for the program, if 328 | necessary. Here is a sample; alter the names: 329 | 330 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 331 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 332 | 333 | {signature of Ty Coon}, 1 April 1989 334 | Ty Coon, President of Vice 335 | 336 | This General Public License does not permit incorporating your program into 337 | proprietary programs. If your program is a subroutine library, you may 338 | consider it more useful to permit linking proprietary applications with the 339 | library. If this is what you want to do, use the GNU Lesser General 340 | Public License instead of this License. 341 | 342 | 343 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Auto GitLab Backup 2 | 3 | [![AGB Logo](https://raw.githubusercontent.com/sund/auto-gitlab-backup/develop/agb_logo.png)](http://sund.la/glup) 4 | 5 | ---- 6 | 7 | ## Synopsis 8 | 9 | A simple script to backup your Gitlab data. This script will copy the backup archives of your gitlab installation via rsync, or scp. Also, you can copy backups to [Backblaze’s B2 Cloud Storage service.](https://www.backblaze.com/b2/cloud-storage.html) There is also a restore script available (see below.) 10 | 11 | It can backup and copy the ```gitlab.rb``` config file, if configured. 12 | 13 | This script is now more omnibus-gitlab centric. Compare your config file with the template! Usage with a source install is possible but not expressly shown here. 14 | 15 | ## Installation 16 | 17 | ### Prerequisites 18 | 19 | Deploy a working GitLab Omnibus installation and verify you can back it up with the rake task as documented in the [GitLab Documents](http://doc.gitlab.com/ce/raketasks/backup_restore.html). 20 | 21 | For Backblaze usage, configure your system for the [Backblaze Command-Line Tool](https://www.backblaze.com/b2/docs/quick_command_line.html) Also, see the [wiki page on B2](https://github.com/sund/auto-gitlab-backup/wiki/Backblaze-B2-Command-Line-Tool). 22 | 23 | #### Set up gitlab to expire backups 24 | 25 | Change ```/etc/gitlab/gitlab.rb``` to expire backups 26 | 27 | ``` 28 | # backup keep time 29 | gitlab_rails['backup_keep_time'] = 604800 30 | ``` 31 | 32 | ### Installation 33 | 34 | Clone to your directory of choice. I usually use ```/usr/local/sbin``` 35 | 36 | ``` 37 | git clone git@github.com:sund/auto-gitlab-backup.git 38 | ``` 39 | 40 | ### Updates 41 | 42 | Compare the ```auto-gitlab-backup.conf.sample``` file with your own copy. Make changes as needed to ensure no errors are encountered. 43 | 44 | ### Configure 45 | 46 | ```bash 47 | cp auto-gitlab-backup.conf.sample auto-gitlab-backup.conf 48 | ``` 49 | 50 | edit ```auto-gitlab-backup.conf``` 51 | 52 | ```bash 53 | ## user account on remote server 54 | # likely 'git' user 55 | remoteUser="" 56 | 57 | ## remote host 58 | # a backup gitlab server? 59 | remoteServer="" 60 | 61 | ## path to an alternate ssh key, if needed. 62 | sshKeyPath="" 63 | 64 | ## $remoteServer path for gitlab backups 65 | remoteDest="/var/opt/gitlab/backups" 66 | 67 | ## backup gitlab configs 68 | # change to 1 to enable 69 | backupConfigs=“0” 70 | 71 | ## rake quietly 72 | # change to 1 to enable quiet rake job 73 | quietRake=0 74 | 75 | ## enable backblaze b2 sync 76 | # change to 1 to enable 77 | # and set bucket name 78 | # and change b2keepDays if other than 5 days is desired 79 | b2blaze=0 80 | b2Bucketname=“” 81 | b2keepDays=“5” 82 | 83 | ## set $localConfDir 84 | # blank disables conf backups 85 | # you can create /var/opt/gitlab/backups/configBackups -- 86 | # gitlab doesn't seem to complain with a subfolder 87 | # in there. Plus it will rsync up with the backup. 88 | # So you won't need to enable a separate rsync run 89 | localConfDir="/var/opt/gitlab/backups/configBackups" 90 | 91 | ## set $remoteServer path for gitlab configs 92 | # blank disables remote copy 93 | # unless $localConfDir is outside /var/opt/gitlab/backups/configBackups 94 | # you can leave this blank 95 | remoteConfDest="" 96 | 97 | ## ssh port or 873 for rsyncd port 98 | remotePort=22 99 | 100 | ## git user home. 101 | # Only change the below setting if you have git's home in a different location 102 | gitHome="/var/opt/gitlab" 103 | 104 | ## only set below if rvm is in use and you need to source the rvm env file 105 | # echo $(rvm env --path) 106 | RVM_envPath="" 107 | 108 | ## only use the below settings if your destination is using rsync in daemon mode 109 | remoteModule="" 110 | rsync_password_file="" 111 | 112 | ## only change if configs are in different locations. (unlikely) 113 | localConfig="/etc/gitlab" 114 | localsshkeys="/var/opt/gitlab/.ssh" 115 | 116 | ## Check remote quota 117 | # change to true or 1 to enable 118 | checkQuota="0" 119 | 120 | ``` 121 | 122 | ### cron settings 123 | 124 | Example for crontab to run at 5:05am everyday. 125 | 126 | ```bash 127 | 5 5 * * * /usr/local/sbin/auto-gitlab-backup/auto-gitlab-backup.sh 128 | ``` 129 | 130 | ## Restore 131 | 132 | *Still under development but useful* 133 | 134 | run ```./restoreGitLab.sh -r``` and it will attempt to restore a backup. You may have to run some rake commands manually. 135 | 136 | ## Help 137 | 138 | See the [Wiki](https://github.com/sund/auto-gitlab-backup/wiki) for more detailed instructions or submit a [Issue](https://github.com/sund/auto-gitlab-backup/issues). 139 | 140 | ## Contribute 141 | 142 | See [Contribution Guide](https://github.com/sund/auto-gitlab-backup/blob/master/CONTRIBUTING.md) to improve this script. -------------------------------------------------------------------------------- /agb_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sund/auto-gitlab-backup/9e681fa2686bae8d869e3e2ad48d1556c8e7f30a/agb_logo.png -------------------------------------------------------------------------------- /auto-gitlab-backup.conf.sample: -------------------------------------------------------------------------------- 1 | ## user account on remote server 2 | # likely 'git' user 3 | remoteUser="" 4 | 5 | ## remote host 6 | # a backup gitlab server? 7 | remoteServer="" 8 | 9 | ## path to an alternate ssh key, if needed. 10 | sshKeyPath="" 11 | 12 | ## $remoteServer path for gitlab backups 13 | remoteDest="/var/opt/gitlab/backups" 14 | 15 | ## backup gitlab configs 16 | # change to 1 to enable 17 | backupConfigs="0" 18 | 19 | ## rake quietly 20 | # change to 1 to enable quiet rake job 21 | quietRake=0 22 | 23 | ## enable backblaze b2 sync 24 | # change to 1 to enable 25 | # and set bucketname 26 | # and change b2keepDays if other than 5 days is desired 27 | b2blaze=0 28 | b2Bucketname="" 29 | b2keepDays="5" 30 | 31 | ## set $localConfDir 32 | # blank disables conf backups 33 | # you can create /var/opt/gitlab/backups/configBackups -- 34 | # gitlab doesn't seem to complain with a subfolder 35 | # in there. Plus it will rsync up with the backup. 36 | # So you won't need to enable a separate rsync run 37 | localConfDir="/var/opt/gitlab/backups/configBackups" 38 | 39 | ## set $remoteServer path for gitlab configs 40 | # blank disables remote copy 41 | # unless $localConfDir is outside /var/opt/gitlab/backups/configBackups 42 | # you can leave this blank 43 | remoteConfDest="" 44 | 45 | ## ssh port or 873 for rsyncd port 46 | remotePort=22 47 | 48 | ## git user home. 49 | # Only change the below setting if you have git's home in a different location 50 | gitHome="/var/opt/gitlab" 51 | 52 | ## only set below if rvm is in use and you need to source the rvm env file 53 | # echo $(rvm env --path) 54 | RVM_envPath="" 55 | 56 | ## only use the below settings if your destination is using rsync in daemon mode 57 | remoteModule="" 58 | rsync_password_file="" 59 | 60 | ## localbackup directories 61 | # only change if gitlab_rails['backup_path'] in gitlab.rb is set 62 | # other than $gitHome/[backups|ci-backups] 63 | gitRakeBackups="/var/opt/gitlab/backups" 64 | gitRakeCIBackups="/var/opt/gitlab/ci-backups" 65 | 66 | ## only change if configs are in different locations. (unlikely) 67 | localConfig="/etc/gitlab" 68 | localsshkeys="/var/opt/gitlab/.ssh" 69 | 70 | ## Check remote quota 71 | # change to true or 1 to enable 72 | checkQuota="0" 73 | -------------------------------------------------------------------------------- /auto-gitlab-backup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ################################ 4 | # 5 | # autogitbackup.sh 6 | # ----------------------------- 7 | # 8 | # this script backups 9 | # /var/opt/gitlab/backups to 10 | # another host 11 | ################################ 12 | 13 | ## GPL v2 License 14 | # auto-gitlab-backup 15 | # Copyright (C) 2013 Shaun Sundquist 16 | # 17 | # This program is free software; you can redistribute it and/or modify 18 | # it under the terms of the GNU General Public License as published by 19 | # the Free Software Foundation; either version 2 of the License, or 20 | # (at your option) any later version. 21 | # 22 | # This program is distributed in the hope that it will be useful, 23 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 24 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 25 | # GNU General Public License for more details. 26 | # 27 | # You should have received a copy of the GNU General Public License along 28 | # with this program; if not, write to the Free Software Foundation, Inc., 29 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 30 | 31 | ### 32 | ## Settings/Variables 33 | # 34 | 35 | ### in cron job , the path may be just /bin and /usr/bin 36 | export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH" 37 | # 38 | gitHome="$(awk -F: -v v="git" '{if ($1==v) print $6}' /etc/passwd)" 39 | gitlabHome="$gitHome/gitlab" 40 | gitlab_rails="/opt/gitlab/embedded/service/gitlab-rails" 41 | PDIR=$(dirname $(readlink -f $0)) 42 | dateStamp=`date +"%F %H:%m:%S"` 43 | confFile="$PDIR/auto-gitlab-backup.conf" 44 | 45 | ### 46 | ## Functions 47 | # 48 | 49 | rvm_ENV() { 50 | ## environment for rvm 51 | # 52 | # rvm env --path -- ruby-version[@gemset-name] 53 | if [[ "$RVM_envPath" != "" ]] 54 | then 55 | echo "Using RVM environment file:" 56 | echo $RVM_envPath 57 | source $RVM_envPath 58 | fi 59 | 60 | } 61 | 62 | checkSize() { 63 | echo ===== Sizing ===== 64 | echo "Total disk space used for backup storage.." 65 | echo "Size - Location" 66 | echo `du -hs "$gitRakeBackups"` 67 | echo 68 | } 69 | 70 | archiveConfig() { 71 | echo ===== Archiving Configs ===== 72 | if [[ $backupConfigs = 1 ]] 73 | then 74 | if [[ -w $localConfDir ]] 75 | then 76 | tar -czf "$localConfDir/gitlabConf-$dateStamp.tgz" $localConfig $localsshkeys 77 | 78 | # remove files not within 3 days 79 | find $localConfDir -type f -mtime +3 -exec rm -v {} \; 80 | 81 | else 82 | echo "$localConfDir is not writable." 83 | fi 84 | else 85 | echo "Local config backups aren't enabled!" 86 | fi 87 | } 88 | 89 | rakeBackup() { 90 | echo ===== raking a backup ===== 91 | cd $gitRakeBackups 92 | 93 | if [[ $quietRake == 1 ]] 94 | then 95 | rakeBackup="gitlab-rake gitlab:backup:create CRON=1" 96 | else 97 | rakeBackup="gitlab-rake gitlab:backup:create" 98 | fi 99 | 100 | $rakeBackup 101 | } 102 | 103 | rsyncUp() { 104 | # rsync up with default key 105 | echo ============================================================= 106 | echo -e "Start rsync to \n$remoteServer:$remoteDest\ndefault key\n" 107 | rsync -Cavz --delete-after -e "ssh -p$remotePort" $gitRakeBackups/ $remoteUser@$remoteServer:$remoteDest 108 | 109 | # config rsync 110 | if [ ! -z $remoteConfDest ] 111 | then 112 | echo ===== rsync a config backup ===== 113 | echo ============================================================= 114 | echo -e "Start rsync to \n$remoteServer:$remoteConfDest\ndefault key\n" 115 | rsync -Cavz --delete-after -e "ssh -p$remotePort" $localConfDir/ $remoteUser@$remoteServer:$remoteConfDest 116 | fi 117 | } 118 | 119 | rsyncUp_dryrun() { 120 | # rsync up with default key 121 | echo ============================================================= 122 | echo -e "Start dry run rsync to \n$remoteServer:$remoteDest\ndefault key\n" 123 | rsync --dry-run -Cavz --delete-after -e "ssh -p$remotePort" $gitRakeBackups/ $remoteUser@$remoteServer:$remoteDest 124 | 125 | # config rsync 126 | if [ ! -z $remoteConfDest ] 127 | then 128 | echo ===== rsync a config backup ===== 129 | echo ============================================================= 130 | echo -e "Start dry run rsync to \n$remoteServer:$remoteConfDest\ndefault key\n" 131 | rsync --dry-run -Cavz --delete-after -e "ssh -p$remotePort" $localConfDir/ $remoteUser@$remoteServer:$remoteConfDest 132 | fi 133 | } 134 | 135 | rsyncKey() { 136 | # rsync up with specific key 137 | echo ============================================================= 138 | echo -e "Start rsync to \n$remoteServer:$remoteDest\nwith specific key\n" 139 | rsync -Cavz --delete-after -e "ssh -i $sshKeyPath -p$remotePort" $gitRakeBackups/ $remoteUser@$remoteServer:$remoteDest 140 | 141 | # config rsync 142 | if [ ! -z $remoteConfDest ] 143 | then 144 | echo ===== rsync a config backup ===== 145 | echo ============================================================= 146 | echo -e "Start rsync to \n$remoteServer:$remoteConfDest\ndefault key\n" 147 | rsync -Cavz --delete-after -e "ssh -p$remotePort" $localConfDir/ $remoteUser@$remoteServer:$remoteConfDest 148 | fi 149 | } 150 | 151 | rsyncKey_dryrun() { 152 | # rsync up with specific key 153 | echo ============================================================= 154 | echo -e "Start dry run rsync to \n$remoteServer:$remoteDest\nwith specific key\n" 155 | rsync --dry-run -Cavz --delete-after -e "ssh -i $sshKeyPath -p$remotePort" $gitRakeBackups/ $remoteUser@$remoteServer:$remoteDest 156 | 157 | # config rsync 158 | if [ ! -z $remoteConfDest ] 159 | then 160 | echo ===== rsync a config backup ===== 161 | echo ============================================================= 162 | echo -e "Start rsync to \n$remoteServer:$remoteConfDest\ndefault key\n" 163 | rsync --dry-run -Cavz --delete-after -e "ssh -p$remotePort" $localConfDir/ $remoteUser@$remoteServer:$remoteConfDest 164 | fi 165 | } 166 | 167 | rsyncDaemon() { 168 | # rsync up with specific key 169 | echo ============================================================= 170 | echo -e "Start rsync to \n$remoteUser@$remoteServer:$remoteModule\nin daemon mode\n" 171 | rsync -Cavz --port=$remotePort --password-file=$rsync_password_file --delete-after /$gitRakeBackups/ $remoteUser@$remoteServer::$remoteModule 172 | 173 | # config rsync 174 | if [ ! -z $remoteConfDest ] 175 | then 176 | echo ===== rsync a config backup ===== 177 | echo ============================================================= 178 | echo -e "Start rsync to \n$remoteServer:$remoteConfDest\ndefault key\n" 179 | rsync -Cavz --delete-after -e "ssh -p$remotePort" $localConfDir/ $remoteUser@$remoteServer:$remoteConfDest 180 | fi 181 | } 182 | 183 | rsyncDaemon_dryrun() { 184 | # rsync up with specific key 185 | echo ============================================================= 186 | echo -e "Start rsync to \n$remoteUser@$remoteServer:$remoteModule\nin daemon mode\n" 187 | rsync --dry-run -Cavz --port=$remotePort --password-file=$rsync_password_file --delete-after /$gitRakeBackups/ $remoteUser@$remoteServer::$remoteModule 188 | 189 | # config rsync 190 | if [ ! -z $remoteConfDest ] 191 | then 192 | echo ===== rsync a config backup ===== 193 | echo ============================================================= 194 | echo -e "Start rsync to \n$remoteServer:$remoteConfDest\ndefault key\n" 195 | rsync --dry-run -Cavz --delete-after -e "ssh -p$remotePort" $localConfDir/ $remoteUser@$remoteServer:$remoteConfDest 196 | fi 197 | } 198 | 199 | sshQuotaKey() { 200 | #quota check: with a key remoteServer, run the quota command 201 | if [[ $checkQuota == "true" || $checkQuota = 1 ]] 202 | then 203 | echo ============================================================= 204 | echo -e "Quota check: \n$remoteUser@$remoteServer:$remoteModule\nwith key\n" 205 | ssh -p $remotePort -i $sshKeyPath $remoteUser@$remoteServer "quota" 206 | echo ============================================================= 207 | 208 | fi 209 | } 210 | 211 | sshQuota() { 212 | #quota check: assuming we can ssh into remoteServer, run the quota command 213 | if [[ $checkQuota == "true" || $checkQuota = 1 ]] 214 | then 215 | echo ============================================================= 216 | echo -e "Quota check: \n$remoteUser@$remoteServer:$remoteModule\n" 217 | ssh -p $remotePort $remoteUser@$remoteServer "quota" 218 | echo ============================================================= 219 | 220 | fi 221 | } 222 | 223 | printScriptver() { 224 | # print the most recent tag 225 | echo "This is $0" 226 | echo "Version $(git describe --abbrev=0 --tags --always), commit #$(git log --pretty=format:'%h' -n 1)." 227 | } 228 | 229 | usage() { 230 | echo "" 231 | echo "Usage:" 232 | echo "$0 -h | --help this help page" 233 | echo "$0 -d | --dry-run test rsync operations; no data transmitted." 234 | echo "$0 no options, perform backup, rsync or b2 operations." 235 | echo "" 236 | } 237 | 238 | areWeRoot() { 239 | ## test for running as root 240 | if [[ "$UID" -ne "$ROOT_UID" ]]; 241 | then 242 | echo "You must run this script as root to run." 243 | if [[ $1 == -d ]] || [[ $1 == --dry-run ]] 244 | then 245 | echo "...even to dryrun as we need to access the backup dir." 246 | fi 247 | usage 248 | exit 1 249 | fi 250 | } 251 | 252 | b2Sync() { 253 | # b2 sync 254 | echo ============================================================= 255 | echo -e "Start b2 sync of $gitRakeBackups to bucket $b2Bucketname \n" 256 | 257 | if [[ $b2blaze == 0 ]] 258 | then 259 | echo "Backblaze b2 file operations not enabled!" 260 | else 261 | 262 | # test for b2 command 263 | if type b2 > /dev/null 2>&1 264 | then 265 | # bucketname set and readable 266 | if [ ! -z $b2Bucketname ] 267 | then 268 | if test -r "$gitRakeBackups" -a -d "$gitRakeBackups" 269 | then 270 | b2 sync --noProgress --keepDays $b2keepDays --replaceNewer $gitRakeBackups/ b2://$b2Bucketname/backups/ 271 | else 272 | echo " gitRakeBackups ($gitRakeBackups) not readable." 273 | fi 274 | else 275 | echo " b2Bucketname not set." 276 | fi 277 | else 278 | echo " b2 command not found!" 279 | fi 280 | 281 | fi 282 | echo "" 283 | } 284 | 285 | b2SyncProgress() { 286 | # b2 sync 287 | echo ============================================================= 288 | echo -e "Start b2 sync of $gitRakeBackups to bucket $b2Bucketname \n" 289 | 290 | if [[ $b2blaze == 0 ]] 291 | then 292 | echo "Backblaze b2 file operations not enabled!" 293 | else 294 | 295 | # test for b2 command 296 | if type b2 > /dev/null 2>&1 297 | then 298 | # bucketname set and readable 299 | if [ ! -z $b2Bucketname ] 300 | then 301 | if test -r "$gitRakeBackups" -a -d "$gitRakeBackups" 302 | then 303 | b2 sync --keepDays $b2keepDays --replaceNewer $gitRakeBackups/ b2://$b2Bucketname/backups/ 304 | else 305 | echo " gitRakeBackups ($gitRakeBackups) not readable." 306 | fi 307 | else 308 | echo " b2Bucketname not set." 309 | fi 310 | else 311 | echo " b2 command not found!" 312 | fi 313 | 314 | fi 315 | echo "" 316 | } 317 | 318 | b2SyncConf() { 319 | # b2 sync 320 | echo ============================================================= 321 | echo -e "Start b2 sync of /etc/gitlab to bucket $b2Bucketname/configs/ \n" 322 | 323 | if [[ $backupConfigs == 1 ]] 324 | then 325 | if [[ $b2blaze == 0 ]] 326 | then 327 | echo "Backblaze b2 file operations not enabled!" 328 | else 329 | 330 | # test for b2 command 331 | if type b2 > /dev/null 2>&1 332 | then 333 | # bucketname set and readable 334 | if [ ! -z $b2Bucketname ] 335 | then 336 | if test -r "$gitRakeBackups" -a -d "$gitRakeBackups" 337 | then 338 | b2 sync --noProgress --keepDays $b2keepDays --replaceNewer /etc/gitlab/ b2://$b2Bucketname/configs/ 339 | else 340 | echo " gitRakeBackups ($gitRakeBackups) not readable." 341 | fi 342 | else 343 | echo " b2Bucketname not set." 344 | fi 345 | else 346 | echo " b2 command not found!" 347 | fi 348 | 349 | fi 350 | fi 351 | echo "" 352 | } 353 | 354 | b2SyncConfProgress() { 355 | # b2 sync 356 | echo ============================================================= 357 | echo -e "Start b2 sync of /etc/gitlab to bucket $b2Bucketname/configs/ \n" 358 | 359 | if [[ $backupConfigs == 1 ]] 360 | then 361 | if [[ $b2blaze == 0 ]] 362 | then 363 | echo "Backblaze b2 file operations not enabled!" 364 | else 365 | 366 | # test for b2 command 367 | if type b2 > /dev/null 2>&1 368 | then 369 | # bucketname set and readable 370 | if [ ! -z $b2Bucketname ] 371 | then 372 | if test -r "$gitRakeBackups" -a -d "$gitRakeBackups" 373 | then 374 | b2 sync --keepDays $b2keepDays --replaceNewer /etc/gitlab/ b2://$b2Bucketname/configs/ 375 | else 376 | echo " gitRakeBackups ($gitRakeBackups) not readable." 377 | fi 378 | else 379 | echo " b2Bucketname not set." 380 | fi 381 | else 382 | echo " b2 command not found!" 383 | fi 384 | 385 | fi 386 | fi 387 | echo "" 388 | } 389 | 390 | confFileExist() { 391 | # read the conffile 392 | if [ -e $confFile -a -r $confFile ] 393 | then 394 | source $confFile 395 | echo "Parsing config file..." 396 | rvm_ENV 397 | else 398 | echo "No confFile found; Remote copy DISABLED." 399 | fi 400 | } 401 | 402 | ### 403 | ## Git'r done 404 | # 405 | 406 | case $1 in 407 | -h|--help ) 408 | usage 409 | ;; 410 | -d|--dry-run ) 411 | areWeRoot $1 412 | confFileExist 413 | archiveConfig 414 | ##test ssh and rsync functions 415 | if [[ $remoteModule != "" ]] 416 | then 417 | rsyncDaemon_dryrun 418 | b2SyncProgress 419 | # no Daemon so lets see if we are using a special key 420 | else if [ -e $sshKeyPath -a -r $sshKeyPath ] && [[ $sshKeyPath != "" ]] 421 | then 422 | rsyncKey_dryrun 423 | b2SyncProgress 424 | sshQuotaKey 425 | else if [[ $remoteServer != "" ]] 426 | then 427 | # use the default 428 | rsyncUp_dryrun 429 | b2SyncProgress 430 | sshQuota 431 | fi 432 | fi 433 | fi 434 | ;; 435 | * ) 436 | areWeRoot $1 437 | confFileExist 438 | # perform backup 439 | rakeBackup 440 | archiveConfig 441 | checkSize 442 | # go back to where we came from 443 | cd $PDIR 444 | # if the $remoteModule is set run rsyncDaemo 445 | ## here we assume variables are set right and only check when needed. 446 | if [[ $remoteModule != "" ]] 447 | then 448 | rsyncDaemon 449 | b2Sync 450 | # no Daemon so lets see if we are using a special key 451 | else if [ -e $sshKeyPath -a -r $sshKeyPath ] && [[ $sshKeyPath != "" ]] 452 | then 453 | rsyncKey 454 | b2Sync 455 | sshQuotaKey 456 | else if [[ $remoteServer != "" ]] 457 | then 458 | # use the default 459 | rsyncUp 460 | b2Sync 461 | sshQuota 462 | else if [[ $b2blaze == "1" ]] 463 | then 464 | # use b2Sync only 465 | b2Sync 466 | fi 467 | fi 468 | fi 469 | fi 470 | ;; 471 | esac 472 | 473 | ## temp 474 | if [[ $b2blaze != 0 ]] 475 | then 476 | b2 get-account-info 477 | b2 list-buckets 478 | fi 479 | 480 | # Print version 481 | printScriptver 482 | 483 | ### 484 | ## Exit gracefully 485 | # 486 | exit 0 487 | -------------------------------------------------------------------------------- /restoreGitLab.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ################################ 4 | # 5 | # restoreGitLab.sh 6 | # ----------------------------- 7 | # 8 | # this script restores 9 | # /var/opt/gitlab/backups to 10 | # current host 11 | ################################ 12 | 13 | ## GPL v2 License 14 | # auto-gitlab-backup 15 | # Copyright (C) 2013 Shaun Sundquist 16 | # 17 | # This program is free software; you can redistribute it and/or modify 18 | # it under the terms of the GNU General Public License as published by 19 | # the Free Software Foundation; either version 2 of the License, or 20 | # (at your option) any later version. 21 | # 22 | # This program is distributed in the hope that it will be useful, 23 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 24 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 25 | # GNU General Public License for more details. 26 | # 27 | # You should have received a copy of the GNU General Public License along 28 | # with this program; if not, write to the Free Software Foundation, Inc., 29 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 30 | 31 | ### 32 | ## Settings/Variables 33 | # 34 | 35 | ### in cron job, the path may be just /bin and /usr/bin 36 | export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH" 37 | # 38 | gitHome="$(awk -F: -v v="git" '{if ($1==v) print $6}' /etc/passwd)" 39 | gitlabHome="$gitHome/gitlab" 40 | gitlab_rails="/opt/gitlab/embedded/service/gitlab-rails" 41 | gitRakeBackups="/var/opt/gitlab/backups" 42 | PDIR=$(dirname $(readlink -f $0)) 43 | confFile="$PDIR/auto-gitlab-backup.conf" 44 | rakeRestore="gitlab-rake gitlab:backup:restore" 45 | 46 | stopUnicorn="gitlab-ctl stop unicorn" 47 | stopsidekiq="gitlab-ctl stop sidekiq" 48 | 49 | restartGitLab="gitlab-ctl restart" 50 | 51 | ### 52 | ## Functions 53 | # 54 | 55 | usage() { 56 | echo "${0} will restore a gitlab backup" 57 | echo "This script should be run as root or" 58 | echo "as a user that can read the backups directory" 59 | echo "in $gitRakeBackups" 60 | echo "" 61 | echo "USAGE:" 62 | echo "${0} -l #list backups found in $gitRakeBackups" 63 | echo "" 64 | echo "${0} -r #restore a backup" 65 | exit 0 66 | } 67 | 68 | runAs() { 69 | ## test for running as root 70 | if [[ "$UID" -ne "$ROOT_UID" ]]; 71 | then 72 | echo "You must be logged in as root to run this script." 73 | exit 1 74 | fi 75 | } 76 | 77 | sanityCheck() { 78 | echo "git's home is found to be : $gitHome" 79 | echo "gitlab backups are found in : $gitRakeBackups" 80 | } 81 | 82 | getFileList() { 83 | echo "File list in : $1" 84 | 85 | # best not to use ls to get a listing of a dir 86 | local e=1 87 | shopt -s nullglob 88 | for f in $1/*; do 89 | [[ -e $f ]] && [[ -f $f ]] || continue 90 | fileArray[$e]=$(basename "$f") 91 | ((e++)) 92 | done 93 | shopt -u nullglob 94 | 95 | #echo "fileArray is : ${fileArray[@]}" 96 | #echo "fileArray size is : ${#fileArray[@]}" 97 | 98 | ## sort array so newest is first 99 | 100 | local c=1 101 | for ((i=${#fileArray[@]}; i>=1; i--)); do 102 | #echo " sort i : $i & c $c" 103 | REVfileArray[$c]="${fileArray[$i]}" 104 | ((c++)) 105 | done 106 | 107 | #echo "REVfileArray is : ${REVfileArray[@]}" 108 | #echo "REVfileArray size is : ${#REVfileArray[@]}" 109 | 110 | ## copy REVfileArray to fileArray 111 | unset fileArray 112 | 113 | for ((i=${#REVfileArray[@]}; i>=1; i--)); do 114 | fileArray[$i]="${REVfileArray[$i]}" 115 | done 116 | 117 | #echo "did we reasign? : ${fileArray[@]}" 118 | #echo "reset fileArray size is : ${#fileArray[@]}" 119 | } 120 | 121 | printFileList() { 122 | #echo "Array is ${fileArray[@]}" 123 | 124 | # print out each element with while based 125 | # on # of elements of array 126 | e=1 127 | while [ $e -le ${#fileArray[@]} ] 128 | do 129 | #echo "Element $e is : ${fileArray[$e]}" 130 | ((e++)) 131 | done 132 | 133 | #echo "Reversed is : ${REVfileArray[@]}" 134 | 135 | # print each element 136 | local z=1 137 | while [ $z -le ${#fileArray[@]} ] 138 | do 139 | #echo "Element $z of REVfileArray is : ${REVfileArray[$z]}" 140 | echo "${REVfileArray[$z]}" 141 | ((z++)) 142 | done 143 | 144 | 145 | } 146 | 147 | verifyRestore() { 148 | case "${#fileArray[@]}" in 149 | 0) 150 | # if the file list is 0 then exit with message 151 | echo "ERROR: I didn't find any backup files in $gitRakeBackups." 152 | echo "Copy or restore backups to $gitRakeBackups." 153 | exit 1 154 | ;; 155 | 156 | 1) 157 | # if the file list is 1 skip the menu and ask if ready to restore 158 | verifySingle 159 | ;; 160 | 161 | *) 162 | # if the file list is grather than 1, menu time 163 | ## if whiptale installed, use 164 | if `command -v whiptail > /dev/null` 165 | then 166 | chooseBackupWhiped 167 | else 168 | chooseBackup 169 | fi 170 | rakeRestore 171 | 172 | ;; 173 | esac 174 | 175 | } 176 | 177 | verifySingle() { 178 | echo "${fileArray[1]} was the only backup file found to restore from." 179 | 180 | 181 | if [ -f ${fileArray[1]} ] 182 | then 183 | read -p " Proceed with restore? (Y/n)" yesorno 184 | case $yesorno in 185 | y*) 186 | rakeRestoreSingle 187 | ;; 188 | n*) 189 | echo "${fileArray[1]} has not been restored" 190 | exit 1 191 | ;; 192 | esac 193 | fi 194 | 195 | } 196 | 197 | chooseBackup() { 198 | echo "Choose from below which backup to restore from:" 199 | echo "** The newest will be at the bottom of the list! **" 200 | ## TO DO: add option to exit in array at this point 201 | ## TO DO: invert array, newest on top 202 | 203 | fileArray=("${fileArray[@]}" "Abort") 204 | 205 | select chosen in ${fileArray[@]}; 206 | do 207 | echo "you picked $chosen " 208 | break; 209 | done 210 | 211 | if [[ "$chosen" == "Abort" ]] 212 | then 213 | echo "Aborting backup!" 214 | exit 1 215 | fi 216 | 217 | } 218 | 219 | chooseBackupWhiped() { 220 | # make sure we don't have an empty list 221 | WC=`echo ${fileArray[@]} | wc -l` 222 | 223 | if [[ "${WC}" -ne 0 ]] 224 | then 225 | # create the whippedList 226 | i=0 227 | s=1 # decimal ASCII "A" 228 | for f in ${fileArray[@]} 229 | do 230 | # convert to octal then ASCII character for selection tag 231 | whippedListArray[i]="$f" 232 | whippedListArray[i+1]=" " # save file name 233 | ((i+=2)) 234 | ((s++)) 235 | done 236 | 237 | ## calc screen height and width 238 | whipHeight=20 # find away to calc a pleasing height 239 | whipWidth=42 # find away to calc a pleasing height 240 | 241 | # whiptail uses stdin and stdout to draw boxes, have to reassign with 3>&1 1>&2 2>&3 242 | chosen=$(whiptail --backtitle "Restore a GitLab Backup" --title "Restore from backup" \ 243 | --menu "Please select the file to restore" "$whipHeight" "$whipWidth" \ 244 | "${#fileArray[@]}" "${whippedListArray[@]}" 3>&1 1>&2 2>&3) 245 | fi 246 | 247 | echo " you chose : $chosen" 248 | 249 | } 250 | 251 | rakeRestore() { 252 | cd $gitlabDir 253 | # if chosen is null then abort 254 | if [ -z "$chosen" ] 255 | then 256 | echo "Cancelling restore..." 257 | exit 1 258 | fi 259 | 260 | backupfilename=$(basename "$chosen") 261 | echo $backupfilename 262 | timeStamp=${backupfilename%_gitlab_backup.tar} 263 | echo "timestamp is : $timeStamp" 264 | # restore the chosen backup 265 | #$rakeRestore BACKUP=$timeStamp 266 | echo " rake gitlab:backup:restore returned : $?" 267 | } 268 | 269 | rakeRestoreSingle() { 270 | cd $gitlabDir 271 | # restore the only backup available; will complain if it finds more than one 272 | $rakeRestore RAILS_ENV=production 273 | echo " rake gitlab:backup:restore returned : $?" 274 | } 275 | 276 | permsFixBase() { 277 | cd $gitlabDir 278 | # Fix the permissions on the repository base 279 | # repositories ahould be found in git's home 280 | sudo chmod -R ug+rwX,o-rwx $gitHome/repositories/ 281 | sudo chmod -R ug-s $gitHome/repositories/ 282 | sudo find $gitHome/repositories/ -type d -print0 | sudo xargs -0 chmod g+s 283 | # 284 | } 285 | 286 | postRestoreLink() { 287 | cd $gitlabDir 288 | # # find a list of repos put into array 289 | 290 | local folderFound=("`sudo -u git -H find $gitHome/repositories -name *.git -type d -print`") 291 | declare -a gitfolderArray 292 | gitfolderArray=(${folderFound// / }) 293 | 294 | echo "I found ${#gitfolderArray[*]} git repos." 295 | 296 | #and loop through the array 297 | 298 | for i in ${gitfolderArray[@]}; do 299 | echo "Creating symlink to $i/hooks/post-receive" 300 | sudo -u git ln -sf $gitHome/gitlab-shell/hooks/post-receive $i/hooks/post-receive 301 | done 302 | } 303 | # 304 | rakeInfo() { 305 | cd $gitlabDir 306 | # echo "function: rakeInfo" 307 | } 308 | # 309 | # 310 | rakeCheck() { 311 | cd $gitlabDir 312 | gitlab-rake gitlab:check 313 | } 314 | 315 | ### 316 | ## Git'r done 317 | # 318 | 319 | case "$1" in 320 | "-l") ## gitlab in place other than /home/git 321 | # find the backup and make a list 322 | runAs 323 | sanityCheck 324 | getFileList $gitRakeBackups 325 | printFileList 326 | verifyRestore 327 | #chooseBackupWhiped 328 | # fix things 329 | ## if the rake fails (it will with different git home 330 | # run the fixes 331 | ## TO DO determine if we really need to run these 332 | permsFixBase 333 | postRestoreLink 334 | rakeInfo 335 | rakeCheck 336 | ;; 337 | 338 | "-r") ## gitlab in /home/git 339 | # find the backup and make a list 340 | runAs 341 | sanityCheck 342 | getFileList $gitRakeBackups 343 | #printFileList 344 | verifyRestore 345 | # run the fixes 346 | ## TO DO determine if we really need to run these 347 | # permsFixBase 348 | # postRestoreLink 349 | rakeInfo 350 | rakeCheck 351 | ;; 352 | 353 | "-t") 354 | sanityCheck 355 | getFileList $gitlabBackups 356 | #printFileList 357 | verifyRestore 358 | 359 | # fix things 360 | permsFixBase 361 | postRestoreLink 362 | rakeInfo 363 | #rakeCheck 364 | 365 | ;; 366 | 367 | "-f") 368 | permsFixBase 369 | postRestoreLink 370 | rakeInfo 371 | rakeCheck 372 | 373 | ;; 374 | *) 375 | usage 376 | ;; 377 | esac 378 | 379 | ### 380 | ## Exit gracefully 381 | # 382 | exit 0 383 | --------------------------------------------------------------------------------