1

I'm currently using the following shell command to copy the contents of a local directory to another directory:

cp -a /var/lib/jenkins /opt/jenkinsbackups

Once the files are copied, I am tarring the copied directory then committing the compressed directory to source control (Git, Bitbucket). The shell commands look like thus:

  1. cp -a /var/lib/jenkins /opt/jenkinsbackups
  2. cd /opt/jenkinsbackups
  3. tar czf jenkinsBackup_$(date +%Y%m%d-%H%M%S).tar.gz jenkins/
  4. rm -rf /opt/jenkinsbackups/jenkins/
  5. git add –all
  6. git commit -m "Jenkins Automated Backup"
  7. git push

The process for the shell scripts is as follows:

  1. Copy Jenkins home directory to the directory where the backup will be stored.
  2. Navigate to the backup directory locally.
  3. Compress the copy of Jenkins home directory.
  4. Remove the uncompressed copy of Jenkins home directory within the backup directory.
  5. Git add newly compressed backup.
  6. Git commit the changes (adding the compressed backup file), with the commit message “Jenkins Automated Backup”.
  7. Git push to the master branch of the repository within Bitbucket.

Shell one liner I'm using:

cp -a /var/lib/jenkins /opt/jenkinsbackups && cd /opt/jenkinsbackups && tar czf jenkinsBackup_$(date +%Y%m%d-%H%M%S).tar.gz jenkins/ && rm -rf /opt/jenkinsbackups/jenkins/ && git add --all && git commit -m "Jenkins Automated Backup" && git push

My questions is, if there are some files I need to exclude in the copy prior to compressing the directory, how would I modify the shell command to copy the directory to exclude the files? For instance, rather than copying all files:

cp -a /var/lib/jenkins /opt/jenkinsbackups

I need to exclude .ssh/ and secrets folders found within the /var/lib/jenkins directory from being copied, and also need to retain the original files that were excluded in the /var/lib/jenkins directory. I'm thinking that after the directory is copied and before it is compressed, just delete the files from the uncompressed copy of the directory.

  • Would you like to keep the .ssh and secrets folder in the original /var/lib/jenkins? is it okay that those folder will be erased (but not backup)? – Yaron Jul 26 '17 at 13:07
  • I will need to keep the .ssh and secrets folders in the original /var/lib/jenkins directory. – J0991 Jul 26 '17 at 13:14
0

You can implement it like that:

  • tar the original folder (excluding the folder you don't want to backup)

    tar czf jenkinsBackup_$(date +%Y%m%d-%H%M%S).tar.gz --exclude=dir_to_exclude *

  • cd to the destination folder

  • Extract the tar file in the destination folder
  • Backup the tar file
  • Remove the original folder

In order to remove the original folder excluding some folders/files you can use the following method take from SO Q&A

find . -type 'f' | grep -v "NameToExclude" | xargs rm 
find . -type 'd' | grep -v "NameToExclude" | xargs rmdir

In your case, if you want to exclude two directories (.ssh and secret), you can use:

find . -type 'f' | xargs rm 
find . -type 'd' | grep -v ".ssh" | grep -v "secret"| xargs rmdir

man tar

--exclude=PATTERN
    exclude files, given as a PATTERN
  • Sorry I'm not exactly clear here on the process. I will edit my originally post momentarily to provide more information which may help to make what it is I'm trying to achieve more clear. – J0991 Jul 26 '17 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.