2016-04-19 22:34:25 +02:00
|
|
|
/*
|
2016-04-26 23:02:26 +02:00
|
|
|
* Copyright (C) 2016 Didier Clermonté <dclermonte@april.org>
|
2016-04-19 22:34:25 +02:00
|
|
|
* Copyright (C) 2016 Christian Pierre Momon <christian.momon@devinsy.fr>
|
|
|
|
*
|
|
|
|
* This file is part of Siba.
|
|
|
|
*
|
|
|
|
* Siba is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2016-03-29 23:25:18 +02:00
|
|
|
package org.dclermonte.siba.model;
|
2016-03-26 23:26:19 +01:00
|
|
|
|
2016-04-26 23:02:26 +02:00
|
|
|
import java.io.BufferedOutputStream;
|
2016-03-27 22:32:35 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
2016-05-02 22:28:35 +02:00
|
|
|
import java.io.FileReader;
|
2016-03-27 22:32:35 +02:00
|
|
|
import java.io.FileWriter;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.security.MessageDigest;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2016-04-01 00:08:32 +02:00
|
|
|
import java.time.LocalDateTime;
|
2016-04-07 00:54:03 +02:00
|
|
|
import java.time.format.DateTimeFormatter;
|
2016-05-19 18:40:00 +02:00
|
|
|
import java.util.ResourceBundle;
|
2016-03-27 22:32:35 +02:00
|
|
|
|
|
|
|
import org.apache.commons.compress.archivers.ArchiveException;
|
|
|
|
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
2016-04-26 23:02:26 +02:00
|
|
|
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
|
2016-04-12 00:41:43 +02:00
|
|
|
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
|
2016-03-27 22:32:35 +02:00
|
|
|
import org.apache.commons.compress.utils.IOUtils;
|
2016-05-18 23:20:29 +02:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2016-05-19 18:40:00 +02:00
|
|
|
import org.dclermonte.siba.SibaBackupSourceMissingException;
|
|
|
|
import org.dclermonte.siba.SibaBackupSourceNotDirectoryException;
|
|
|
|
import org.dclermonte.siba.SibaBackupTargetMissingException;
|
|
|
|
import org.dclermonte.siba.SibaException;
|
2016-03-27 22:32:35 +02:00
|
|
|
|
2016-05-18 23:20:29 +02:00
|
|
|
/**
|
|
|
|
* The model class
|
|
|
|
*
|
|
|
|
* @author papou
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-03-29 23:25:18 +02:00
|
|
|
public class SibaManager
|
2016-03-26 23:26:19 +01:00
|
|
|
{
|
2016-05-19 18:40:00 +02:00
|
|
|
private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("org.dclermonte.siba.cli.messages"); //$NON-NLS-1$
|
|
|
|
|
2016-05-18 23:20:29 +02:00
|
|
|
/**
|
2016-05-19 18:40:00 +02:00
|
|
|
* This method perform the backup.
|
|
|
|
*
|
2016-05-18 23:20:29 +02:00
|
|
|
* @param fileToSave
|
|
|
|
* @param target
|
|
|
|
* @return
|
|
|
|
* @throws ArchiveException
|
|
|
|
* @throws IOException
|
2016-05-19 18:40:00 +02:00
|
|
|
* @throws SibaBackupTargetMissingException
|
2016-05-18 23:20:29 +02:00
|
|
|
*/
|
2016-05-16 23:40:07 +02:00
|
|
|
public static File backup(final File fileToSave, final File target)
|
2016-05-19 18:40:00 +02:00
|
|
|
throws ArchiveException, IOException, SibaException
|
2016-04-12 00:41:43 +02:00
|
|
|
{
|
2016-05-19 18:40:00 +02:00
|
|
|
File result = null;
|
2016-05-16 23:40:07 +02:00
|
|
|
{
|
2016-05-19 18:40:00 +02:00
|
|
|
if (!target.exists())
|
|
|
|
{
|
|
|
|
throw new SibaBackupTargetMissingException(BUNDLE.getString("missingTargetFile.text"));
|
|
|
|
}
|
|
|
|
else if (!fileToSave.exists())
|
|
|
|
{
|
|
|
|
throw new SibaBackupSourceMissingException(BUNDLE.getString("missingDirectoryToSave.text"));
|
|
|
|
}
|
|
|
|
else if (!fileToSave.isDirectory())
|
|
|
|
{
|
|
|
|
throw new SibaBackupSourceNotDirectoryException(BUNDLE.getString("NotDirectorySource.text"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int pathLength = fileToSave.getParentFile().getAbsolutePath().length();
|
|
|
|
|
|
|
|
LocalDateTime date = LocalDateTime.now();
|
|
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH'h'mm'mn'ss's'");
|
|
|
|
String textDate = date.format(formatter);
|
|
|
|
String outputFileNameWithoutExtension = fileToSave.getName() + "-" + textDate;
|
|
|
|
|
|
|
|
result = new File(target + "/" + outputFileNameWithoutExtension + ".tgz");
|
|
|
|
OutputStream gzipOutputStream = new GzipCompressorOutputStream(
|
|
|
|
new BufferedOutputStream(new FileOutputStream(result)));
|
|
|
|
TarArchiveOutputStream out = new TarArchiveOutputStream(gzipOutputStream);
|
|
|
|
directoryToSave(fileToSave, out, pathLength);
|
|
|
|
out.close();
|
|
|
|
fileCheckMD5(result, target + "/" + outputFileNameWithoutExtension + ".tgz.md5");
|
|
|
|
}
|
2016-04-23 17:50:13 +02:00
|
|
|
|
2016-05-19 18:40:00 +02:00
|
|
|
return result;
|
|
|
|
}
|
2016-05-02 22:28:35 +02:00
|
|
|
}
|
|
|
|
|
2016-05-18 23:20:29 +02:00
|
|
|
/**
|
2016-05-19 18:40:00 +02:00
|
|
|
* This method is used to check the file.
|
|
|
|
*
|
2016-05-18 23:20:29 +02:00
|
|
|
* @param choosenFile
|
|
|
|
* @return
|
|
|
|
* @throws IOException
|
|
|
|
* @throws NoSuchAlgorithmException
|
|
|
|
*/
|
2016-05-02 22:28:35 +02:00
|
|
|
public static boolean check(final File choosenFile) throws IOException, NoSuchAlgorithmException
|
|
|
|
{
|
|
|
|
boolean result;
|
|
|
|
FileReader fileReader = new FileReader(choosenFile);
|
|
|
|
char[] md5 = new char[32];
|
|
|
|
byte[] md5byte = new byte[32];
|
|
|
|
char[] fileToCheck = new char[(int) (choosenFile.length()) - 32];
|
|
|
|
String md5String = new String();
|
|
|
|
for (int index = 0; index < 32; index++)
|
|
|
|
{
|
|
|
|
md5[index] = (char) fileReader.read();
|
|
|
|
md5byte[index] = (byte) md5[index];
|
|
|
|
md5String = md5String + md5[index];
|
|
|
|
}
|
|
|
|
fileReader.read();
|
|
|
|
String fileNameToString = new String();
|
|
|
|
for (int index = 36; index < (choosenFile.length() + 2); index++)
|
|
|
|
{
|
|
|
|
fileToCheck[index - 36] = (char) fileReader.read();
|
|
|
|
fileNameToString = fileNameToString + fileToCheck[index - 36];
|
|
|
|
}
|
|
|
|
|
|
|
|
fileReader.close();
|
|
|
|
File fileToCheck1 = new File(choosenFile.getParent() + "/" + fileNameToString);
|
2016-04-26 23:02:26 +02:00
|
|
|
|
2016-05-18 23:20:29 +02:00
|
|
|
if (StringUtils.equals(md5String, md5(fileToCheck1)))
|
2016-05-02 22:28:35 +02:00
|
|
|
{
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = false;
|
|
|
|
}
|
2016-04-19 22:34:25 +02:00
|
|
|
return result;
|
2016-04-01 00:08:32 +02:00
|
|
|
}
|
2016-04-12 00:41:43 +02:00
|
|
|
|
2016-05-19 18:40:00 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* This method generate ArchiveEntry.
|
|
|
|
*
|
|
|
|
* @param directory
|
|
|
|
* @param outputStream
|
|
|
|
* @param pathLength
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
2016-05-02 22:28:35 +02:00
|
|
|
public static void directoryToSave(final File directory, final TarArchiveOutputStream outputStream,
|
|
|
|
final int pathLength) throws IOException
|
2016-04-07 00:54:03 +02:00
|
|
|
{
|
2016-04-12 00:41:43 +02:00
|
|
|
for (File file : directory.listFiles())
|
2016-03-27 22:32:35 +02:00
|
|
|
{
|
2016-04-18 23:22:33 +02:00
|
|
|
|
2016-04-12 00:41:43 +02:00
|
|
|
if (file.isDirectory())
|
|
|
|
{
|
2016-05-17 23:59:42 +02:00
|
|
|
if (file.listFiles().length == 0)
|
|
|
|
{
|
|
|
|
TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(file);
|
|
|
|
String pathPartiel = file.getPath().substring(pathLength);
|
|
|
|
tarArchiveEntry.setName(pathPartiel);
|
|
|
|
outputStream.putArchiveEntry(tarArchiveEntry);
|
|
|
|
outputStream.closeArchiveEntry();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
directoryToSave(file, outputStream, pathLength);
|
|
|
|
}
|
|
|
|
|
2016-04-12 00:41:43 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-26 23:02:26 +02:00
|
|
|
TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(file);
|
2016-04-18 23:22:33 +02:00
|
|
|
String pathPartiel = file.getPath().substring(pathLength);
|
2016-04-26 23:02:26 +02:00
|
|
|
tarArchiveEntry.setName(pathPartiel);
|
|
|
|
outputStream.putArchiveEntry(tarArchiveEntry);
|
2016-04-12 00:41:43 +02:00
|
|
|
IOUtils.copy(new FileInputStream(file), outputStream);
|
|
|
|
outputStream.closeArchiveEntry();
|
2016-04-26 23:02:26 +02:00
|
|
|
|
2016-04-12 00:41:43 +02:00
|
|
|
}
|
2016-03-27 22:32:35 +02:00
|
|
|
}
|
2016-04-12 00:41:43 +02:00
|
|
|
|
2016-03-27 22:32:35 +02:00
|
|
|
}
|
2016-04-12 00:41:43 +02:00
|
|
|
|
2016-05-18 23:20:29 +02:00
|
|
|
/**
|
2016-05-19 18:40:00 +02:00
|
|
|
*
|
|
|
|
* This method Generate the file with MD5.
|
|
|
|
*
|
2016-05-18 23:20:29 +02:00
|
|
|
* @param directoryToSave
|
|
|
|
* @param destination
|
|
|
|
* @return
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
2016-05-19 18:40:00 +02:00
|
|
|
public static File fileCheckMD5(final File directoryToSave, final String destination) throws IOException
|
2016-04-12 00:41:43 +02:00
|
|
|
{
|
2016-04-23 17:50:13 +02:00
|
|
|
File result;
|
|
|
|
|
|
|
|
result = new File(destination);
|
2016-05-18 23:20:29 +02:00
|
|
|
|
|
|
|
FileWriter fileWriter = new FileWriter(result);
|
|
|
|
fileWriter.write(md5(directoryToSave));
|
|
|
|
String newLine = System.getProperty("line.separator");
|
|
|
|
fileWriter.append(" " + directoryToSave.getName() + newLine);
|
|
|
|
fileWriter.close();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-19 18:40:00 +02:00
|
|
|
* This method calculate the MD5 itself.
|
|
|
|
*
|
2016-05-18 23:20:29 +02:00
|
|
|
* @param input
|
|
|
|
* @return
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
|
|
|
public static String md5(final File input) throws IOException
|
|
|
|
{
|
|
|
|
String result;
|
|
|
|
StringBuilder hashString = new StringBuilder();
|
2016-04-12 00:41:43 +02:00
|
|
|
try
|
|
|
|
{
|
2016-05-18 23:20:29 +02:00
|
|
|
byte[] byteInput = new byte[(int) input.length()];
|
|
|
|
FileInputStream fileInputStream = new FileInputStream(input);
|
|
|
|
for (int index1 = 0; index1 < input.length(); index1++)
|
2016-04-12 00:41:43 +02:00
|
|
|
{
|
2016-05-18 23:20:29 +02:00
|
|
|
byteInput[index1] = (byte) fileInputStream.read();
|
2016-04-12 00:41:43 +02:00
|
|
|
}
|
2016-03-27 22:32:35 +02:00
|
|
|
|
2016-04-12 00:41:43 +02:00
|
|
|
byte[] hash = null;
|
|
|
|
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
|
2016-05-18 23:20:29 +02:00
|
|
|
hash = messageDigest.digest(byteInput);
|
|
|
|
|
2016-04-12 00:41:43 +02:00
|
|
|
for (int index = 0; index < hash.length; index++)
|
|
|
|
{
|
|
|
|
String hex = Integer.toHexString(hash[index]);
|
|
|
|
if (hex.length() == 1)
|
|
|
|
{
|
|
|
|
hashString.append('0');
|
|
|
|
hashString.append(hex.charAt(hex.length() - 1));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hashString.append(hex.substring(hex.length() - 2));
|
|
|
|
}
|
|
|
|
}
|
2016-04-19 22:34:25 +02:00
|
|
|
fileInputStream.close();
|
2016-04-12 00:41:43 +02:00
|
|
|
}
|
|
|
|
catch (NoSuchAlgorithmException e)
|
|
|
|
{
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2016-05-18 23:20:29 +02:00
|
|
|
result = hashString.toString();
|
2016-04-19 22:34:25 +02:00
|
|
|
return result;
|
2016-04-12 00:41:43 +02:00
|
|
|
}
|
2016-04-26 23:02:26 +02:00
|
|
|
|
2016-04-12 00:41:43 +02:00
|
|
|
}
|