143 lines
5.4 KiB
JavaScript
143 lines
5.4 KiB
JavaScript
#! /usr/bin/env runjs
|
|
|
|
requireJAR('aliyun-sdk-oss-2.4.1-SNAPSHOT.jar');
|
|
requireJAR('commons-beanutils-1.8.0.jar');
|
|
requireJAR('commons-codec-1.9.jar');
|
|
requireJAR('commons-collections-3.2.1.jar');
|
|
requireJAR('commons-lang-2.5.jar');
|
|
requireJAR('commons-logging-1.2.jar');
|
|
requireJAR('ezmorph-1.0.6.jar');
|
|
requireJAR('hamcrest-core-1.1.jar');
|
|
requireJAR('httpclient-4.4.1.jar');
|
|
requireJAR('httpcore-4.4.1.jar');
|
|
requireJAR('jdom-1.1.jar');
|
|
requireJAR('json-lib-2.4-jdk15.jar');
|
|
|
|
var File = java.io.File;
|
|
var System = java.lang.System;
|
|
var FileInputStream = java.io.FileInputStream;
|
|
var FileOutputStream = java.io.FileOutputStream;
|
|
|
|
var CounterInputStream = Packages.me.hatter.tools.commons.io.CounterInputStream;
|
|
var RFile = Packages.me.hatter.tools.commons.io.RFile;
|
|
var IOUtil = Packages.me.hatter.tools.commons.io.IOUtil;
|
|
|
|
var main = () => {
|
|
var start = $$.date().millis();
|
|
|
|
if ($ARGS.length == 0) {
|
|
println('Config file is not assigned.');
|
|
return;
|
|
}
|
|
|
|
var configJSON = null;
|
|
try {
|
|
var config = RFile.from($ARGS[0]).string();
|
|
configJSON = JSON.parse(config);
|
|
} catch(e) {
|
|
println('Parse config error: ' + e);
|
|
}
|
|
|
|
if (!configJSON || !configJSON.bucket || !configJSON.endpoint || !configJSON.accessKey || !configJSON.secretKey || !configJSON.backupFile) {
|
|
println('----- Config format -----');
|
|
println('{');
|
|
println(' \"endpoint\": \"<ENDPOINT>\",');
|
|
println(' \"accessKey\": \"<AK>\",');
|
|
println(' \"secretKey\": \"<SK>\",');
|
|
println(' \"bucket\": \"<BUCKET>\",');
|
|
println(' \"backupFile\": \"<BACKUPFILE>\",');
|
|
println(' \"path\": \"default-backup-path\",');
|
|
println(' \"limit\": \"10\",');
|
|
println('}');
|
|
return;
|
|
}
|
|
configJSON.path = configJSON.path || 'default-backup-path';
|
|
configJSON.limit = configJSON.limit || 10;
|
|
|
|
var OSSClient = Packages.com.aliyun.oss.OSSClient;
|
|
var ListObjectsRequest = Packages.com.aliyun.oss.model.ListObjectsRequest;
|
|
var client = new OSSClient(configJSON.endpoint, configJSON.accessKey, configJSON.secretKey);
|
|
try {
|
|
if (!client.doesBucketExist(configJSON.bucket)) {
|
|
println("Bucket NOT exists: " + configJSON.bucket);
|
|
return;
|
|
}
|
|
|
|
var getCounter = (() => {
|
|
var total = 0;
|
|
var step = 1024 * 64;
|
|
return (len) => {
|
|
var t = total;
|
|
total += len;
|
|
var n = parseInt(total / step) - parseInt(t / step);
|
|
if (n > 0) {
|
|
for (var i = 0; i < n; i++) {
|
|
print('.');
|
|
}
|
|
}
|
|
};
|
|
});
|
|
|
|
var backupFiles = (configJSON.backupFile instanceof Array)? configJSON.backupFile: [configJSON.backupFile];
|
|
|
|
backupFiles.forEach((backupFile) => {
|
|
println('+++++ Start backup file: ' + backupFile + ' +++++');
|
|
|
|
var yyyyMMddHHmmss = $$.date().fmt('yyyyMMddHHmmss').format($$.date().today());
|
|
var backupFileFile = new File(backupFile);
|
|
if (!backupFileFile.exists()) {
|
|
println("File not exists: " + backupFile);
|
|
return; // continue next file
|
|
}
|
|
|
|
var fileName = backupFileFile.getName();
|
|
var actualBackupFile = backupFile;
|
|
var actualBackupFileSuffix = "";
|
|
if (configJSON.gpg && configJSON.gpg.keyId) {
|
|
var newFilePath = new File(System.getProperty("java.io.tmpdir"), fileName + '-' + yyyyMMddHHmmss + ".gpg").getAbsolutePath();
|
|
actualBackupFile = newFilePath;
|
|
actualBackupFileSuffix = ".gpg";
|
|
println("Encrypt file with GPG: " + newFilePath);
|
|
$$.shell().commands('sh', '-c', 'gpg -r ' + configJSON.gpg.keyId + ' -e -o "' + newFilePath + '" ' + backupFile).start();
|
|
}
|
|
|
|
println('Upload file: ' + backupFile + '-' + yyyyMMddHHmmss + actualBackupFileSuffix);
|
|
var databaseDbGpg = new FileInputStream(actualBackupFile);
|
|
var counterIS = new CounterInputStream(databaseDbGpg, getCounter());
|
|
client.putObject(configJSON.bucket, configJSON.path + '/' + fileName + '-' + yyyyMMddHHmmss + actualBackupFileSuffix, counterIS);
|
|
println();
|
|
|
|
println('Clear OSS history files.');
|
|
var objectListing = client.listObjects(
|
|
new ListObjectsRequest(configJSON.bucket)
|
|
.withMaxKeys(100)
|
|
.withPrefix(configJSON.path + "/" + fileName + '-')
|
|
);
|
|
|
|
var sums = $ARRAY(objectListing.getObjectSummaries());
|
|
sums = sums.filter((s) => {
|
|
return (!(configJSON.path + "/").equals(s.getKey()));
|
|
}).filter((s) => {
|
|
return s.getKey().contains('/' + fileName + '-');
|
|
}).sort((a, b) => {
|
|
return b.getLastModified().compareTo(a.getLastModified());
|
|
});
|
|
|
|
for (var i = configJSON.limit; i < sums.length; i++) {
|
|
var s = sums[i];
|
|
println("Delete object: " + $$.asList(s.getKey(), s.getSize(), s.getLastModified()));
|
|
client.deleteObject(configJSON.bucket, s.getKey());
|
|
}
|
|
});
|
|
|
|
println('Backup finished, cost: ' + ($$.date().millis() - start) + ' ms.');
|
|
} catch(e) {
|
|
println('OSS Error: ' + e);
|
|
} finally {
|
|
client.shutdown();
|
|
}
|
|
};
|
|
|
|
main();
|
|
|