Here’s a bash script which will back up all repositories on a self hosted Jira and BitBucket server

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash

FTPSERVER=backup-server-ip-or-hostname
FTPUSER=ftpusername
FTPPASS=ftppassword
DAY=`date +%A`

ftp -4vpn $FTPSERVER <<ENDFOO
user $FTPUSER $FTPPASS
prompt
mdelete $DAY\*
quit
ENDFOO

cd /tmp

echo "Backing up yesterdays Jira backups"
# Back up yesterday's Jira backups
for i in $(ls -1 /var/atlassian/application-data/jira/export | grep `date -d yesterday +%Y-%b-%d`); do
ftp -4vpn $FTPSERVER <<ENDFOO
user $FTPUSER $FTPPASS
put /var/atlassian/application-data/jira/export/$i $i
prompt
mdelete _`date -d "last month" +%Y-%b-%d`_
quit
ENDFOO
done

echo "Backing up today's backup"
for i in $(ls -1 /var/atlassian/application-data/jira/export | grep `date +%Y-%b-%d`); do
ftp -4vpn $FTPSERVER <<ENDFOO
user $FTPUSER $FTPPASS
put /var/atlassian/application-data/jira/export/$i $i
quit
ENDFOO
done

echo "Backing up Jira attachments"

tar -czf /tmp/$DAY-var-atlassian-application-data-jira-data.tgz /var/atlassian/application-data/jira/data
ftp -4vpn $FTPSERVER <<ENDFOO
user $FTPUSER $FTPPASS
put /tmp/$DAY-var-atlassian-application-dara-jira-data.tgz $DAY-var-atlassian-application-dara-jira-data.tgz
quit
ENDFOO
rm -f /tmp/$DAY-var-atlassian-application-dara-jira-data.tgz
#####
#
# Back up all repos
#
#####
for i in `curl -q https://**BitbucketServer**/rest/api/1.0/repos\?limit\=2000 | grep -Eo "(http|https)://[a-zA-Z0-9./?i@=_-]*" | grep git`; do
i=`echo $i | sed "s/username@//g"`
echo $i
PROJECT=`echo $i | cut -d/ -f5` REPO=`echo $i | cut -d/ -f6`
echo "Backing up $REPO from project $PROJECT"
git clone --bare "$i" /tmp/$REPO && tar -czf "/tmp/$DAY-$PROJECT_$REPO.tar.gz" "/tmp/$REPO" >/dev/null
if [ $? -ne 0 ]; then
 echo -e "Subject: FAILED BACKUP\n\n$PROJECT_$REPO failed backup" | sendmail erroremailaddress@foo.com
else
 ftp -4vpn $FTPSERVER <<ENDFOO
user $FTPUSER $FTPPASS
put /tmp/$DAY-$PROJECT_$REPO.tar.gz $DAY-$PROJECT*$REPO.tar.gz
quit
ENDFOO
rm -fr /tmp/$REPO
rm -f /tmp/$DAY-$PROJECT*$REPO.tar.gz
fi
done