Upload/Download file to/fro MongoDB in Java

// This code is just for my reference

 public static void main(String[] args) {
Logger mongoLogger = Logger.getLogger( "org.mongodb.driver" );
mongoLogger.setLevel(Level.SEVERE);

MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("testdb");

GridFSBucket gridFSFilesBucket = GridFSBuckets.create(database);
ObjectId fileId = new ObjectId();
try {

InputStream streamToUploadFrom = new FileInputStream(new File("d:\\200mb.mkv"));

// GridFSUploadOptions options = new GridFSUploadOptions()
// .chunkSizeBytes(1000)
// .metadata(new Document("type", "presentation"));

fileId = gridFSFilesBucket.uploadFromStream("myfile", streamToUploadFrom);

System.out.println("ObjectID" + fileId);
} catch(FileNotFoundException ex) {
System.out.println("Error" + ex.getMessage());
}


try {
FileOutputStream streamToDownloadTo = new FileOutputStream("d:/out.mkv");
gridFSFilesBucket.downloadToStream(fileId , streamToDownloadTo);
streamToDownloadTo.close();
System.out.println("Finished!");
} catch (IOException e) {
// handle exception
}
}

Bookmark: Raise event from a WPF User control

This blog is just for sample code keeping.

// UserControl1.xaml

<UserControl x:Class=”WpfApp1.UserControl1″

             xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”

             xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”

             xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006″

             xmlns:d=”http://schemas.microsoft.com/expression/blend/2008″

             xmlns:local=”clr-namespace:WpfApp1″

             mc:Ignorable=”d” Background=”Red” Height=”186.646″ Width=”411.693″>

    <Grid>

        <Button Content=”Button” HorizontalAlignment=”Left” Margin=”80,72,0,0″ VerticalAlignment=”Top” Width=”75″ Click=”Button_Click”/>

    </Grid>

</UserControl>


// UserControl1.xaml.cs

namespace WpfApp1

{

    /// <summary>

    /// Interaction logic for UserControl1.xaml

    /// </summary>

    public partial class UserControl1 : UserControl

    {

        public event EventHandler MyButtonClick;

        public UserControl1()

        {

            InitializeComponent();

        }

        private void Button_Click(object sender, RoutedEventArgs e)

        {

            this.MyButtonClick(this, new EventArgs());

        }

    }

}


// UserControl2.xaml

<UserControl x:Class=”WpfApp1.UserControl2″

             xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”

             xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”

             xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006″

             xmlns:d=”http://schemas.microsoft.com/expression/blend/2008″

             xmlns:local=”clr-namespace:WpfApp1″

             mc:Ignorable=”d”

             d:DesignHeight=”450″ d:DesignWidth=”800″ Background=”Green”>

    <Grid>

    </Grid>

</UserControl>

// MainWindow.xaml

<Window x:Class=”WpfApp1.MainWindow”

        xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”

        xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”

        xmlns:d=”http://schemas.microsoft.com/expression/blend/2008″

        xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006″

        xmlns:local=”clr-namespace:WpfApp1″

        mc:Ignorable=”d”

        Title=”MainWindow” Height=”450″ Width=”800″>

    <Grid x:Name=”MyGrid”>

        <local:UserControl1 x:Name=”userControl1″ HorizontalAlignment=”Left” Height=”100″ Margin=”99,62,0,0″ VerticalAlignment=”Top” Width=”242″ MyButtonClick=”UserControl1_MyButtonClick” />

    </Grid>

</Window>

// MainWindow.xaml.cs

namespace WpfApp1

{

    /// <summary>

    /// Interaction logic for MainWindow.xaml

    /// </summary>

    public partial class MainWindow : Window

    {

        private void UserControl1_MyButtonClick(object sender, EventArgs e)

        {

        

        }

    }

}

Bash script to backup using lftp

Can use below script to gzip a folder, and upload to a FTP site. We can use a cron task to schedule it.


#!/bin/bash
Fn=myvps-backup-$(date +%Y%m%d).tar.gz
tar cf - websites | gzip -9 - > $Fn lftp -u user@name,password myhostname.com << EOF
set ssl:check-hostname no
set ssl:verify-certificate false
cd /public_ftp
put $Fn
bye
EOF