0

I recently just wrote a script for backing up a remote Git server onto my local server. However, I have about 45 different repositories so this involves copying and pasting my code 45 times and changing the repo names for each one. Is there a simpler way to accomplish what I am trying to do? (In terms of editing my script) Here is my code:

sudo rm -rf /home/<user>/<localRepoFolder>
git clone --mirror git@<remoteIP:repo> /home/<user>/<localRepos>/<repo>/$(date +%Y%m%d)
cd /home/<user>/<localRepos>/<repo>/$(date +%Y%m%d)
git remote rm origin
git remote add origin git@<localIP>
git push origin --all
git push --tags

Also, how would I go about editing the script so that I can save the 2 latest backups in case something breaks after removing them from my local server? If/else statement dealing with the timestamp?

kvantour
  • 25,269
  • 4
  • 47
  • 72
JFisher
  • 25
  • 3
  • 1
    Loops? Variables? Parameters that can be passed to scripts? – Eugene Sh. Jul 27 '18 at 14:08
  • What has this got to do with [tag:c]? – Spikatrix Jul 27 '18 at 14:09
  • Why not store the git remote IPs in an array and loop over them? – Spikatrix Jul 27 '18 at 14:10
  • Have you heard about [bash script arguments](https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash), [bash variables](http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-5.html) and [bash loops](https://www.cyberciti.biz/faq/bash-for-loop/) – running.t Jul 27 '18 at 14:17

1 Answers1

0

Only way to go quicker is to create a bash files like this :

PREPARE A REPO :

#!/usr/bin/env bash
if [ $# != 0 ]
then
name_repo="$*"
email=<your email>
blih -u $email repository create "$name_repo"
blih -u $email repository getacl "$name_repo"
git clone git@<git address>/$email/$name_repo
else
echo "Enter repository name"
fi

PUSH MASTER

#!/bin/bash
if [ $# = 0 ]
then
commit_message="default commit message"
else
commit_message=$*
fi
git config user.name "<username>"
git config user.email "<mail / git acount>"
git add --all
git commit -m"$commit_message"
git push

AND CLONE :

#!/bin/bash
if [ $# == 1 ]
then
git clone git@<git address>/<email>/$1
fi

Hope that help

A beginner
  • 124
  • 1
  • 14