commit f7650a0e09741f76b13ab27c5d9ce7e70e00ae7a
parent aee118cbedec1dbbff7327f3db82bb5c70e71cca
Author: oscarbenedito <oscar@obenedito.org>
Date:   Sun, 13 Oct 2019 13:30:10 +0200

Added custom repositories support

Diffstat:
M.gitignore | 1+
MREADME.md | 56++++++++++++++++++++++++++++++++++++++------------------
Mgit-backup.py | 21+++++++++++++++++++++
3 files changed, 60 insertions(+), 18 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,3 +1,4 @@ tokens.json repositories backup_data.json +custom_directories.json diff --git a/README.md b/README.md @@ -2,15 +2,15 @@ Very simple script to back up all the repositories the user is member of on GitLab and GitHub. - ## Prerequisites +## Prerequisites - In order to run the program, you will need `python3` and the python libraries specified in the `requirements.txt` file. If you have `pip3` installed, you can install the libraries with the command: +In order to run the program, you will need `python3` and the python libraries specified in the `requirements.txt` file. If you have `pip3` installed, you can install the libraries with the command: - ``` - pip3 install -r requirements.txt - ``` +``` +pip3 install -r requirements.txt +``` - You will also need a GitLab and a GitHub access token and crete a file named `tokens.json` with the following content: +You will also need a GitLab and a GitHub access token and crete a file named `tokens.json` with the following content: ```json { "gitlab.com":"<GitLab token>", @@ -18,23 +18,43 @@ Very simple script to back up all the repositories the user is member of on GitL } ``` - ## Running the program +Finally, you must upload your ssh key to GitLab or GitHub if you have any private or internal repositories. + +## Running the program - To run the program, execute the file `git-backup.py`: +To run the program, execute the file `git-backup.py`: + +``` +python3 git-backup.py +``` + +### Adding custom repositories + +If you want to backup repositories from which you are not a member on the currently supported hosts, or if you want to add repositories from other hosts, you can do so creating a file named `custom_directories.json` with the following structure: + +```json +[ + { + "name":"<Repository name - for backup information>", + "description":"<Repository description - for backup information>", + "path":"<Repository path where the backup will be saved>", + "ssh_url":"<Repository url>", + "host":"<Repository host - for backup information and stablishing saving directory>" + } +] +``` - ``` - python3 git-backup.py - ``` +You can add more than one object to the array and it will backup all of the repositories. Make sure that you upload your ssh key to the hosts in case it is needed for authentification. - ## Future updates +## Future updates - - [ ] More information on the output file or the terminal: how many new repositories were added, how many have been deleted on the hosting services. - - [ ] Manage what happens if history has changed in the hosting service. + - [ ] More information on the output file or the terminal: how many new repositories were added, how many have been deleted on the hosting services. + - [ ] Manage what happens if history has changed in the hosting service. - ## License +## License - The program is licensed under the GPL v3. License is available [here](https://gitlab.com/oscarbenedito/git-backup/blob/master/COPYING). +The program is licensed under the GPL v3. License is available [here](https://gitlab.com/oscarbenedito/git-backup/blob/master/COPYING). - ## Author +## Author - - **Oscar Benedito** - oscar@obenedito.org + - **Oscar Benedito** - oscar@obenedito.org diff --git a/git-backup.py b/git-backup.py @@ -82,6 +82,27 @@ if 'github.com' in tokens: page += 1 repositories = get_repositories_data(url, page) +# custom +if os.path.exists("custom_directories.json"): + custom_file = open('custom_directories.json', 'r') + repositories = json.load(custom_file) + custom_file.close() + for repository in repositories: + clone_dir = 'repositories/' + repository['host'] + '/' + repository['path'] + print(repository['host'] + '/' + repository['path']) + if os.path.isdir(clone_dir): + git.cmd.Git(clone_dir).fetch() + else: + os.system('git clone --mirror ' + repository['ssh_url'] + ' ' + clone_dir) + if repository['host'] not in backup_data['sites']: + backup_data['sites'][repository['host']] = [] + backup_data['sites'][repository['host']].append({ + 'name': repository['name'], + 'description': repository['description'], + 'path': repository['path'], + 'ssh_url': repository['ssh_url'] + }) + with open('backup_data.json', 'w', encoding='utf-8') as output_file: json.dump(backup_data, output_file, ensure_ascii=False) output_file.close()