Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix jenkins backup if some directories don't exist #27

Merged
merged 5 commits into from
Jul 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ bundle exec rake test
## rotate backup files
```bash
# keep backup with latest 30 days
find /path/to/backup_* -mtime +30 | xargs rm -f
find /path/to/backup_* -mtime +30 -delete
```

## Restore commands
Expand Down
38 changes: 21 additions & 17 deletions jenkins-backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,44 @@ readonly CUR_DIR=$(cd $(dirname ${BASH_SOURCE:-$0}); pwd)
readonly TMP_DIR="$CUR_DIR/tmp"
readonly ARC_NAME="jenkins-backup"
readonly ARC_DIR="$TMP_DIR/$ARC_NAME"
readonly TMP_TAR_NAME="archive.tar.gz"
readonly TMP_TAR_NAME="$TMP_DIR/archive.tar.gz"

if [ -z "$JENKINS_HOME" -o -z "$DEST_FILE" ] ; then
usage >&2
exit 1
fi

if [[ -f "$TMP_DIR/$TMP_TAR_NAME" ]]; then
rm "$TMP_DIR/$TMP_TAR_NAME"
fi
rm -rf "$ARC_DIR"
rm -rf "$ARC_DIR" "$TMP_TAR_NAME"
mkdir -p "$ARC_DIR/"{plugins,jobs,users,secrets}

cp "$JENKINS_HOME/"*.xml "$ARC_DIR"

cp "$JENKINS_HOME/plugins/"*.[hj]pi "$ARC_DIR/plugins"
hpi_pinned_count=$(find $JENKINS_HOME/plugins/ -name *.hpi.pinned | wc -l)
jpi_pinned_count=$(find $JENKINS_HOME/plugins/ -name *.jpi.pinned | wc -l)
if [ $hpi_pinned_count -ne 0 -o $jpi_pinned_count -ne 0 ]; then
cp "$JENKINS_HOME/plugins/"*.[hj]pi.pinned "$ARC_DIR/plugins"
fi
cp -R "$JENKINS_HOME/users/"* "$ARC_DIR/users"
cp -R "$JENKINS_HOME/secrets/"* "$ARC_DIR/secrets"

cd "$JENKINS_HOME/jobs/"
ls -1 | while read job_name
do
mkdir -p "$ARC_DIR/jobs/$job_name/"
find "$JENKINS_HOME/jobs/$job_name/" -maxdepth 1 -name "*.xml" | xargs -I {} cp {} "$ARC_DIR/jobs/$job_name/"
done
if [ -d "$JENKINS_HOME/users/" ] ; then
cp -R "$JENKINS_HOME/users/"* "$ARC_DIR/users"
fi

cd "$TMP_DIR"
tar -czvf "$TMP_DIR/$TMP_TAR_NAME" "$ARC_NAME/"*
if [ -d "$JENKINS_HOME/secrets/" ] ; then
cp -R "$JENKINS_HOME/secrets/"* "$ARC_DIR/secrets"
fi

if [ -d "$JENKINS_HOME/jobs/" ] ; then
cd "$JENKINS_HOME/jobs/"
ls -1 | while read job_name ; do
mkdir -p "$ARC_DIR/jobs/$job_name/"
find "$JENKINS_HOME/jobs/$job_name/" -maxdepth 1 -name "*.xml" | xargs -I {} cp {} "$ARC_DIR/jobs/$job_name/"
done
fi

cd "$CUR_DIR"
cp "$TMP_DIR/$TMP_TAR_NAME" "$DEST_FILE"
cd "$TMP_DIR"
tar -czvf "$TMP_TAR_NAME" "$ARC_NAME/"*
mv -f "$TMP_TAR_NAME" "$DEST_FILE"
rm -rf "$ARC_DIR"

exit 0